0

I am playing an mp3 audio from server using the below code. It is working fine, no issues. But sometimes when clicking on Play button, it is taking few seconds to start the audio, it takes some buffering time, in that gap time if i click Play button again and again, then app crashes. How can i have the condition under play button code for checking whether the audio has been buffered and play starting or not? I can't check here if(mediaPlayer.isPlaying()).

private void playAudio(String url) throws Exception
{
    if( (mediaPlayer == null || isCompleted) && !isPaused ) {
        isCompleted = false;
        isPaused = false;
        playpauseButton.setBackgroundResource(R.drawable.pauseimage);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepare();
        mediaPlayer.start();
    }
    else if ( isPaused ) {
        playpauseButton.setBackgroundResource(R.drawable.pauseimage);
        mediaPlayer.start();
        isPaused = false;
    }
    else {
        playpauseButton.setBackgroundResource(R.drawable.playimage);
        isPaused = true;
        mediaPlayer.pause();
    }

    mediaPlayer.setOnCompletionListener(new OnCompletionListener(){

       @Override
       public void onCompletion(MediaPlayer mp) {
           // TODO Auto-generated method stub
           isCompleted = true;
           isPaused = false;
           playpauseButton.setBackgroundResource(R.drawable.playimage);
       }}
    );
}
public void stopAudio(View view) {
    killMediaPlayer();
}
@Override
protected void onDestroy() {
    super.onDestroy();
    killMediaPlayer();
}
private void killMediaPlayer() {
    isPaused = false;
    isCompleted = true;
    playpauseButton.setBackgroundResource(R.drawable.playimage);

    if(mediaPlayer!=null) {
        try {
            mediaPlayer.release();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
Stella
  • 1,728
  • 5
  • 41
  • 95
  • If youre streaming the mp3 make sure to use `prepareAsync` then `setOnPreparedListener` should work – Mario Apr 14 '15 at 20:44
  • There is no streaming, it is playing directly a recorded .mp3 audio file. – Stella Apr 14 '15 at 21:02
  • Instead mediaPlayer.prepare(); If i use mediaPlayer.prepareAsync(); Then the dialog box appears as mentioned by Amine Chikhaoui. But, when this dialog box appears, if i click on the view, then dialog box simply disappears, which is not supposed to be. – Stella Apr 14 '15 at 21:07
  • [This question](http://stackoverflow.com/questions/2502443/prevent-progressdialog-from-being-dismissed-when-i-click-the-search-button-andr) may help you out with that. – Mario Apr 14 '15 at 21:20
  • I fixed it by mProgressDialog.setCanceledOnTouchOutside(false); – Stella Apr 15 '15 at 05:36

1 Answers1

0

You can block the user UI with a ProgressDialog or something like that.

In pseudo-code:

mediaplayer.setOnPreparedListener(this);
mediaplayer.prepare();
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Preparing the audio");
mProgressDialog.show();

And then when the callback is triggered you can start the mediaplayer:

public void OnPrepared(MediaPlayer mp) {
       mProgressDialog.dismiss();
       mp.start();
}
Amine Chikhaoui
  • 345
  • 1
  • 11