当前位置:   article > 正文

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

exoplayer播放本地视频

基于版本

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

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"
        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

播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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

播网络视频

        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()
  • 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

缓存工厂设置

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!!
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

视频旋转(仅 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
                }

            }
        })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/299313
推荐阅读
相关标签
  

闽ICP备14008679号