将VideoView 切换到ExoPlayer 即可。

与VideoView不同。

版本 2.18.1 下面是替换后的代码。

class CustomVideo : PlayerView {
    lateinit var player: SimpleExoPlayer
    private var mLocalMuteFlag = false // 本地静音标识
    private var mAction: PlayerUtil.OnMediaListener? = null
    private var mProgressTask: CoroutinesTask? = null // 进度任务
    private var mMediaDuration = 0L // 多媒体时长
    init {
        this.apply {
            controllerAutoShow = false
            controllerHideOnTouch = false
            controllerShowTimeoutMs = 0
//        controllerVisibilityListener = object : PlayerControlView.VisibilityListener {
//                override fun onVisibilityChange(visibility: Int) {
//                    // Do nothing
//                }
//            }
    // 设置播放事件
    fun setOnActionListener(listener: PlayerUtil.OnMediaListener?) {
        mAction = listener
    // 设置多媒体路径
    fun setMediaUri(path: String) = catchException({
        if (player.isPlaying) player.stop()
        releaseProgressTask()
        val dataSourceFactory = DefaultDataSourceFactory(context)
        val videoUri = Uri.parse(path)
        val mediaItem: MediaItem = MediaItem.fromUri(videoUri)
        val mediaSource =
            ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem)
        player.prepare(mediaSource)
    // 播放视频
    fun playVideo() = catchException({
        if (!player.isPlaying) {
            player.playWhenReady = true
            startProgressTask()
            mAction?.onMediaStart()
    // 暂停播放
    fun pauseVideo() = catchException({
        if (player.isPlaying) {
            player.playWhenReady = false
            mAction?.onMediaPause()
    // 静音
    fun updateMuteStatus(muteFlag: Boolean) {
        mLocalMuteFlag = muteFlag
        val volume = if (muteFlag) 0f else 1f
        player.volume = volume
    constructor(context: Context) : this(context, null, 0)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
        player = SimpleExoPlayer.Builder(context).build()
        player.addListener(object : Player.Listener {
            override fun onPlayerError(error: PlaybackException) {
                mAction?.onMediaErr("media err what:${error.errorCode} extra:${error.cause}")
            override fun onPlaybackStateChanged(state: Int) {
                if (state == Player.STATE_ENDED) {
                    mAction?.onMediaComplete()
        player.addAnalyticsListener(object : AnalyticsListener {
            override fun onPositionDiscontinuity(
                eventTime: AnalyticsListener.EventTime, reason: Int
                if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {
                    val position = player.currentPosition
                    val duration = player.duration
                    val progress = (position * 100f / duration).toInt()
                    mAction?.onMediaPlaying(
                        progress = progress, position = position, duration = duration
        setPlayer(player)
    // 开始进度任务
    private fun startProgressTask() {
        if (mProgressTask == null) mProgressTask = CoroutinesTask()
        if (!player.isPlaying) return
        if (mMediaDuration == 0L) mMediaDuration = (player.duration).toLong()
        val position = (player.currentPosition).toLong()
        val lastProgress = (position * 100f) / mMediaDuration
        val progress = if (mMediaDuration <= 0) 0 else (lastProgress + 0.6f).toInt()
        mAction?.onMediaPlaying(progress = progress, position = position, duration = mMediaDuration)
        mProgressTask?.launchDelayRequest(200) { startProgressTask() }
    // 释放进度任务
    private fun releaseProgressTask() {
        mProgressTask?.destroyTask()
        mProgressTask = null
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        releaseProgressTask()
        player.release()