如何在Android中更改活动时停止MediaPlayer

时间:2015-08-27 22:08:40

标签: java android android-activity android-mediaplayer

我正在尝试在Android中实现一系列音乐播放器。当我按下后退按钮时,我希望当应用程序返回上一个屏幕时音乐停止。我尝试了很多解决方案并在线搜索但是如果我输入诸如“停止”和“释放”之类的行,应用程序就会崩溃。也许我没有正确地做到这一点?感谢您提供任何帮助......这是我的代码......

public class MusicPlayerA extends Activity {

    private MediaPlayer mediaPlayer;
    public TextView songName, duration;
    private double timeElapsed = 0, finalTime = 0;
    private int forwardTime = 2500, backwardTime = 2500;
    private Handler durationHandler = new Handler();
    private SeekBar seekbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //set the layout of the Activity
        setContentView(R.layout.musicplayerview);
        //initialize views
        initializeViews();
    }

    public void initializeViews(){
        songName = (TextView) findViewById(R.id.songName);
        mediaPlayer = MediaPlayer.create(this, R.raw.druidsad);
        finalTime = mediaPlayer.getDuration();
        duration = (TextView) findViewById(R.id.songDuration);
        seekbar = (SeekBar) findViewById(R.id.seekBar);
        songName.setText("Druids Ad");  
        seekbar.setMax((int) finalTime);
        seekbar.setClickable(true);
    }

    // play mp3 song
    public void play(View view) {
        mediaPlayer.start();
        timeElapsed = mediaPlayer.getCurrentPosition();
        seekbar.setProgress((int) timeElapsed);
        durationHandler.postDelayed(updateSeekBarTime, 100);
    }

    //handler to change seekBarTime
    private Runnable updateSeekBarTime = new Runnable() {
        public void run() {
            //get current position
            timeElapsed = mediaPlayer.getCurrentPosition();
            //set seekbar progress
            seekbar.setProgress((int) timeElapsed);
            //set time remaining
            double timeRemaining = finalTime - timeElapsed;
            duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));

            //repeat yourself that again in 100 miliseconds
            durationHandler.postDelayed(this, 100);
        }
    };

    // pause mp3 song
    public void pause(View view) {
        mediaPlayer.pause();
    }

    // go forward at forwardTime seconds
    public void forward(View view) {
        //check if we can go forward at forwardTime seconds before song endes
        if ((timeElapsed + forwardTime) <= finalTime) {
            timeElapsed = timeElapsed + forwardTime;

            //seek to the exact second of the track
            mediaPlayer.seekTo((int) timeElapsed);
        }
    }

    // go backwards at backwardTime seconds
    public void rewind(View view) {
        //check if we can go back at backwardTime seconds after song starts
        if ((timeElapsed - backwardTime) > 0) {
            timeElapsed = timeElapsed - backwardTime;

            //seek to the exact second of the track
            mediaPlayer.seekTo((int) timeElapsed);
        }
    }


    public void stopMusic(View view) {
        mediaPlayer.stop();
        mediaPlayer.release();
    }



     // handler for back button used on all screens
     public void BackButton2 (View view) {

        //mediaPlayer.pause();
        //mediaPlayer.release();
         //mediaPlayer.stop();
        // mediaPlayer.release();
        stopMusic(view);

        MediaPlayer mMediaPlayer = MediaPlayer.create(this, R.raw.soundbackbutton) ;
        mMediaPlayer.start();

        Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vib.vibrate(200);

        Intent mus = new Intent (this, Music.class);
        startActivity(mus);
     }

    // handler for back button used on all screens
     public void BackButton (View view) {

        // mediaPlayer.pause();
         //mediaPlayer.release();

        // mediaPlayer.stop();
        // mediaPlayer.release();

        MediaPlayer mMediaPlayer = MediaPlayer.create(this, R.raw.soundbackbutton) ;
        mMediaPlayer.start();

        Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vib.vibrate(200);

        Intent mn = new Intent (this, Music.class);
        startActivity(mn);
     }
}

1 个答案:

答案 0 :(得分:1)

当您点击后退按钮时,Android系统应调用Activity.onPause()。我没有看到onPause()的任何实现,除非你的backbutton处理程序正在调用它。我会尝试这样的事情:

@Override
protected void onPause() {
    super.onPause();
    if (mediaPlayer != null) {
        mediaPlayer.pause();
        if (isFinishing()) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}