Reputation: 3828
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?
Upvotes: 8
Views: 9587
Reputation: 51
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;
}
Upvotes: 0
Reputation: 5146
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.
Upvotes: 3
Reputation: 1062
In order to get the onCompletion() function called you should disable looping with a call mediaPlayer.setLooping(false);
Upvotes: 2
Reputation: 748
It seems you have to call setOnCompletionListener AFTER calling start(). Take a look here: https://stackoverflow.com/a/19555480/1860130
Worked for me.
Upvotes: 3
Reputation: 6653
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.
Upvotes: 6
Reputation: 2728
Try to use a sleep method once recording is completed. Also use; .prepare() before the placeback occurs , to avoid crashes.
Upvotes: -3
Reputation: 47
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.
Upvotes: 0