YouTubePlayer
YouTube kotlin multiplatform player.
Composable YouTubePlayer memungkinkan Anda menyematkan pemutar video YouTube di aplikasi Jetpack Compose Anda.
Donasi
Jika Anda ingin mengucapkan terima kasih atau berkontribusi pada pengembangan backlog, Anda dapat berdonasi kepada saya. Itu membantu saya untuk lebih fokus pada proyek ini.Anda juga dapat mengikuti saya di platform berikut untuk melihat pembaruan topik saya
Instalasi
Anda dapat menambahkan pustaka ini ke proyek Anda menggunakan Gradle.Multiplatform Untuk menambahkannya ke proyek multiplatform, tambahkan dependency ke common source-set:
repositories {
mavenCentral()
}kotlin {
sourceSets {
commonMain {
dependencies {
implementation("io.github.ilyapavlovskii:youtubeplayer-compose:${latest_version}")
}
}
}
}
Penggunaan
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
},
)
Fungsi komposabel memiliki parameter utama berikut:
options- untuk membangun opsi pemutar. Semua parameter dibungkus dari dokumentasi resmi youtube iframe.hostState- pengontrol untuk melacak status pemutar youtube dan mengeksekusi perintah satu kali
YouTubePlayerHostState
Pengontrol utama. Berisi 2 komponen publik utama:- currentState - mendefinisikan status aktual pemutar youtube di layar. Bisa berupa: Idle, Ready, Playing, Error
- executeCommand - fungsi suspend untuk mengeksekusi perintah pemutar. Menerima hanya satu argumen - YouTubeExecCommand. Juga memiliki fungsi tambahan seperti:
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
Status pemutar YouTube mendefinisikan status aktual pemutar YouTube di layar. Berisi status-status berikut yang mungkin:Idle- Status Idle berarti pemutar belum diinisialisasiReady- Berarti pemutar siap untuk memutarPlaying- Pemutar sedang memutar video. Berisi parameter berikut:
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
Error- Mendefinisikan status error dengan pesan error di dalamnya.
YouTubeExecCommand
LoadVideo(val videoId: YouTubeVideoId,val startSeconds: Duration)- memuat video berdasarkan youtube id. Dapat dimulai dengan offset waktu awal default.Play- memutar videoPause- menjeda videoSeekTo(val duration: Duration)- menuju waktu tertentu dalam videoSeekBy(val duration: Duration)- menuju waktu dalam video berdasarkan durasi tertentuMute- membisukan suaraUnmute- mengaktifkan suara kembaliSetVolume(val volumePercent: Int)- mengatur volume. Nilai argumen yang diharapkan dari 0 sampai 100.NextVideo- navigasi ke video berikutnyaPreviousVideo- navigasi ke video sebelumnyaSetLoop(val loop: Boolean)- mengulang video. Diatur oleh argumen.SetShuffle(val shuffle: Boolean)- mengacak video. Diatur oleh argumen.
YouTubeEvent
Ready- dipanggil ketika inisialisasi pemutar youtube selesaiPlaybackQualityChange(val quality: Quality)- dipanggil ketika kualitas pemutar berubahError(val error: String)- event penanganan error. Lihat argumen untuk detail.VideoDuration(val duration: Duration)- dipanggil ketika durasi video terinisialisasiStateChanged(val state: State)- dipanggil ketika status video berubah:UNSTARTED,ENDED,PLAYING,PAUSED,BUFFERING,CUED.TimeChanged(val time: Duration)- timestamp berubahOnVideoIdHandled(val videoId: YouTubeVideoId)- callback ketika video dimuat
Contoh

LISENSI
Copyright 2026 Ilia PavlovskiiLicensed 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-06-12
---