android mediaplayer在state1中调用

时间:2015-04-27 23:54:28

标签: android buffer android-mediaplayer

我正在尝试在应用开始时播放音频文件,但我收到错误消息start called in state 1 Error (-38,0),这是我的代码:

public void audioPlayer(){
        //set up MediaPlayer
        MediaPlayer mp = new MediaPlayer();

        try {
            mp.create(this, R.raw.music);
            onPrepared(mp);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }

1 个答案:

答案 0 :(得分:1)

public void audioPlayer(){
    MediaPlayer mp = new MediaPlayer();
    mp.setOnPreparedListener(mListener);
    try {
        mp.setDataSource(this, R.raw.music);
        mp.prepare();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
private OnPreparedListener mListener=new OnPreparedListener() {

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }
};