Android播放音频文件时出现问题

我已从服务器下载音频文件并使用保存

/data/data/packagename/sounds/filename.mp3

然后,如果我使用播放该音频

.MediaPlayer mp= new MediaPlayer();
            try {
                mp.setDataSource(PATH+"/"+fileName);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            mp.start();

但它显示错误

MediaPlayer:start called in state 2
MediaPlayer:Error(-38,0)
MediaPlayer:Error(-38,0)

问题是什么我对此进行了很多搜索,但没有解决方案可以提出任何解决方案

最佳答案 试试这个代码,

public void audioPlayer(String path, String fileName){
    //set up MediaPlayer    
    MediaPlayer mp = new MediaPlayer();

    try {
        mp.setDataSource(path+"/"+filename.mp3);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        mp.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mp.start();
}
点赞