Skip to content

3 VideoView

WenchangMai edited this page Jun 7, 2020 · 3 revisions

VideoView

VideoView的作用是承载视图,我们设计成一个FramLayout,主要持有一个SurfaceView和一个MediaPlayer。它实现了LifecycleObserver,监听Activity的onPause()onDestory()方法,在onPause()的时候会暂停播放,在onDestory的时候会停止播放并且释放播放器。

IVideoView

VideoView的抽象,可以在这里看到VideoView的主要成员变量和函数。

interface IVideoView : TextureView.SurfaceTextureListener, LifecycleObserver {

    /**
     * 封面
     */
    val cover: ImageView

    /**
     * 播放器内核
     */
    var mediaPlayer: IMediaPlayer<*>?

    /**
     * 是否正在播放
     */
    val isPlaying: Boolean

    /**
     * 当前位置
     */
    val currentPosition: Long

    /**
     * 视频时长
     */
    val duration: Long

    /**
     * 视频高度
     */
    val videoHeight: Int

    /**
     * 视频宽度
     */
    val videoWidth: Int

    /**
     * 音频管理器
     */
    var audioManager : IAudioManager

    /**
     * 播放器状态
     */
    val playerState: PlayerState

    /**
     * 初始化播放
     */
    fun prepare()

    /**
     * 播放
     */
    fun start()

    /**
     * 重播
     */
    fun replay()

    /**
     * 暂停
     */
    fun pause()

    /**
     * 停止
     */
    fun stop()

    /**
     * 释放资源
     */
    fun release()

    /**
     * 重置
     */
    fun reset()

    /**
     * 跳转
     */
    fun seekTo(time: Long)

    /**
     * 设置音量
     *
     */
    fun setVolume(volume: Int)

    /**
     * 绑定视图
     */
    fun attach()

    /**
     * 获取当前视图Bitmap
     */
    fun getBitmap(): Bitmap?
}

状态保护

根据官网上的这个表:地址

Method Name Valid States Invalid States Comments
getCurrentPosition {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.
getDuration {Prepared, Started, Paused, Stopped, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.
getVideoHeight {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.
getVideoWidth {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.
isPlaying {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.
pause {Started, Paused, PlaybackCompleted} {Idle, Initialized, Prepared, Stopped, Error} Successful invoke of this method in a valid state transfers the object to the Paused state. Calling this method in an invalid state transfers the object to the Error state.
prepare {Initialized, Stopped} {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Prepared state. Calling this method in an invalid state throws an IllegalStateException.
prepareAsync {Initialized, Stopped} {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Preparing state. Calling this method in an invalid state throws an IllegalStateException.
release any {} After release(), the object is no longer available.
reset {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} {} After reset(), the object is like being just created.
seekTo {Prepared, Started, Paused, PlaybackCompleted} {Idle, Initialized, Stopped, Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.
setDataSource {Idle} {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Initialized state. Calling this method in an invalid state throws an IllegalStateException.
setLooping {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.
setVolume {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} {Error} Successful invoke of this method does not change the state.
start {Prepared, Started, Paused, PlaybackCompleted} {Idle, Initialized, Stopped, Error} Successful invoke of this method in a valid state transfers the object to the Started state. Calling this method in an invalid state transfers the object to the Error state.
stop {Prepared, Started, Stopped, Paused, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method in a valid state transfers the object to the Stopped state. Calling this method in an invalid state transfers the object to the Error state.

上表说明了各个播放方法在应该在哪些播放状态调用,如果在错误的播放状态调用就会报错。于是,在VideoView内部,我们在各个播放方法调用时做了状态检查,以前减少播放器报错的概率,起到一个状态保护的作用。

Clone this wiki locally