为什么我在Android Media Player中遇到这3个疯狂的错误

时间:2013-05-18 10:12:19

标签: java android android-mediaplayer

这很疯狂,当我点击播放按钮一次(歌曲没有播放)时我得到3个错误,但是当我再次点击相同的播放按钮时它开始播放。 (在给出这3个错误后,点击2播放歌曲)。我哪里错了?请帮忙,谢谢:D

/* This are the errors which i get,
   start called in state 4
   error (-38, 0)
   Error (-38, 0)
*/

public class Jsonmedia extends Activity {
private MediaPlayer mp;
Button play;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jsonview);

    mp = new MediaPlayer();
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    play = (Button) findViewById(R.id.play);

try{
//Logic that reads the file which is online, splits song_name and song_url in arrays

mp.setDataSource(song_url[0]); //read index zero of song_url;
}
//various catch statements

mp.prepareAsync();
    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
        }
    });

    play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mp.start();
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.jsonmedia, menu);
    return true;
}

}

错误

05-18 15:27:00.764: E/MediaPlayer(1593): start called in state 4
05-18 15:27:00.764: E/MediaPlayer(1593): error (-38, 0)
05-18 15:27:00.764: E/MediaPlayer(1593): Error (-38,0)

1 个答案:

答案 0 :(得分:1)

在媒体播放器准备好之前,您可能单击开始按钮。你应该这样做:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    play.setEnabled(false);


....

mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        play.setEnabled(true);
    }
});

这样,只有在播放器准备就绪时才能点击播放按钮