gpt4 book ai didi

android - 从 VideoView 调用 MediaPlayer.PrepareAsync

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:05 26 4
gpt4 key购买 nike

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

最佳答案

正如user1023110提到,VideoView 是 MediaPlayer 的包装器。深入研究源代码(因为文档根本没有用)我确认它在其私有(private)方法 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;
}
}

关于android - 从 VideoView 调用 MediaPlayer.PrepareAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18598675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com