Search code examples
androidkotlinyoutubedummy-variablevideo-player

How to make dynamic source youtube url on dummy and show as a video


I don't know how to show a video as a dynamic source video url. This is my dummy

interface DummyMusicDataSource {
    fun getMusicData(context: Context): List<Music>
}

class DummyMusicDataSourceImpl() : DummyMusicDataSource {
    override fun getMusicData(context: Context): List<Music> {

        return mutableListOf(
            Music(
                title = "title",
                imgUrl = "https://raw.githubusercontent.com/ryhanhxx/img_asset/main/IMG_MATERI_3.jpg",
                desc = context.getString(R.string.desc_materi_1),
                videoUrl = "yTRdWD_ar78",
            )
        )
    }
}

This is my ViewHolder on my activity

    private fun showMusicData(music: Music?) {
        music?.apply {
            binding.ivImg.load(this.imgUrl) {
               crossfade(true)
            }
            binding.tvTitle.text = this.title
            binding.tvDesc.text = this.desc
            //TODO : binding id videoplayer and show as a video
        }
    }

    private fun playVideo(){
        val youTubePlayerView: YouTubePlayerView = binding.youtubePlayerView
        lifecycle.addObserver(youTubePlayerView)

        youTubePlayerView.addYouTubePlayerListener(object : AbstractYouTubePlayerListener() {
            override fun onReady(youTubePlayer: YouTubePlayer) {
                val videoId = "S0Q4gqBUs7c"
                youTubePlayer.loadVideo(videoId, 0f)
            }
        })

And this is my xml

<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
                android:id="@+id/youtube_player_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:autoPlay="false"
                app:videoId="GYZpknfi5YQ" />

I wanna make "app:videoId" as a dynamic


Solution

  • You don't need to add app:videoId="GYZpknfi5YQ" in your XML.

    There are multiple ways to make it dynamic. I am assuming that you are calling the playVideo method in the onBindViewHolder method. In that case, in the onBindViewHolder() method, you can call playVideo method with the url as the argument.

    For example:

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        music = musicList[position]
        holder.binding.layoutOfTheListItem.setOnClickListener { playVideo(music.videoUrl) }
        // replace layoutOfTheListItem with the button or layout you want the user to click to load the video
    }
    
    
    private fun playVideo(videoId: String) {
        val youTubePlayerView: YouTubePlayerView = binding.youtubePlayerView
        lifecycle.addObserver(youTubePlayerView)
    
        youTubePlayerView.addYouTubePlayerListener(
            object : AbstractYouTubePlayerListener() {
                override fun onReady(youTubePlayer: YouTubePlayer) {
                    youTubePlayer.loadVideo(videoId, 0f)
                }
            }
        )
    }