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

时间:2012-05-28 12:25:29

标签: android audio media-player

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

保存
/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)

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

1 个答案:

答案 0 :(得分:2)

尝试使用此代码,

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();
}