単純なアニメーション (画面の左右に移動するボール) を伴うアクティビティがあります。
onAnimationRepeat(Animator animator) メソッドで、短い音を再生します (1 秒以下だと思います):
sputnikLeft.start();
デバイスの向きが変わると、アニメーション (animX) が停止してリセットされますが、サウンドはアニメーションの実行時と同じ規則性で再生され続けます。
プレーヤーを無効にするためにいくつかのことを試みましたが、最後に onAnimationCanel(...) メソッドでプレーヤーをオフにしようとしました。
@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;
}
}
onAnimationEnd() で試してみましたが、効果がありませんでした。
アクティビティ:
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();
}
}