我正在尝试实施视频播放列表,以便从一个视频到下一个视频顺利过渡。 mediaPlayer对象有一个prepareasync()方法,似乎预先缓冲视频,以便它可以播放。如何从VideoView对象调用prepareasync方法?我找到的mediaPlayer示例要么不使用VideoView,要么从头开始创建表面。或者这些示例使用mediaPlayer作为videoview eventlisteners的返回参数,这些参数似乎只发生在videoview.play()之后。我想在调用play方法之前访问videoview的mediaplayer,这样我就可以调用prepareasync(),然后调用play()。
答案 0 :(得分:1)
就像user1023110提到的那样,VideoView是MediaPlayer的包装器。深入了解源代码(因为文档根本没用)我确认它在私有方法内部调用prepareAsync() 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;
}
}
答案 1 :(得分:0)
VideoView是MediaPlayer和SurfaceView的包装器,因此您无需自己动手。我相信当你调用setVideoURI时,包装器会做好准备。 可以通过onPrepared事件处理程序获取VideoView中的媒体播放器,一旦你得到它,我想你可以在第一次之后调用自己做好准备,但是我认为内部结构很棘手并没有很好的记录,所以事情可能会没有按预期工作。