Вопрос

I'm playing around with Android to learn the API and I'm trying to code an activity which can listen for changes in audio events. For example, the activity I created plays a random ringtone when you press a button. The button displays a text saying "Random Ringtone", but when you press the button it says "Stop" and pressing it will, of course, stop playing the ringtone.

However, the problem is that when the ringtone stops playing on its own, the button still says "stop".

I've looked around to try to find an event listener that could listen for when the ringtone stops playing, but I can't seem to find one. I've seen some info out there about creating your own listeners, but I'm not interested in doing that (a little advanced for me right now).

Does an event listener of this type exist?

Это было полезно?

Решение

I may be wrong but I think the only audio class which raises an event when it finishes playing 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
        ...
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top