当setVideoPath()的时候,底层是这个走的:
setVideoPath()-->setVideoURI()-->openVideo();
openVideo()只是setVideoURI()中的一部分,还有的没有写出来,下面看一下openVideo的具体内容:
1 private void openVideo() { 2 if (mUri == null || mSurfaceHolder == null) { 3 // not ready for playback just yet, will try again later 4 return; 5 } 6 // we shouldn‘t clear the target state, because somebody might have 7 // called start() previously 8 release(false); 9 10 AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 11 am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); 12 13 try { 14 mMediaPlayer = new MediaPlayer(); 15 // TODO: create SubtitleController in MediaPlayer, but we need 16 // a context for the subtitle renderers 17 final Context context = getContext(); 18 final SubtitleController controller = new SubtitleController( 19 context, mMediaPlayer.getMediaTimeProvider(), mMediaPlayer); 20 controller.registerRenderer(new WebVttRenderer(context)); 21 controller.registerRenderer(new TtmlRenderer(context)); 22 controller.registerRenderer(new Cea708CaptionRenderer(context)); 23 controller.registerRenderer(new ClosedCaptionRenderer(context)); 24 mMediaPlayer.setSubtitleAnchor(controller, this); 25 26 if (mAudioSession != 0) { 27 mMediaPlayer.setAudioSessionId(mAudioSession); 28 } else { 29 mAudioSession = mMediaPlayer.getAudioSessionId(); 30 } 31 mMediaPlayer.setOnPreparedListener(mPreparedListener); 32 mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); 33 mMediaPlayer.setOnCompletionListener(mCompletionListener); 34 mMediaPlayer.setOnErrorListener(mErrorListener); 35 mMediaPlayer.setOnInfoListener(mInfoListener); 36 mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener); 37 mCurrentBufferPercentage = 0; 38 mMediaPlayer.setDataSource(mContext, mUri, mHeaders); 39 mMediaPlayer.setDisplay(mSurfaceHolder); 40 mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 41 mMediaPlayer.setScreenOnWhilePlaying(true); 42 mMediaPlayer.prepareAsync(); 43 44 for (Pair<InputStream, MediaFormat> pending: mPendingSubtitleTracks) { 45 try { 46 mMediaPlayer.addSubtitleSource(pending.first, pending.second); 47 } catch (IllegalStateException e) { 48 mInfoListener.onInfo( 49 mMediaPlayer, 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. 55 mCurrentState = STATE_PREPARING; 56 attachMediaController(); 57 } catch (IOException ex) { 58 Log.w(TAG, "Unable to open content: " + mUri, ex); 59 mCurrentState = STATE_ERROR; 60 mTargetState = STATE_ERROR; 61 mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); 62 return; 63 } catch (IllegalArgumentException ex) { 64 Log.w(TAG, "Unable to open content: " + mUri, ex); 65 mCurrentState = STATE_ERROR; 66 mTargetState = STATE_ERROR; 67 mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); 68 return; 69 } finally { 70 mPendingSubtitleTracks.clear(); 71 } 72 }
从31行看,可以知道,执行setVideoPath()会重新执行VideoView的监听事件,会重新prepared等等。。。
时间: 2024-10-08 17:04:04