I have an activity with a simple animation (a ball moving from side to side of the screen)
in the onAnimationRepeat(Animator animator) method, I play a short sound (1 sec. or less I think):
sputnikLeft.start();
When the device changes orientation, the animation (animX) stops, resets, but the sound keeps playing with the same regularity as it did when the anim was running.
I've tried several things to turn off the player to no avail, lastly attempting to shut it off in the onAnimationCanel(...) method:
@Override
public void onAnimationCancel(Animator animator) {
System.out.println("\n\nonAnimationCancel called\n\n");
/* end sounds */
if (sputnikRight != null && sputnikRight.isPlaying())
{
sputnikRight.stop();
sputnikRight.release();
sputnikRight = null;
}
if (sputnikLeft != null && sputnikLeft.isPlaying())
{
sputnikLeft.stop();
sputnikLeft.release();
sputnikLeft = null;
}
}
I tried in onAnimationEnd() to no effect.
the Activity:
public class AnimationActivity extends AppCompatActivity {
ImageView img;
ObjectAnimator animX;
int animationRunning = 0;
int sputnikLeftRightFlag = 0; /* alternate between 0 and 1 - play left or right sound */
MediaPlayer sputnikLeft;
MediaPlayer sputnikRight;
int sputnikOn = 0;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
....
playPauseBtn = (ImageButton) findViewById( R.id.playPauseImageBtn);
animX = ObjectAnimator.ofFloat(img, "x", (w - (48) ) );
animX.setRepeatCount(repeatCount);
animX.setRepeatMode( 2 );
animX.setDuration( animationSpeed );
totalTime = repeatCount * animationSpeed;
this.setVolumeControlStream(AudioManager.STREAM_SYSTEM);
sputnikLeft = MediaPlayer.create(this, R.raw.sputnik_left);
sputnikLeft.setVolume( 5.0f, 5.0f );
sputnikRight = MediaPlayer.create(this, R.raw.sputnik_right);
sputnikRight.setVolume( 5.0f, 5.0f );
...
animX.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
//System.out.println("\n\nonAnimationStart called\n\n");
}
@Override
public void onAnimationEnd(Animator animator) {
System.out.println("\n\nonAnimationEnd called\n\n");
getWindow().clearFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
System.out.println("\n\n repeat count at end: " + animX.getRepeatCount() + "\n\n");
timeRemaining.setText( Long.toString(animX.getDuration() + animX.getRepeatCount()) );
totalTime = 0;
countRemaining.setText( "" + animX.getRepeatCount() );
animationSpeed = (int) animX.getDuration();
repeatCount = animX.getRepeatCount();
playPauseBtn.setImageResource( R.drawable.ic_play_circle_filled_black_24dp);
animationRunning = 0;
}
@Override
public void onAnimationCancel(Animator animator) {
System.out.println("\n\nonAnimationCancel called\n\n");
/* end sounds */
if (sputnikRight != null && sputnikRight.isPlaying())
{
sputnikRight.stop();
sputnikRight.release();
sputnikRight = null;
}
if (sputnikLeft != null && sputnikLeft.isPlaying())
{
sputnikLeft.stop();
sputnikLeft.release();
sputnikLeft = null;
}
}
@Override
public void onAnimationRepeat(Animator animator) {
totalTime = totalTime - animationSpeed;
System.out.println("\n\nonAnimationRepeat called\n\n");
repeatCount = repeatCount - 1;
countRemaining.setText( Integer.toString( repeatCount ) );
timeRemaining.setText( eyeBallerUtils.getFormattedTimeRemaining( totalTime ) );
if( sputnikOn == 1 ) {
if (sputnikLeftRightFlag == 0) {
sputnikLeft.start();
sputnikLeftRightFlag = 1;
} else if (sputnikLeftRightFlag == 1) {
sputnikRight.start();
sputnikLeftRightFlag = 0;
}
}
}
});
}
...
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Animation Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.eyeballer.eyeballernative/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Animation Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.eyeballer.eyeballernative/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}