我正在尝试使用 MediaPlayer 播放音频文件。我想在 MediaPlayer 中播放字节数组。我怎样才能做到这一点?我已经检查过了
public void writeSamples(byte[] samples, int length)
{
// track.write( samples, 0, length);
File tempMp3;
try {
tempMp3 = File.createTempFile("kurchina", ".mp3");
tempMp3.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempMp3);
fos.write(samples);
fos.close();
// Tried reusing instance of media player
// but that resulted in system crashes...
MediaPlayer mediaPlayer = new MediaPlayer();
// Tried passing path directly, but kept getting
// "Prepare failed.: status=0x1"
// so using file descriptor instead
FileInputStream fis = new FileInputStream(tempMp3);
mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
但它不播放音频。它只是在 SD 卡中生成许多文件。并给出这个错误:
06-06 11:02:59.191: E/MediaPlayer(1831): Unable to to create media player
06-06 11:02:59.191: W/System.err(1831): java.io.IOException: setDataSourceFD failed.: status=0x80000000
06-06 11:02:59.201: W/System.err(1831): at android.media.MediaPlayer.setDataSource(Native Method)
06-06 11:02:59.201: W/System.err(1831): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:749)
06-06 11:02:59.201: W/System.err(1831): at org.vinuxproject.sonic.SonicTest$1.run(SonicTest.java:178)
06-06 11:02:59.201: W/System.err(1831): at java.lang.Thread.run(Thread.java:1096)
请帮我。任何形式的帮助表示赞赏。
谢谢