当前位置:   article > 正文

Android 播放视频之VideoView_android videoview

android videoview

1. VideoView使用

VideoView继承SurfaceView,并且实现了MediaPlayerControl接口,相当于MediaPlayer+SurfaceView的组合。关于MediaPlayer可参考这里

在布局文件里添加VideoView

<VideoView
      android:id="@+id/video_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
  • 1
  • 2
  • 3
  • 4

Activity里面播放视频

var mVideoView = findViewById(R.id.video_view)

mVideoView.setVideoURI(Uri.parse(uri))
// 设置控制器
mVideoView.setMediaController(MediaController(this))
mVideoView.start()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. VideoView的主要方法

主要方法

// 设置视频源
void setVideoPath(String path)
void setVideoURI(Uri uri)
void setVideoURI(Uri uri, Map<String, String> headers)

// 恢复,从头播放
void resume()
// 播放
void start()
// 暂停
void pause()
// 释放资源
void suspend()
// 停止播放
void stopPlayback()

// 视频时长
int getDuration()
// 当前位置
int getCurrentPosition()
// 跳转到指定时间
void seekTo(int msec)

// 是否正在播放
boolean isPlaying()
// 设置控制器
void setMediaController(MediaController controller)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

3. VideoView源码分析

VideoView里面有mCurrentStatemTargetState两个状态,当前状态和目标状态,初始值都是STATE_IDLE,播放的时候都与这两个状态有关。

VideoView里面的setVideoXXXresume方法都直接调用openVideo,在openVideo方法里面,调用MediaPlayer播放视频。
mPreparedListenerMediaPlayer准备好播放资源后,如果mSeekWhenPrepared被设置,跳转到指定时间。而如果mTargetState被设置为STATE_PLAYING,直接播放。

public void setVideoPath(String path) {
    setVideoURI(Uri.parse(path));
}

public void setVideoURI(Uri uri) {
    setVideoURI(uri, null);
}

public void setVideoURI(Uri uri, Map<String, String> headers) {
    mUri = uri;
    mHeaders = headers;
    mSeekWhenPrepared = 0;
    openVideo();
    requestLayout();
    invalidate();
}

public void resume() {
    openVideo();
}

private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;
    }

    // 清空mCurrentState,但保留mTargetState
    release(false);

    mMediaPlayer = new MediaPlayer();

    mMediaPlayer.setOnPreparedListener(mPreparedListener);
    mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
    mMediaPlayer.setOnCompletionListener(mCompletionListener);
    mMediaPlayer.setDataSource(mContext, mUri, mHeaders);
    mMediaPlayer.setDisplay(mSurfaceHolder);
    mMediaPlayer.prepareAsync();

    mCurrentState = STATE_PREPARING;
}

private void release(boolean cleartargetstate) {
    if (mMediaPlayer != null) {
        mMediaPlayer.reset();
        mMediaPlayer.release();
        mMediaPlayer = null;
        mPendingSubtitleTracks.clear();
        mCurrentState = STATE_IDLE;
        if (cleartargetstate) {
            mTargetState  = STATE_IDLE;
        }
        if (mAudioFocusType != AudioManager.AUDIOFOCUS_NONE) {
            mAudioManager.abandonAudioFocus(null);
        }
    }
}

MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {
        mCurrentState = STATE_PREPARED;

        if (mOnPreparedListener != null) {
            mOnPreparedListener.onPrepared(mMediaPlayer);
        }

        int seekToPosition = mSeekWhenPrepared;
        if (seekToPosition != 0) {
            seekTo(seekToPosition);
        }
        if (mVideoWidth != 0 && mVideoHeight != 0) {
            getHolder().setFixedSize(mVideoWidth, mVideoHeight);
            if (mSurfaceWidth == mVideoWidth && mSurfaceHeight == mVideoHeight) {
                if (mTargetState == STATE_PLAYING) {
                    start();
                } else if (!isPlaying() &&
                           (seekToPosition != 0 || getCurrentPosition() > 0)) {
                   if (mMediaController != null) {
                       mMediaController.show(0);
                   }
               }
            }
        } else {
            if (mTargetState == STATE_PLAYING) {
                start();
            }
        }
    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

VideoView里面startpause方法都是直接调用MediaPlayer的对应方法。suspendstopPlayback都是停止播放并释放资源,但stopPlayback会修改mTargetState

@Override
public void start() {
    if (isInPlaybackState()) {
        mMediaPlayer.start();
        mCurrentState = STATE_PLAYING;
    }
    mTargetState = STATE_PLAYING;
}

@Override
public void pause() {
    if (isInPlaybackState()) {
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
            mCurrentState = STATE_PAUSED;
        }
    }
    mTargetState = STATE_PAUSED;
}

public void suspend() {
    release(false);
}

public void stopPlayback() {
    if (mMediaPlayer != null) {
        mMediaPlayer.stop();
        mMediaPlayer.release();
        mMediaPlayer = null;
        mCurrentState = STATE_IDLE;
        mTargetState  = STATE_IDLE;
        mAudioManager.abandonAudioFocus(null);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

getDurationgetCurrentPositionseekTo也都是调用MediaPlayer的对应方法,只是包装了一层状态的校验,比直接调用更合理。

public int getDuration() {
    if (isInPlaybackState()) {
        return mMediaPlayer.getDuration();
    }

    return -1;
}

@Override
public int getCurrentPosition() {
    if (isInPlaybackState()) {
        return mMediaPlayer.getCurrentPosition();
    }
    return 0;
}

@Override
public void seekTo(int msec) {
    if (isInPlaybackState()) {
        mMediaPlayer.seekTo(msec);
        mSeekWhenPrepared = 0;
    } else {
        mSeekWhenPrepared = msec;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

4. MediaController控制器

MediaController控制器是VideoView默认的控制器。

public void setMediaController(MediaController controller) {
    if (mMediaController != null) {
        mMediaController.hide();
    }
    mMediaController = controller;
    attachMediaController();
}

private void attachMediaController() {
    if (mMediaPlayer != null && mMediaController != null) {
        mMediaController.setMediaPlayer(this);
        View anchorView = this.getParent() instanceof View ?
                (View)this.getParent() : this;
        mMediaController.setAnchorView(anchorView);
        mMediaController.setEnabled(isInPlaybackState());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

MediaController创建界面,一般包含3个按钮,从左往右分别是倒退、播放/暂停和快进。底部从左到右是当前时间、进度条和总播放时间。拖动进度条可以跳转到指定时间。

public void setMediaPlayer(MediaPlayerControl player) {
    mPlayer = player;
    updatePausePlay();
}

public void setAnchorView(View view) {
    if (mAnchor != null) {
        mAnchor.removeOnLayoutChangeListener(mLayoutChangeListener);
    }
    mAnchor = view;
    if (mAnchor != null) {
        mAnchor.addOnLayoutChangeListener(mLayoutChangeListener);
    }

    FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
    );

    removeAllViews();
    View v = makeControllerView();
    addView(v, frameParams);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

调用setPrevNextListeners 方法后,根据nextprev的值,在两边添加按钮。

public void setPrevNextListeners(View.OnClickListener next, View.OnClickListener prev)
  • 1

显示如下
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/499348
推荐阅读
相关标签
  

闽ICP备14008679号