音频连续播放,即使它移动到下一个活动

时间:2012-09-03 04:00:31

标签: android

我制作了一个activity音频作为背景音乐。当我添加一个跳过按钮移动到另一个activity时,我的第一个活动的背景音乐一直在播放。这是我的代码,请检查并发布我应该添加到我的代码中的内容。提前谢谢!

public class pgone extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 7000;
     public void onAttachedToWindow() {
            super.onAttachedToWindow();
            Window window = getWindow();
            window.setFormat(PixelFormat.RGBA_8888);


        }

     MediaPlayer audio;
     MediaPlayer SLONE;

    private long splashDelay = 6000; 


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pgone);

        TimerTask task = new TimerTask()
        {

            public void run() {

                Intent mainIntent = new Intent().setClass(pgone.this, pgtwo.class);
                startActivity(mainIntent);
                finish();

            }

        };

        final Timer timer = new Timer();
        timer.schedule(task, splashDelay);


        audio = MediaPlayer.create(this,R.raw.storyaud);
        audio.start();

        SLONE = MediaPlayer.create(this,R.raw.sl1);
        SLONE.start();


        final MediaPlayer mp = MediaPlayer.create(this, R.raw.buttonbeep);
        final Vibrator mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);


Button skipButton = (Button)findViewById(R.id.skip);
skipButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        Intent skipIntent = new Intent(pgone.this,choose.class);
        startActivity(skipIntent);
         mp.start();
         mVibrator.vibrate(500);
        finish();
        timer.cancel();
        timer.purge();


    }
});

    }

    protected void exitByBackKey() {

        AlertDialog alertbox = new AlertDialog.Builder(this)
        .setMessage("Do you want to exit the game?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


            public void onClick(DialogInterface arg0, int arg1) {

                finish();
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {
            }
        })
          .show();
    }

}

3 个答案:

答案 0 :(得分:1)

您应该在转到下一个活动之前停止音频。尝试用mp.stop()替换mp.start(),

Button skipButton = (Button)findViewById(R.id.skip);
skipButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        Intent skipIntent = new Intent(pgone.this,choose.class);
        startActivity(skipIntent);
         mp.stop();
         mVibrator.vibrate(500);
        finish();
        timer.cancel();
        timer.purge();


    }
});

答案 1 :(得分:0)

请点击跳过按钮的点击事件mp.stop() mp.reset()而不是mp.start(),有关详细信息,请参阅以下链接。

Media Player example of Playing Sounds

Simple Android Mp3 Media Player

答案 2 :(得分:0)

最简单,最有效的方式 -

@Override
    protected void onPause() {
        super.onPause();
        yoursMusicPlayerObject.release();

    }

当您跳转到下一个活动时,利用活动生命周期的onPause阶段。在此处释放正在使用的I / O资源,这是您的MediaPlayer对象。