'Android TV Replacing ImageCardView with SimpleExoPlayerView

I am developing and Android TV App. What I want to achieve is when user focus on a Card, the stream video start to play on the ImageCardView Area, replacing the Image with SimpleExoPlayerView. The code I worked so far only be able to play the sound of the stream but no video could be played. Here is my Presenter Class. I guess the problem is that I dont know how to make the SimpleExoPlayerView to replace the ImageCardView

 override fun onCreateViewHolder(parent: ViewGroup): Presenter.ViewHolder {
    Log.d(TAG, "onCreateViewHolder")
    mContext = parent.context
    sDefaultBackgroundColor = ContextCompat.getColor(mContext, R.color.default_background)
    sSelectedBackgroundColor = ContextCompat.getColor(
        mContext,
        R.color.selected_background
    )
    mDefaultCardImage = ContextCompat.getDrawable(mContext, R.drawable.banner)

    var player: SimpleExoPlayer
    var playerView: SimpleExoPlayerView
    var dataSourceFactory: DefaultDataSourceFactory
    var hlsMediaSourceFactory: HlsMediaSource.Factory
    var ProgressiveMediaSourceFactory : ProgressiveMediaSource.Factory
    var bandwithMeter = DefaultBandwidthMeter()
    var videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwithMeter)
    var trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)

    player = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector)

    val inflater = LayoutInflater.from(mContext)
    var container:ViewGroup?=parent

    var videoview: View = inflater.inflate(
    R.layout.exoplayer, container,
    false
    )

    var cardView = object : ImageCardView(mContext) {
        override fun setSelected(selected: Boolean) {
            updateCardBackgroundColor(this, selected)
            super.setSelected(selected)
        }
    }
    cardView.isFocusable = true
    cardView.isFocusableInTouchMode = true
    cardView.setOnFocusChangeListener { view, isFocused ->

        if (isFocused) {
            player.playWhenReady = true
            playerView =videoview.findViewById(R.id.player_view)
            playerView.requestFocus()
            playerView.player = player
            playerView.hideController()
            dataSourceFactory = DefaultDataSourceFactory(mContext, "exoplayer", bandwithMeter)
            hlsMediaSourceFactory = HlsMediaSource.Factory(
                dataSourceFactory
            )
            ProgressiveMediaSourceFactory = ProgressiveMediaSource.Factory(
                dataSourceFactory
            )
            val mediaUri = Uri.parse("http://www.somethingstreaming.m3u8")
            val mediaSource = hlsMediaSourceFactory.createMediaSource(mediaUri)
            player.prepare(mediaSource)

        } else {
                player.release()
        }
    }

    updateCardBackgroundColor(cardView, false)


    return Presenter.ViewHolder(cardView)
}

override fun onBindViewHolder(viewHolder: Presenter.ViewHolder, item: Any) {
    val movie = item as Movie
    val cardView = viewHolder.view as ImageCardView

    Log.d(TAG, "onBindViewHolder")
    if (movie.cardImageUrl != null) {
        cardView.titleText = movie.title
        cardView.contentText = movie.description



        cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT)

        Glide.with(viewHolder.view.context)
            .load(movie.cardImageUrl)
            .centerCrop()
            .error(mDefaultCardImage)
            .into(cardView.mainImageView)

    }


}

override fun onUnbindViewHolder(viewHolder: Presenter.ViewHolder) {
    Log.d(TAG, "onUnbindViewHolder")
    val cardView = viewHolder.view as ImageCardView
    // Remove references to images so that the garbage collector can free up memory
    cardView.badgeImage = null
    cardView.mainImage = null
}

private fun updateCardBackgroundColor(view: ImageCardView, selected: Boolean) {
    val color = if (selected) sSelectedBackgroundColor else sDefaultBackgroundColor
    // Both background colors should be set because the view's background is temporarily visible
    // during animations.
    view.setBackgroundColor(color)
    view.setInfoAreaBackgroundColor(color)
}

companion object {
    private val TAG = "CardPresenter"
    private val CARD_WIDTH = 285
    private val CARD_HEIGHT = 130
}
}

and this is my exoplayer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:clickable="true"

        android:focusable="true"/>

My CardPresenter Class is modified from https://github.com/android/tv-samples/blob/main/ClassicsKotlin/app/src/main/java/com/android/tv/classics/presenters/TvMediaMetadataPresenter.kt



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source