本文介绍了Android的播放外接MP3:错误 - 媒体播放器启动名为状态1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有麻烦玩Android的外部MP3文件。我用下面的code:
I'm having trouble playing an external MP3 file on Android. I'm using the following code:
MediaPlayer player = new MediaPlayer();
try {
BufferedInputStream bis = new BufferedInputStream(new java.net.URL(url).openStream());
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/forvo_temp.mp3");
BufferedOutputStream bos = new BufferedOutputStream(fos,1024);
byte [] data = new byte[1024];
int x=0;
while((x=bis.read(data,0,1024))>=0){
bos.write(data,0,x);
}
player.reset();
player.start();
} catch (Exception e) {
e.printStackTrace();
}
网址
是外部文件是字符串。我有写权限的SD卡( WRITE_EXTERNAL_STORAGE
)。
url
is the string where the external file is. I do have write permission for the SD card (WRITE_EXTERNAL_STORAGE
).
在调试,我看到:
E MediaPlayer start called in state 1
E MediaPlayer error (-38, 0)
E MediaPlayer Error (-38,0)
可能是什么问题呢?
What might the problem be?
推荐答案
找到了答案:在调用 player.start()
,你必须运行的setDataSource()
和 prepare()
,根据的的/reference/android/media/MediaPlayer.html#StateDiagram">State图。
Found the answer: before calling player.start()
, you have to run setDataSource()
and prepare()
, according to the State Diagram of the MediaPlayer reference.
这样的:
// Use same path as before
player.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/forvo_temp.mp3");
player.prepare();
这篇关于Android的播放外接MP3:错误 - 媒体播放器启动名为状态1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!