androidandroid-mediaplayeranimationdrawable

how to stop frame animation when mediaplayer completes playback?


I have a frame animation (Animation Drawable) that is supposed to run in sync with an audio (MediaPlayer).

I pause the audio (mediaplayer.pause()) and stop the animation (as I am not able to find a way to pause it) when I pause my Activity class (e.g. When I press the home button of my device). when I resume my Activity, I call mediaplayer.start() and it starts where it was paused. Then, I call frameAnimation.start and it again starts from the beginning and goes on to complete the cycle even after the mediaplayer has completed playback. As a result, the frameAnimation and mediaPlayer get out of sync. What do I do? I am new to android programming. Please help.

My frame animation xml is similar to

android:id="@+id/selected" android:oneshot="true">
<item android:drawable="@drawable/bird1" android:duration="750"/>
<item android:drawable="@drawable/bird2" android:duration="750"/>

And my onResume() is

protected void onResume() {
         mediaplayer.start();
         frameAnimation.start();
         If(!mediaplayer.isPlaying()&&frameAnimation.isRunnng()){
                frameAnimation.stop();                                                                   
}

Clearly, the if statement never gets executed as the mediaplayer is playing then. What else can I do? I am at a total loss.


Solution

  • You should set a MediaPlayer.OnCompletionListener with setOnCompletionListener(): then you'll get a callback when the MediaPlayer finishes and you can stop your animation.