0

我正在开发一个基于 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();
    }
}

提前致谢。

4

1 回答 1

0

我想你也可以在你MediaPlayer release()的方法中调用方法,surfaceDestroyed如下所示

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

MediaPlayer你也可以发布OnBackPressed

你可以检查这是否有效...

于 2013-03-26T12:10:41.070 回答