IlyaPavlovskii / YouTubePlayer

YouTube kotlin multiplatform player
Apache License 2.0
48 stars 0 forks source link

YouTubePlayer

Maven Central Kotlin Compose Multiplatform

badge-android badge-ios

YouTube kotlin multiplatform player. The YouTubePlayer composable allows you to embed a YouTube video player in your Jetpack Compose app.

Donate

If you want to thank me or contribute to the development of the backlog, you may donate me. That helps me to concentrate more on the project.

"PayPal" "Buy Me A Coffee"

You also might subscribe me on the next platforms to see any updates of my topics

medium habr

Install

You can add this library to your project using Gradle.

Multiplatform To add to a multiplatform project, add the dependency to the common source-set:

repositories {
    mavenCentral()
}

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation("io.github.ilyapavlovskii:youtubeplayer-compose:${latest_version}")
            }
        }
    }
}

Usage

val coroutineScope = rememberCoroutineScope()
val hostState = remember { YouTubePlayerHostState() }

when(val state = hostState.currentState) {
    is YouTubePlayerState.Error -> {
        Text(text = "Error: ${state.message}")
    }
    YouTubePlayerState.Idle -> {
        // Do nothing, waiting for initialization
    }
    is YouTubePlayerState.Playing -> {
        // Update UI button states
    }
    YouTubePlayerState.Ready -> coroutineScope.launch {
        hostState.loadVideo(YouTubeVideoId("ufKj1sBrC4Q"))
    }
}

YouTubePlayer(
    modifier = Modifier
        .fillMaxWidth()
        .height(300.dp)
        .gesturesDisabled(),
    hostState = hostState,
    options = SimpleYouTubePlayerOptionsBuilder.builder {
        autoplay(true)
        controls(false)
        rel(false)
        ivLoadPolicy(false)
        ccLoadPolicy(false)
        fullscreen = true
    },
)

Composable function has the next major params:

YouTubePlayerHostState

The main controller. Contains 2 major public components:

YouTubePlayerState

YouTube player state defines actual youtube player state on the screen. Contains the next possible states:

YouTubeExecCommand

YouTubeEvent

Sample

Sample

LICENSE

Copyright 2024 Ilia Pavlovskii

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.