8

In my app i have created an audio player (only WAV files) using MediaPlayer API. But the player doesn't give callback to onCompletion Listener everytime. Sometimes it gives callback but not everytime. I am doing some audio processing on wav file , like insertion and overwriting.

Is it because any missing in audio header? Why it doesn't give callback when the playback is completed?

Raneez Ahmed
  • 3,808
  • 3
  • 35
  • 58
  • In case the app is on sleep. Try this https://stackoverflow.com/questions/4813486/oncompletion-isnt-being-called-when-i-would-expect-it-to/60911323#60911323 – SJX Mar 29 '20 at 09:28

7 Answers7

6

Make sure that the headers of file is set correctly. If somethings in header is missing callback to the onCompletion may not occur.

If mediaplayer is playing a .wav file, seeking may happen correctly but a jerking will be there . So when the playingback completes there will be a differecnce of 0-1000 ms between total duration of file and onCompletion respectively. So if such a situation comes you should guess that as onColmpletion and do what you wanted. Thats a bit tricky way to get the onCompletion.

I faced the same problem while playing a .wav file with the mediaplayer. This is not a good way to solve this issue, but I tackled the same problem like this when i was having the same situation. Hope this will help you too in some ways.

Sreedev
  • 6,563
  • 5
  • 43
  • 66
3

It seems you have to call setOnCompletionListener AFTER calling start(). Take a look here: https://stackoverflow.com/a/19555480/1860130

Worked for me.

Community
  • 1
  • 1
bernardo.g
  • 826
  • 1
  • 12
  • 27
2

In order to get the onCompletion() function called you should disable looping with a call mediaPlayer.setLooping(false);

Hack06
  • 963
  • 1
  • 12
  • 20
2

Make sure the MediaPlayer is not a local(temporal) variable. Since the local variable will be collected by GC. In that case, the onCompletion will have no chance to be invoked.

DàChún
  • 4,751
  • 1
  • 36
  • 39
0

You're using the correct Method but have you passed any code in the "// do some tasks here when playback completes " AREA.

If not then the callback wont make any sense.

mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

   public void onCompletion(MediaPlayer mp) {

        Toast.makeText(getApplicationContext(),"Playback Completes", Toast.LENGTH_SHORT).show();
    }
});

This method will pop a Toast on completion of playback.

Rahul
  • 47
  • 8
0

Return true to OnErrorListner method on MediaPlayer

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    //Invoked when there has been an error during an asynchronous operation
    switch (what) {
        case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
            Log.e("MediaPlayer Error", "MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + extra);
            break;
        case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
            Log.e("MediaPlayer Error", "MEDIA ERROR SERVER DIED " + extra);
            break;
        case MediaPlayer.MEDIA_ERROR_UNKNOWN:
            Log.e("MediaPlayer Error", "MEDIA ERROR UNKNOWN " + extra);
            break;
    }
    return true;
}
Abhilasha
  • 51
  • 6
-3

Try to use a sleep method once recording is completed. Also use; .prepare() before the placeback occurs , to avoid crashes.

Zen
  • 2,698
  • 4
  • 28
  • 41