How to implement ExoPlayer with Databinding?

一曲冷凌霜 提交于 2021-02-09 15:43:31

问题


I have a class activity_player layout in which I have exoplayer2.ui.PlayerView and I created exo_player_control_view so that it overrides default controls in ExoPlayer. So I wanted to use Databinding in newly created custom control view but don't know how to do it. Any advice?

It is actually an open issue over here, but yet to be solved. So is there anyone who had a workaround to make exo_player_control_view Databinding friendly?


回答1:


You can use binding variable inside fragment/activity to access the playerView inside fragment/activity and

  val uri: Uri? = if (url is String) Uri.parse(url as String?) else url as Uri?
    val trackSelector =
        DefaultTrackSelector(AdaptiveTrackSelection.Factory(DefaultBandwidthMeter()))
    val player: SimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(view.context, trackSelector)
    val dataSourceFactory = DefaultDataSourceFactory(view.context, "ua")
    val mediaSource =
        ExtractorMediaSource(uri, dataSourceFactory, DefaultExtractorsFactory(), null, null)
    player.prepare(mediaSource)
    player.apply {
        volume = 0f
        repeatMode = Player.REPEAT_MODE_ONE
        playWhenReady = true
        videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT
    }
    binding.playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL)
    binding.playerView.player = player


来源:https://stackoverflow.com/questions/54739354/how-to-implement-exoplayer-with-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!