0

私はビデオを再生するAndroidベースのアプリケーションに取り組んでいます。ビデオを再生すると起動し、戻るボタンを押して戻る画面に移動し、(最初のビデオを停止せずに)再生する別のビデオを選択します。2番目のビデオを選択すると、最初のビデオ(すでに再生されていた)が取得されます。凍結された2番目のビデオはこの最初のビデオの後ろで開始されます、私は2番目のビデオの音楽などを聞くことができます、ユーザーが新しいビデオを見ることができるようにメディアプレーヤーからビデオを停止して削除する方法を誰かが助けることができますこれが私の活動です

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 に答える