6

我正在尝试实现一个视频播放列表,以便它们可以从一个视频平滑过渡到下一个视频。mediaPlayer 对象有一个 prepareasync() 方法,该方法似乎可以预缓冲视频,以便可以播放。如何从 VideoView 对象调用 prepareasync 方法?我为 mediaPlayer 找到的示例要么不使用 VideoView,而且似乎是从头开始创建表面的。或者示例使用 mediaPlayer 作为 videoview 事件监听器的返回参数,这似乎只在 videoview.play() 之后发生。我想在调用 play 方法之前访问 videoview 的媒体播放器,这样我就可以调用 prepareasync(),然后再调用 play()。

4

2 回答 2

1

正如user1023110提到的,VideoView 是 MediaPlayer 的包装器。深入研究源代码(因为文档根本没有用),我确认它在其私有方法openVideo() 中内部调用了 prepareAsync()

   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;
    }
}
于 2014-02-26T23:50:06.280 回答
0

VideoView 是 MediaPlayer 和 SurfaceView 的包装器,因此您不必自己动手。我相信包装器会在您调用 setVideoURI 时进行准备。有可能通过 onPrepared 事件处理程序在 VideoView 中获取媒体播放器,一旦你得到它,我想你可以在第一次之后自己调用准备,但我认为内部是棘手的并且没有很好的记录所以事情可能不能按预期工作。

于 2013-11-10T02:59:26.790 回答