YouTubePlayer
YouTube kotlin multiplatform oynatıcısı.
YouTubePlayer composable'ı, Jetpack Compose uygulamanıza bir YouTube video oynatıcısı yerleştirmenize olanak tanır.
Bağış Yapın
Bana teşekkür etmek ya da geliştirme sürecine katkıda bulunmak isterseniz, bana bağış yapabilirsiniz. Bu, projeye daha fazla odaklanmamı sağlar.Ayrıca, konularımın herhangi bir güncellemesini görmek için aşağıdaki platformlarda da abone olabilirsiniz
Kurulum
Bu kütüphaneyi projenize Gradle kullanarak ekleyebilirsiniz.Çoklu Platform Çoklu platform projesine eklemek için, bağımlılığı ortak kaynak kümesine ekleyin:
repositories {
mavenCentral()
}kotlin {
sourceSets {
commonMain {
dependencies {
implementation("io.github.ilyapavlovskii:youtubeplayer-compose:${latest_version}")
}
}
}
}
Kullanım
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
},
)
Bileşik fonksiyonun ana parametreleri şunlardır:
options- oynatıcı seçenekleri oluşturucu. Tüm parametreler resmi youtube iframe dokümantasyonu üzerinden alınmıştır.hostState- youtube oynatıcı durumunu takip eden ve tek seferlik komutları yürüten denetleyici
YouTubePlayerHostState
Ana denetleyici. 2 ana genel bileşen içerir:- currentState - ekranda mevcut youtube oynatıcı durumunu tanımlar. Şu değerleri alabilir: Idle, Ready, Playing, Error
- executeCommand - oynatıcı komutlarını yürütmek için askıya alınan fonksiyon. Sadece bir argüman alır - YouTubeExecCommand. Ayrıca şu gibi ek kolaylaştırıcı fonksiyonlara sahiptir:
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 oynatıcı durumu, ekrandaki gerçek YouTube oynatıcı durumunu tanımlar. Aşağıdaki olası durumları içerir:Idle- Boşta durumu, oynatıcının henüz başlatılmadığı anlamına gelirReady- Oynatıcının oynatmaya hazır olduğu anlamına gelirPlaying- Oynatıcı video oynatıyor. Şu parametreleri içerir:
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- Hata mesajı ile hata durumunu tanımlar.
YouTubeExecCommand
LoadVideo(val videoId: YouTubeVideoId,val startSeconds: Duration)- youtube kimliği ile videoyu yükler. Varsayılan başlangıç zamanı ile başlatmak mümkündür.Play- videoyu oynatPause- videoyu duraklatSeekTo(val duration: Duration)- videoyu belirtilen zamana sarSeekBy(val duration: Duration)- videoyu belirtilen süre kadar sarMute- sesi kapatUnmute- sesi açSetVolume(val volumePercent: Int)- ses seviyesini ayarla. Beklenen değer 0 ile 100 arasında olmalıdır.NextVideo- sonraki videoya geçPreviousVideo- önceki videoya geri dönSetLoop(val loop: Boolean)- videoyu tekrar et. Argüman ile yönetilir.SetShuffle(val shuffle: Boolean)- videoları karıştır. Argüman ile yönetilir.
YouTubeEvent
Ready- youtube oynatıcı başlatıldığında çağrılırPlaybackQualityChange(val quality: Quality)- oynatıcı kalitesi değiştiğinde çağrılırError(val error: String)- hata işleme olayı. Ayrıntılar için argümana bakınız.VideoDuration(val duration: Duration)- video süresi başlatıldığında çağrılırStateChanged(val state: State)- video durumu değiştiğinde çağrılır:UNSTARTED,ENDED,PLAYING,PAUSED,BUFFERING,CUED.TimeChanged(val time: Duration)- zaman damgası değiştiOnVideoIdHandled(val videoId: YouTubeVideoId)- video yüklendiğinde geri çağırma
Örnek

LİSANS
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
---