如何实现"关闭音乐"按钮Android

时间:2016-12-21 23:09:59

标签: java android android-mediaplayer

我想在我的应用程序中添加一个按钮来关闭音乐,但我不知道如何处理它,我有一个想法,但我确定它远非最好所以我想咨询你。情况如下:

public class MainActivity extends Activity implements OnClickListener {


MediaPlayer easysong;
MediaPlayer normalsong;
MediaPlayer hardsong;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.land_main);

    mContext = this;
    restartButton = (Button)findViewById(R.id.restartButton);

    restartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            easysong = MediaPlayer.create(MainActivity.this, R.raw.arideniro);
            normalsong = MediaPlayer.create(MainActivity.this, R.raw.junior);
            hardsong = MediaPlayer.create(MainActivity.this, R.raw.ketsathis);
            counter = 101;
            i = 500 - dif;

            new Thread(new Runnable() {

                public void run() {
                    if(i==500){
                    easysong.start();}
                    else if(i==375){
                        normalsong.start();
                    }else if(i==250){
                        hardsong.start();
                    }
                    while (counter > 0) {
                        try {

                            Thread.sleep(i);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        counter--;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                scoreText.setText(Integer.toString(counter));
                            }
                        });
                        if(i>150){
                            i/=1.01;}
                        else if(i>90-(dif/10)){
                            i-=1;
                        }
                    }if (counter==0) {
                        mChronometer.stop();
                        if(easysong.isPlaying()) {
                            easysong.stop();
                            easysong.release();
                            easysong = null;
                        }else if(normalsong.isPlaying()){
                            normalsong.stop();
                            normalsong.release();
                            normalsong = null;
                        }else if(hardsong.isPlaying()){
                        hardsong.stop();
                        hardsong.release();
                            hardsong = null;
                        }

这是我使用媒体播放器的应用程序的主要类,现在我删除了大部分代码,因为它与媒体播放器和问题无关,所以不要寻找丢失的括号等。这里是主菜单类,其中将打开和关闭音乐的开关位于:

public class MainMenu extends Activity{

private Button easy;
private Button normal;
private Button hard;
private Button scores;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);

    easy = (Button) findViewById(R.id.btn_easy);
    scores = (Button) findViewById(R.id.btn_highscores);

    easy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dif = 0;
            startGame();
        }
    });
}
public void startGame() {
    Intent intent = new Intent(MainMenu.this, MainActivity.class);
    startActivity(intent);
}

所以我的想法是simnple,在MainActivity中添加一个变量,如" int p;"并且从MainMenu类改变它在0和1之间的状态,然后我将添加每个开始音乐的行if(p == 1),但这是一个好方法吗?此外,我希望在关闭应用程序时保存int的值

0 个答案:

没有答案