调用MediaPlayer的。prepareAsync从VideoView [英] Calling MediaPlayer.PrepareAsync from VideoView

查看:203
本文介绍了调用MediaPlayer的。prepareAsync从VideoView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现视频的播放列表,使他们有一个平稳过渡,从一个视频到下一个。在MediaPlayer对象有一个prepareasync()方法似乎是$ P $的pbuffer的视频所以它已经准备好打。如何调用从VideoView对象prepareasync方法?我发现媒体播放器或者不使用VideoView的例子似乎从头开始创建面。或者例子使用媒体播放器的返回参数上videoview EventListeners的,这似乎只发生在videoview.play后()。我想有调用play方法,所以我可以调用prepareasync(之前访问videoview的媒体播放器),然后后来的play()。

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().

推荐答案

正如 user1023110 提到,VideoView是一个包装周围的MediaPlayer。潜水到源$ C ​​$ C(因为文档是没有作用的),我证实,它内部调用prepareAsync()在其私有方法的 openVideo()

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;
    }
}

这篇关于调用MediaPlayer的。prepareAsync从VideoView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆