我正在尝试使用MediaPlayer播放音频文件.我想在MediaPlayer中播放字节数组.我怎样才能做到这一点?我查了

this

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)

请帮我.任何形式的帮助表示赞赏.

谢谢

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐