1

我有一个带有简单动画的活动(一个球从屏幕的一侧移动到另一侧)

在 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();
    }
}
4

0 回答 0