3

我在 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)

问题可能是什么?

4

2 回答 2

7

找到了答案:在调用之前player.start(),您必须运行setDataSource()and prepare(),根据MediaPlayer 参考的状态图

像这样:

// Use same path as before
player.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/forvo_temp.mp3");
player.prepare();
于 2013-05-12T19:37:17.743 回答
-4

您需要将AndroidManifest.xml权限设置为:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
于 2016-06-29T13:23:06.193 回答