OnCompletion is not called when I expect it to

Do I have a MediaPlayer member variable hosted in my parent class and after it starts it never calls onCompletion, or at least onCompletionListener will never catch it? My code looks something like this.

mediaPlayer = Mediaplayer.create(this, currentSample); mediaPlayer.start(); 

Elsewhere in the code is my onCompletionListener

 mediaPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer mp) { if (repeatFlag == true) { handler.removeCallbacks(sampleRunnable); handler.postDelayed(sampleRunnable, delay); } } }); 

I use a handler to call sampleRunnable because I want to encode it with a given delay interval. However, onCompletion never seems to be called. I am pretty sure of this because I set breakpoints in onCompletion that never pause the program, and I tried step by step, and this is never called. Any ideas?

+4
source share
6 answers
  mediaPlayer.start(); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { Log.v("Tag", "Completed"); } }); 

This snippet works great. The only difference is "@Override", and I'm not sure if "@Override" affects the code.

+5
source

In my experience, the listener does not start on some devices: /

+4
source

I also had this problem. This has nothing to do with the Override annotation.

I installed a listener before I configured Visualizer. I moved setOnCompletionListener() after I got access to MediaPlayer for the visualizer - this solved my problem. Make sure the method is called at the last stage.

+1
source

I had to make a separate class for it to work. No amount of override was performed.

  private class SoundtrackPlayerListener implements MediaPlayer.OnCompletionListener{ public void onCompletion(MediaPlayer mp) { //completion code here } } 
0
source

I and a couple of other students faced the same problem in our mobile app course.

I'm not sure what the problem is, but for us it seems like a bug with an emulator. On some Linux devices, OnCompletionEvent did not start at all. On one mac, the Event event is fired in the application, but not in unittests.

Both (application and unittest) worked correctly if they were run on a real device, and not in an emulator.

The AVD we used is the Samsung Galaxy Tab tab (android2.2 api8)

0
source

Yes. I have confirmed that the problem occurs on some devices. My last Android phone got this problem, but not the old one.

If you are not using a streaming audio source, you can replace mMediaPlayer.setOnCompletionListener(...) with

 new Timer().schedule(new TimerTask() { @Override public void run() { actionWhenAudioEndIsReached(); } }, mMediaPlayer.getDuration()); 

It should work :)

-1
source

Source: https://habr.com/ru/post/1337270/


All Articles