Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling MediaPlayer.PrepareAsync from VideoView

I am trying to implement a playlist of videos so they have a smooth transition from one video to the next. The mediaPlayer object has a prepareasync() method that would seem to prebuffer the video so it's ready to play. How do I invoke the prepareasync method from the VideoView object? The examples I found for mediaPlayer either don't use VideoView and seem to create the surface from scratch. Or the examples use mediaPlayer as return parameter on videoview eventlisteners which seem to occur only after the videoview.play(). I would like have access to videoview's mediaplayer before invoking the play method so I can invoke the prepareasync() and then later the the play().

like image 217
user2743991 Avatar asked Sep 03 '13 18:09

user2743991


1 Answers

Just as user1023110 mentioned, VideoView is a wrapper around MediaPlayer. Diving into the source code (since the docs aren't useful at all) I confirmed that it internally calls prepareAsync() in its private method openVideo():

   private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;
    }
    // Tell the music playback service to pause
    // TODO: these constants need to be published somewhere in the framework.
    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "pause");
    mContext.sendBroadcast(i);

    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);
    try {
        mMediaPlayer = new MediaPlayer();
        if (mAudioSession != 0) {
            mMediaPlayer.setAudioSessionId(mAudioSession);
        } else {
            mAudioSession = mMediaPlayer.getAudioSessionId();
        }
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mOnInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mMediaPlayer.setDataSource(mContext, mUri, mHeaders);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();
        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } catch (IllegalArgumentException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    }
}
like image 119
saiyancoder Avatar answered Oct 29 '22 02:10

saiyancoder