我从sd卡播放wav音频文件时遇到麻烦:
这是我的代码:
mp = new MediaPlayer(); //Defined as MediaPlayer above
try {
mp.setDataSource(fileNamePath);
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
fileNamePath采用以下格式:/ storage_emulated/0/MyApp/name.wav
这是我得到的错误代码:
E/MediaPlayer: stop called in state 1, mPlayer(0x0)
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: Error (-38,0)
我希望你能在这里帮助我吗?
谢谢!
答案 0 :(得分:1)
Android媒体播放器对某些wav文件有些麻烦,我建议您使用AudioTrack而不是MediaPlayer。
这是一个例子:
new Thread(new Runnable() {
@Override
public void run() {
try {
// just put here your wav file
File yourWavFile = new File(Environment.getExternalStorageDirectory() + "/" + AUDIO_FOLDER, fileName);
FileInputStream fis = new FileInputStream(yourWavFile);
int minBufferSize = AudioTrack.getMinBufferSize(16_000,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 16_000,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
minBufferSize, AudioTrack.MODE_STREAM);
int i = 0;
byte[] music = null;
try {
music = new byte[512];
at.play();
while ((i = fis.read(music)) != -1)
at.write(music, 0, i);
} catch (IOException e) {
e.printStackTrace();
}
at.stop();
at.release();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();