问题
I have a MediaPlayer member variable posted in my parent class, and after I start it, it doesn't ever call onCompletion, or at least onCompletionListener doesn't ever catch it? My code looks something to this effect
mediaPlayer = Mediaplayer.create(this, currentSample);
mediaPlayer.start();
elsewhere in code is my onCompletionListener
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (repeatFlag == true) {
handler.removeCallbacks(sampleRunnable);
handler.postDelayed(sampleRunnable, delay);
}
}
});
I'm using the handler to call sampleRunnable because I want to loop it at a specified delay interval. However, onCompletion seems to never be called. I'm quite positive of this because I've set breakpoints in onCompletion which never suspends the program, and I've tried stepping through and it never seems to be called. Any ideas?
回答1:
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer arg0)
{
Log.v("Tag", "Completed");
}
});
This snippet works fine. The only difference is the "@Override" and I'm not sure if the "@Override" has effect on the code.
回答2:
On my experience, the listener is not triggered on some devices :/
回答3:
I had this problem as well. It has nothing to do with the Override annotation.
I set the listener before I had configured a Visualizer. I moved the setOnCompletionListener() to after I had accessed the MediaPlayer for the visualizer - this solved my problem. Make sure the method is called at the latest stage.
回答4:
I had to make a separate class to get it working. No amount of overriding worked.
private class SoundtrackPlayerListener implements MediaPlayer.OnCompletionListener{
public void onCompletion(MediaPlayer mp) {
//completion code here
}
}
回答5:
I and a couple of other students experienced the same problem in our mobile-apps course.
I'm not sure what the problem is but to us it seems like a bug with the emulator. On some linux machines the OnCompletionEvent didn't fire at all. On ones mac the Event fired in the application but not in the unittests.
Both (application and unittest) worked correctly if executed on a real device instead of the emulator.
The AVD we were using was the Samsung Galaxy Tab (android2.2 api8)
回答6:
With newer Android versions (from 8 Oreo on) the cause of problem could due to the apps falls asleep. This was the reason that in my app. It fell asleep after playing one song.
You can check and change the the Android battery settings:
1. Battery Optimization
2. tap 'not optimized' then 'all apps'
3. look for your app and change the setting to not optimzed
回答7:
Yeah. I confirmed that the issue does happen on some devices. My latest Android phone got this issue but not the old one.
Unless you use streaming audio source, you can replace mMediaPlayer.setOnCompletionListener(...) with
new Timer().schedule(new TimerTask() {
@Override
public void run() {
actionWhenAudioEndIsReached();
}
}, mMediaPlayer.getDuration());
It should work :)
来源:https://stackoverflow.com/questions/4813486/oncompletion-isnt-being-called-when-i-would-expect-it-to