Android媒体播放器\视频问题(第一个视频在我播放第二个视频时被冻结)

时间:2013-03-19 05:58:57

标签: android android-mediaplayer

我正在研究基于Android的应用程序,它可以播放视频。当我播放任何视频它启动它然后我按后退按钮然后返回屏幕选择另一个播放(没有停止第一个视频)当我选择第二个视频时,第一个视频(已经播放)得到冻结和第二个视频开始在第一个视频后面,我可以听第二个视频的音乐等,任何人都可以帮助如何停止和从媒体播放器中删除视频,以便用户可以观看新视频 这是我的活动

public class TrailVideoActivity extends Activity implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback{ 
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyMediaPlayer");
        //setContentView(R.layout.main);

        setContentView(LayoutInflater.from(getParent()).inflate(R.layout.trial_video_player, null));


        mPreview = (SurfaceView) findViewById(R.id.surface_view);
        holder = mPreview.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }
private void playMediaPlayerVideo() {
        Log.d(TAG, "playMediaPlayerVideo called");
        doCleanUp();
        try {
            if (videoURL == "") {
                // Tell the user to provide a media file URL.
                Toast.makeText(this,"Please edit MediaPlayerDemo_Video Activity,"
                        + " and set the path variable to your media file URL.",
                        Toast.LENGTH_LONG).show();
            } 
            Log.d("TrailVideoActivity", videoURL);
            // Create a new media player and set the listeners
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(videoURL);
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            //mMediaPlayer.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);
            //mMediaPlayer.setScreenOnWhilePlaying(true);
            //mMediaPlayer.setLooping(true);
            mMediaPlayer.setAudioStreamType(2);


        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    }

    public void onBufferingUpdate(MediaPlayer arg0, int percent) {
        Log.d(TAG, "onBufferingUpdate percent:" + percent);

    }

    public void onCompletion(MediaPlayer arg0) {
        Log.d(TAG, "onCompletion called");
    }

    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        Log.v(TAG, "onVideoSizeChanged called");
        if (width == 0 || height == 0) {
            Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
            return;
        }
        mIsVideoSizeKnown = true;
        mVideoWidth = width;
        mVideoHeight = height;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");
        mIsVideoReadyToBePlayed = true;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
        Log.d(TAG, "surfaceChanged called");

    }

    public void surfaceDestroyed(SurfaceHolder surfaceholder) {
        Log.d(TAG, "surfaceDestroyed called");
    }


    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");
        playMediaPlayerVideo();

    }

    @Override
    protected void onPause() {
        Log.d(TAG, "onPause called");
        super.onPause();
        releaseMediaPlayer();
        doCleanUp();
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy called");
        super.onDestroy();
        releaseMediaPlayer();
        doCleanUp();
    }

    private void releaseMediaPlayer() {
        Log.d(TAG, "releaseMediaPlayer called");
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    private void doCleanUp() {
        Log.d(TAG, "doCleanUp called");
        mVideoWidth = 0;
        mVideoHeight = 0;
        mIsVideoReadyToBePlayed = false;
        mIsVideoSizeKnown = false;

    }

    private void startVideoPlayback() {
        Log.v(TAG, "startVideoPlayback");
        holder.setFixedSize(mVideoWidth, mVideoHeight);
        /*holder.setKeepScreenOn(false);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);*/
        mMediaPlayer.start();
        app.dismissDialog();
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

我认为您可以在MediaPlayer方法中调用release() surfaceDestroyed方法,如下所示

public void surfaceDestroyed(SurfaceHolder surfaceholder) {
    Log.d(TAG, "surfaceDestroyed called");
     if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

您也可以在MediaPlayer

中发布OnBackPressed

你能检查一下是否有效......