如何在按下主页按钮时停止媒体播放器

时间:2012-01-16 21:10:20

标签: java android eclipse android-mediaplayer

在我的主要活动的应用程序中,我有一个设置为循环的媒体播放器变量。当我按下主页按钮时,音乐继续播放。我宁愿发生的是当用户点击主页按钮时关闭音乐。从我收集到的主页按钮无法被覆盖,我尝试onPause()但是当我点击我的任何图像按钮时,音乐媒体播放器将停止。有没有解决这个问题?谢谢,如果您需要我的代码,请告诉我。

MainActivity:

package com.crazycastles;


import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;



public class MainActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    static MediaPlayer mainSound;


    @Override 







    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mainSound = MediaPlayer.create(MainActivity.this, R.raw.mainscreen);
        mainSound.setLooping(true);
        mainSound.start();



        //CREATE BUTTON 1 & SOUND
        final MediaPlayer buttonSound = MediaPlayer.create(
                MainActivity.this, R.raw.swords);

        ImageButton button1 = (ImageButton) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        button1Activity.class));
            }
        }); 

        ImageButton multiplayerbutton = (ImageButton) findViewById(R.id.multiplayerbutton);
        multiplayerbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        multiplayerbuttonActivity.class));
            }
        }); 

        ImageButton optionsbutton = (ImageButton) findViewById(R.id.optionsbutton);
        optionsbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        optionsActivity.class));
            }
        }); 


        ImageButton exitbutton = (ImageButton) findViewById(R.id.exitbutton);
        exitbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                finish();
                mainSound.stop();
                System.exit(0);

            }
        }); 





        //END OF BUTTON1 & SOUND



        }
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            mainSound.pause(); //stop media player
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }




}

1 个答案:

答案 0 :(得分:0)

为什么不直接阅读主页按钮事件并暂停或停止媒体播放器?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        mediaPlayer.pause(); //stop media player
        return true;
    }
    return super.onKeyDown(keyCode, event);
}