google / ExoPlayer

This project is deprecated and stale. The latest ExoPlayer code is available in https://github.com/androidx/media
https://developer.android.com/media/media3/exoplayer
Apache License 2.0
21.74k stars 6.03k forks source link

View binding custom controls #9600

Closed cherimo closed 3 years ago

cherimo commented 3 years ago

Hai, I'm using Android Exoplayer in my Android app and tried to use the view binding for the custom layout. Within Exoplayer I use a custom control layout"@layout/custom_player" for the controls. I have different elements within the layout for example I have a button element"optionBtn" which I want to connect to onclicklistener from my Kotlin code. Unfortunately that doesn't go very smoothly with view binding.

This is the XML Exoplayer

<com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/playerVIew"
        app:resize_mode="fill"
        android:animateLayoutChanges="true"
        app:controller_layout_id="@layout/custom_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

This is the kotlin code

... private var binding: FragmentVideoBinding? = null private var btnsheetOptions: SheetOptionsBinding? = null private var sheetDialog: BottomSheetDialog? = null private var customPlayer: CustomPlayerBinding? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        btnsheetOptions = SheetOptionsBinding.inflate(inflater, null, false)
        sheetDialog = BottomSheetDialog(requireContext(), R.style.BottomSheetDialogTheme)

        binding = FragmentVideoBinding.inflate(inflater, container, false)
        customPlayer = CustomPlayerBinding.inflate(inflater, binding!!.root, true)

        return binding!!.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val simpleExoPlayer = SimpleExoPlayer.Builder(requireContext()).build()
        binding!!.playerVIew.player = simpleExoPlayer
        val mediaItem = MediaItem.fromUri(video.toString())
        simpleExoPlayer.addMediaItem(mediaItem)
        simpleExoPlayer.prepare()
        simpleExoPlayer.playWhenReady = true

        customPlayer!!.optionBtn.setOnClickListener {

           ...

        }

    }

    override fun onDestroy() {
        super.onDestroy()
        binding = null
        btnsheetOptions = null
        sheetDialog= null
        customPlayer = null
    }

}

... This way the layout is double-inflated on top of each other and one layout works with onclick listener and the other does not, which is not very useful.

Does anyone know the correct solution for this, I've been working on this for almost all afternoon.

cherimo commented 3 years ago

Related topic is https://github.com/google/ExoPlayer/issues/8369 but unfortunately this doesn't seem to help with data bind either. I have a temporary workaround now with synthetic but I would like to fix this in a short term with view binding.

ojw28 commented 3 years ago

I think this is a duplicate of https://github.com/google/ExoPlayer/issues/5417, but please let us know if this is not the case. Thanks!