Java-Android播放外部MP3:错误—在状态1中调用MediaPlayer启动
作者:互联网
我在Android上播放外部MP3文件时遇到问题.我正在使用以下代码:
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();
}
url是外部文件所在的字符串.我确实具有SD卡的写入权限(WRITE_EXTERNAL_STORAGE).
在调试中,我看到:
E MediaPlayer start called in state 1
E MediaPlayer error (-38, 0)
E MediaPlayer Error (-38,0)
可能是什么问题?
解决方法:
找到了答案:在调用player.start()之前,必须根据State Diagram of the MediaPlayer reference运行setDataSource()和prepare().
像这样:
// Use same path as before
player.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/forvo_temp.mp3");
player.prepare();
标签:runtime-error,android-mediaplayer,java,android,mp3 来源: https://codeday.me/bug/20191123/2066633.html