androidkotlinandroid-jetpack-composeyoutubeplayer

Looking for a Jetpack Compose YouTube Video Player wrapper dependency


Intro

I'm new to Jetpack Compose and it is not that easy for me to understand or get everything correct on my first try.

That's why I love to have a look at other's open-source work to understand the topic a little bit better.

Problem

My current problem is that I must embed YouTube videos into my app. At first, I thought I use an embedded web view, but hey, I'm a native app dev, let's do it native!

But I stumbled a lot

  1. Is it true, that we as devs must use a local *.jar file from Google to embed the video player?
  2. Is it true, that there is no official Jetpack Compose component for that?
  3. Is it true, that there is no dependency our there which provides such functionality?
  4. Yes, I searched SO before I asked

That's why it would be awesome if someone of you could point me into the correct direction to make any progress with my tiny self-learning app.


Solution

  • the simplest way is first just add this line in gradle file

    "implementation" ("com.pierfrancescosoffritti.androidyoutubeplayer:core:11.1.0")
    
    @Composable
    fun YoutubeScreen(
        videoId: String,
        modifier: Modifier
    ) {
        val ctx = LocalContext.current
        AndroidView(factory = {
            var view = YouTubePlayerView(it)
            val fragment = view.addYouTubePlayerListener(
                object : AbstractYouTubePlayerListener() {
                    override fun onReady(youTubePlayer: YouTubePlayer) {
                        super.onReady(youTubePlayer)
                        youTubePlayer.loadVideo(videoId, 0f)
                    }
                }
            )
            view
        })
    }