0

This is crazy, I get 3 errors when i click the play button once (the song doesn't play), But when i click the same play button again it starts playing. (On 2 click the songs play, after giving these 3 error). Where i am going wrong? Please help, Thank you :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;
}

}

errors

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)
4

1 回答 1

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

这样,您只有在播放器准备好时才能单击播放按钮

于 2013-05-18T10:37:53.103 回答