其他分享
首页 > 其他分享> > ExoPlayer记录学习

ExoPlayer记录学习

作者:互联网

https://exoplayer.dev/ui-components.html 这篇对我的帮助很大,感谢感谢
implementation 'com.google.android.exoplayer:exoplayer:2.x.x
自定义的控制按钮布局及添加了等待圈

 <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/video_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ProgressBar
            android:id="@+id/prob"
            android:layout_width="30dp"
            android:layout_height="30dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

最重要自定义进度条需要重写一个布局放在xml下,控制按钮Id名字保持一致即可。
exo_play
exo_pause
exo_position
exo_duration
exo_progress

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="212dp">

    <ImageView
        android:id="@id/exo_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        tools:ignore="ContentDescription" />

    <ImageView
        android:id="@id/exo_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        tools:ignore="ContentDescription" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="20dp"
        tools:ignore="MissingConstraints">

        <TextView
            android:id="@+id/exo_position"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            app:layout_constraintLeft_toLeftOf="parent" />

        <TextView
            android:id="@+id/exo_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            app:layout_constraintRight_toRightOf="parent" />

        <com.google.android.exoplayer2.ui.DefaultTimeBar
            android:id="@id/exo_progress"
            android:layout_width="0dp"
            android:layout_height="20dp"
            android:layout_weight="1"
            app:buffered_color="@android:color/darker_gray"
            app:layout_constraintBottom_toBottomOf="@id/exo_position"
            app:layout_constraintLeft_toRightOf="@id/exo_position"
            app:layout_constraintRight_toLeftOf="@id/exo_duration"
            app:layout_constraintTop_toTopOf="@id/exo_position"
            app:played_color=""
            app:unplayed_color="" />
    </androidx.constraintlayout.widget.ConstraintLayout>


</FrameLayout>

写好布局之后,已经完成一半啦

        //创建播放实例
        player = ExoPlayerFactory.newSimpleInstance(requireActivity())
        //播放实例赋值给UI。
        mVideoView?.player = player
        //数据源
        val dataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(requireActivity(), ""))
        videoSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(“视频地址”))
                player.prepare(videoSource)
            }

        }
        //缓存好自动播放
        player.playWhenReady = true
        //监听播放状态以及失败原因
        player.addListener(object : Player.EventListener {
            override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
                when (playbackState) {
                    ExoPlayer.STATE_ENDED -> {
                        //播放器完成了播放

                    }
                    ExoPlayer.STATE_READY -> {
                        mProb.visibility = View.GONE
                        // 播放器可以立即从当前位置开始播放。
                        //当点击暂停或者播放时都会调用此方法
                        //当跳转进度时,进度加载完成后调用此方法
                    }
                    ExoPlayer.STATE_BUFFERING -> { //播放器无法立即从当前位置开始播放。这种状态通常需要加载更多数据时发生。
                        mProb.visibility = View.VISIBLE
                    }
                }
            }

            override fun onPlayerError(error: ExoPlaybackException?) {
                super.onPlayerError(error)
                showToast("播放异常")
            }

        })

    }
    override fun onPause() {
        super.onPause()
        player.setPlayWhenReady(false)
    }

    override fun onResume() {
        super.onResume()
        player.setPlayWhenReady(true)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        instance = null
        if (player != null) {
            player.stop()
            player.release()
        }
    }

标签:fun,记录,exo,学习,player,ExoPlayer,override,播放,super
来源: https://blog.csdn.net/m0_51282960/article/details/120866962