Android MediaPlayer - onBackPressed()不起作用

时间:2014-01-29 10:55:15

标签: android android-mediaplayer

我有一个只在循环中播放相同视频的应用。一切都按预期工作,除了一件事:

如果我按下后退按钮,我想停止播放,但我无法让它工作。我的活动完全忽略了我按下它而不是我得到一个ANR。我认为使用onBackPressed()应该处理这种情况。 谁能告诉我,我做错了什么???

这是我的代码:

package dk.test.videoplayer;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends Activity implements 
OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback, OnClickListener {

    private SurfaceView mSurfaceView = null;
    private SurfaceHolder mSurfaceHolder = null;
    private String videoPath;
    private MediaPlayer mMediaPlayer = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("onCreate called");

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.TYPE_APPLICATION_PANEL |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

        mSurfaceView = new SurfaceView(this);
        setContentView(mSurfaceView);
        mSurfaceView.setOnClickListener(this);

        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);

        videoPath = "android.resource://dk.beatpro.videoplayer/raw/demo_video";
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setScreenOnWhilePlaying(true);
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("onResume called");

    }

    @Override
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        System.out.println("onPrepared called");
        if (!mMediaPlayer.isPlaying())
            mMediaPlayer.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
    System.out.println("onCompletion() called");
        playVideo();
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        System.out.println("onPause() called");

        releaseMediaPlayer();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        releaseMediaPlayer();
    }

    public void surfaceCreated(SurfaceHolder holder) {
        System.out.println("surfaceCreated() called");

        mMediaPlayer.setDisplay(mSurfaceHolder);
        playVideo();
    }

    private void playVideo() {
        System.out.println("playVideo() called");
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(this, Uri.parse(videoPath));
            mMediaPlayer.setDisplay(mSurfaceHolder);
            mMediaPlayer.prepareAsync();
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    private void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            if (mMediaPlayer.isPlaying())
                mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        System.out.println("surfaceChanged() called");

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        System.out.println("surfaceDestroyed() called");

    }

    @Override
    public void onBackPressed() {
        System.out.println("onBackPressed() called");
        releaseMediaPlayer();
        super.onBackPressed();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Touch event", Toast.LENGTH_SHORT).show();
        return super.onTouchEvent(event);
    }

    @Override
    public void onClick(View v) {
        System.out.println("onClick() called");
        Toast.makeText(this, "Click click", Toast.LENGTH_SHORT).show();
    }
}

2 个答案:

答案 0 :(得分:0)

在onBackPressed(..)

中的代码前调用super.onBackPressed();

答案 1 :(得分:0)

 @Override
public void onBackPressed() {
     super.onBackPressed();
    System.out.println("onBackPressed() called");
    releaseMediaPlayer();

}

在发布媒体播放器之前,请先调用super.onBackPressed。

修改 也尝试删除

mMediaPlayer=null;
releaseMediaPlayer()方法中的

语句。