Android开发之视频播放调用setVideoPath()方法

历览千载书,时时见遗烈。这篇文章主要讲述Android开发之视频播放调用setVideoPath()方法相关的知识,希望能为你提供帮助。
【Android开发之视频播放调用setVideoPath()方法】当setVideoPath()的时候,底层是这个走的:
setVideoPath()--> setVideoURI()--> openVideo();
openVideo()只是setVideoURI()中的一部分,还有的没有写出来,下面看一下openVideo的具体内容:

1 private void openVideo() { 2if (mUri == null || mSurfaceHolder == null) { 3// not ready for playback just yet, will try again later 4return; 5} 6// we shouldn‘t clear the target state, because somebody might have 7// called start() previously 8release(false); 9 10AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 11am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); 12 13try { 14mMediaPlayer = new MediaPlayer(); 15// TODO: create SubtitleController in MediaPlayer, but we need 16// a context for the subtitle renderers 17final Context context = getContext(); 18final SubtitleController controller = new SubtitleController( 19context, mMediaPlayer.getMediaTimeProvider(), mMediaPlayer); 20controller.registerRenderer(new WebVttRenderer(context)); 21controller.registerRenderer(new TtmlRenderer(context)); 22controller.registerRenderer(new Cea708CaptionRenderer(context)); 23controller.registerRenderer(new ClosedCaptionRenderer(context)); 24mMediaPlayer.setSubtitleAnchor(controller, this); 25 26if (mAudiosession != 0) { 27mMediaPlayer.setAudioSessionId(mAudioSession); 28} else { 29mAudioSession = mMediaPlayer.getAudioSessionId(); 30} 31mMediaPlayer.setOnPreparedListener(mPreparedListener); 32mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); 33mMediaPlayer.setOnCompletionListener(mCompletionListener); 34mMediaPlayer.setOnErrorListener(mErrorListener); 35mMediaPlayer.setOnInfoListener(mInfoListener); 36mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener); 37mCurrentBufferPercentage = 0; 38mMediaPlayer.setDataSource(mContext, mUri, mHeaders); 39mMediaPlayer.setDisplay(mSurfaceHolder); 40mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 41mMediaPlayer.setScreenOnWhilePlaying(true); 42mMediaPlayer.prepareAsync(); 43 44for (Pair< InputStream, MediaFormat> pending: mPendingSubtitleTracks) { 45try { 46mMediaPlayer.addSubtitleSource(pending.first, pending.second); 47} catch (IllegalStateException e) { 48mInfoListener.onInfo( 49mMediaPlayer, MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE, 0); 50} 51} 52 53// we don‘t set the target state here either, but preserve the 54// target state that was there before. 55mCurrentState = STATE_PREPARING; 56attachMediaController(); 57} catch (IOException ex) { 58Log.w(TAG, "Unable to open content: " + mUri, ex); 59mCurrentState = STATE_ERROR; 60mTargetState = STATE_ERROR; 61mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); 62return; 63} catch (IllegalArgumentException ex) { 64Log.w(TAG, "Unable to open content: " + mUri, ex); 65mCurrentState = STATE_ERROR; 66mTargetState = STATE_ERROR; 67mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); 68return; 69} finally { 70mPendingSubtitleTracks.clear(); 71} 72}

从31行看,可以知道,执行setVideoPath()会重新执行VideoView的监听事件,会重新prepared等等。。。

    推荐阅读