ExoPlayer的基本使用,播本地raw,播网络,缓存, 旋转

基于版本

    api 'com.google.android.exoplayer:exoplayer:2.17.0'

xml中使用布局

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:surface_type="texture_view"
        app:resize_mode="fill"
        />

播raw文件

        val rawSource =  RawResourceDataSource(this)
        rawSource.open(DataSpec(RawResourceDataSource.buildRawResourceUri(R.raw.test)))
        val player = ExoPlayer.Builder(this).build()
        viewBinding.playerView.player = player
        player.repeatMode = Player.REPEAT_MODE_ALL
        player.setMediaItem(MediaItem.fromUri(rawSource.uri!!))
        player.prepare()
        player.play()

播网络视频

        val player = ExoPlayer.Builder(this).setMediaSourceFactory(DefaultMediaSourceFactory(VideoUtil.getCacheFactory(this))).build()
        player.repeatMode = Player.REPEAT_MODE_ALL
        viewBinding.playerView.player = player
        viewBinding.playerView.useController = false
        player.addListener(object : Player.Listener{
            override fun onPlaybackStateChanged(playbackState: Int) {
                super.onPlaybackStateChanged(playbackState)
                logE(TAG,"player State : $playbackState")
                when(playbackState){
                    Player.STATE_IDLE -> {

                    }
                    Player.STATE_READY -> {
                        viewBinding.imgLoading.Gone()
                    }
                }
            }

            override fun onPlayerError(error: PlaybackException) {
                super.onPlayerError(error)
                logE(TAG,"onPlayerError${error}")
            }

            override fun onPlayerErrorChanged(error: PlaybackException?) {
                super.onPlayerErrorChanged(error)
                logE(TAG,"onPlayerErrorChanged${error}")

            }
        })
        player.setMediaItem(MediaItem.fromUri(bean.avatarAnimatedImage))
        player.prepare()
        player.play()

缓存工厂设置

object VideoUtil {
    private var cacheFactory : DataSource.Factory? = null
    fun getCacheFactory(ctx: Context): DataSource.Factory {
        if(cacheFactory == null) {
            var downDirectory = File(ctx.filesDir, "videos")
            var cache = SimpleCache(
                downDirectory,
                LeastRecentlyUsedCacheEvictor(1024L * 1024L * 512L),
                StandaloneDatabaseProvider(ctx)
            )
            cacheFactory = CacheDataSource.Factory().setCache(cache).setUpstreamDataSourceFactory(
                        DefaultHttpDataSource.Factory())
        }
        return cacheFactory!!
    }
}

视频旋转(仅 TextureView)

        player.addListener(object : Player.Listener{
            override fun onVideoSizeChanged(videoSize: VideoSize) {
                super.onVideoSizeChanged(videoSize)

                logE("onVideoSizeChanged ${videoSize.width}  ${videoSize.height}  ; ${viewBinding.playerView.width}  ${viewBinding.playerView.height}")
                if(videoSize.width > videoSize.height){
                    var layout = viewBinding.playerView.videoSurfaceView!!.layoutParams as FrameLayout.LayoutParams
                    layout.gravity = Gravity.CENTER
                    layout.width = viewBinding.playerView.height
                    layout.height = viewBinding.playerView.width
                    viewBinding.playerView.videoSurfaceView!!.layoutParams = layout
                    viewBinding.playerView.videoSurfaceView!!.rotation = 90F
                    viewBinding
                }

            }
        })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫的于

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值