Web Analytics

YouTubePlayer

⭐ 90 stars Simplified Chinese by IlyaPavlovskii

YouTubePlayer

Maven Central Kotlin Compose Multiplatform

badge-android badge-ios

YouTube kotlin 多平台播放器。 YouTubePlayer 组合式组件允许你在 Jetpack Compose 应用中嵌入一个 YouTube 视频播放器。

捐赠

如果你想感谢我或为项目开发做出贡献,可以捐赠给我。这有助于我更专注于项目。

你也可以在以下平台关注我,以获取我主题的最新动态

medium habr

安装

你可以使用 Gradle 将此库添加到你的项目中。

多平台 要添加到多平台项目中,请将依赖项添加到 common 源代码集:

repositories {
    mavenCentral()
}

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

用法

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) mute(true) // autoplay works only with mute for mobile devices controls(false) rel(false) ivLoadPolicy(false) ccLoadPolicy(false) fullscreen = true }, )

可组合函数具有以下主要参数:

YouTubePlayerHostState

主要控制器。包含两个主要公共组件:
suspend fun loadVideo(videoId: YouTubeVideoId) = executeCommand(YouTubeExecCommand.LoadVideo(videoId))
suspend fun play() = executeCommand(YouTubeExecCommand.Play)
suspend fun pause() = executeCommand(YouTubeExecCommand.Pause)
suspend fun seekTo(duration: Duration) = executeCommand(YouTubeExecCommand.SeekTo(duration))
suspend fun seekBy(duration: Duration) = executeCommand(YouTubeExecCommand.SeekBy(duration))
suspend fun mute() = executeCommand(YouTubeExecCommand.Mute)
suspend fun unMute() = executeCommand(YouTubeExecCommand.Unmute)
suspend fun setVolume(volume: Int) = executeCommand(YouTubeExecCommand.SetVolume(volume))
suspend fun setPlaybackRate(rate: Float) = executeCommand(YouTubeExecCommand.SetPlaybackRate(rate))
suspend fun toggleFullScreen() = executeCommand(YouTubeExecCommand.ToggleFullscreen)

YouTubePlayerState

YouTube播放器状态定义屏幕上实际的YouTube播放器状态。包含以下可能的状态:
videoId: YouTubeVideoId - id of the video that is playing
duration: Duration - duration of the video
currentTime: Duration - current time of the video
quality: YouTubeEvent.PlaybackQualityChange.Quality - quality of the video, see [YouTubeEvent.PlaybackQualityChange.Quality]
isPlaying: Boolean - is video playing

YouTubeExecCommand

YouTubeEvent

Sample

Sample

LICENSE

Copyright 2026 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.

--- Tranlated By Open Ai Tx | Last indexed: 2026-05-30 ---