0

我正在创建一个音乐应用程序。我遇到了一个问题:当用户按下返回/主页时,音乐继续播放。

这是我的APK文件

如何让用户返回主活动并清除当前活动(播放音乐)?

4

5 回答 5

1

您需要覆盖onPause()onStop()停止那里的音乐。

于 2012-05-16T04:39:22.520 回答
1

尝试覆盖该onWindowsFocusChanged(boolean hasFocus)方法。

于 2012-05-16T06:59:56.280 回答
0

您可以使用 Activity 的 onPause() 来停止或暂停 Mediaplayer。

像这样覆盖 onPause(),

@Override
protected void  onPause()
{
    super.onPause();
  if(mediaplayer!=null)
{
    mediaplayer.pause();
}
}

当你再次进入你的活动时,像这样覆盖 onResume(),

@Override
            protected void onResume() {
                super.onResume();

 if(mediaplayer!=null)
{
    mediaplayer.start();
}
}
于 2012-05-16T04:39:53.397 回答
0

在 onpause 和 onstart 之间切换很重要 onPause() 方法被重写以将播放器控制权授予此实例。你也可以参考这个

package org.example.audio;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AudioDemo extends Activity implements OnClickListener {
    private static final String TAG = "AudioDemo";
    private static final String isPlaying = "Media is Playing"; 
    private static final String notPlaying = "Media has stopped Playing"; 

    MediaPlayer player;
    Button playerButton;

    public void onClick(View v) {
        Log.d(TAG, "onClick: " + v);
        if (v.getId() == R.id.play) {
            playPause();
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        player = MediaPlayer.create(this, R.raw.robotrock);
        player.setLooping(false); // Set looping

        // Get the button from the view
        playerButton = (Button) this.findViewById(R.id.play);
        playerButton.setText(R.string.stop_label);
        playerButton.setOnClickListener(this);

        // Begin playing selected media
        demoPlay();

        // Release media instance to system
        player.release();

    }

    @Override
    public void onPause() {
        super.onPause();
        player.pause();
    }

    // Initiate media player pause
    private void demoPause(){
            player.pause();
            playerButton.setText(R.string.play_label);
            Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
            Log.d(TAG, notPlaying);
    }

    // Initiate playing the media player
    private void demoPlay(){
            player.start();
            playerButton.setText(R.string.stop_label);
            Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
            Log.d(TAG, isPlaying);
    }

    // Toggle between the play and pause
    private void playPause() {
        if(player.isPlaying()) {
          demoPause();
        } else {
          demoPlay();
        }   
    }
}
于 2012-05-16T04:51:38.427 回答
0

当用户单击返回按钮时,控件进入以下方法(在您的代码中插入此方法)

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                    // code to pause or stop the music!!!!
            }
            **return false;**
        }
于 2012-05-16T05:19:30.317 回答