Android Event Listeners

I play with Android to find out the API, and I'm trying to code an action that can listen to changes in audio events. For example, when I press a button, I create a random ringtone. The text โ€œRandom Ringtoneโ€ is displayed on the button, but when you press the button, it says โ€œStopโ€, and pressing it, of course, stops playing the ringtone.

However, the problem is that when the melody stops playing by itself, the button still says โ€œstopโ€.

I looked around to try to find an event listener that could listen when the melody stops playing, but I cannot find it. I saw some information about creating your own listeners, but I'm not interested in doing this (a bit advanced for me right now).

Is there an event listener of this type?

+4
source share
1 answer

Maybe I'm wrong, but I think that the only audio class that triggers an event when it ends is the MediaPlayer class. Something like this should work ...

public class MyActivity extends Activity implements MediaPlayer.OnCompletionListener { MediaPlayer player = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); player = new MediaPlayer(); player.setOnCompletionListener(this); ... } @Override public void onCompletion(MediaPlayer mp) { // Called when playback is complete ... } } 
+6
source

All Articles