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


    当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等等。。。

    GitHub:https://github.com/godfunc
    博客园:http://www.cnblogs.com/godfunc
    Copyright ©2019 Godfunc
  • 相关阅读:
    Siri
    ArcSDE和Geodatabase10.1抢先版谍照介绍(3)——ArcToolbox工具和其他功能
    ‘马太’效应
    Linux琐屑下Resin JSP MySQL的布置和设置配备陈设2
    平安防御:分级防御对Linux服务器的攻击
    Linux无法解析域名的处理活动
    Ubuntu Linux Server的用户僻静后果分析
    大概要领确保确保Linux体系中SSH的安适性
    无错版Vsftpd Mysql Pam设置虚拟用户要领
    Redhat Linux AS4下的LAMP与Discuz装置1
  • 原文地址:https://www.cnblogs.com/Godfunc/p/6262280.html
Copyright © 2020-2023  润新知