How to Use Media Player on Compose & Kotlin Multiplatform
Today, we’re going to learn about playing YouTube Videos, Multiple Extension Videos like .mp4, mkv e.g. We will also learn about the Audio Player as well. How can we play audios & videos on Android, iOS, Web and Desktop. So, without any further do, Let’s get into t.
Overview:
To Play Videos And Audio on Multiple platforms like Android, iOS, Web and Desktop, We are going to use the MediaPlayer-KMP Library. With the help of this library, we can achieve this in a simple way.
Setup:
To Setup, We need to add the dependencies in our build.gradle.kts
.
[versions]
mediaPlayerKMP = "1.0.8"
[libraries]
alert-kmp = { module = "io.github.khubaibkhan4:mediaplayer-kmp", version.ref = "mediaPlayerKMP" }
implementation("io.github.khubaibkhan4:mediaplayer-kmp:1.0.8")
You can use the version Catalog or simple copy paste the dependency.
Usage:
To use the Video Player, We Simply Need to Use the VideoPlayer
. To Player the YouTube Videos, We Simply need to add the YouTube URL and it will automatically detect and display the YouTube videos.
import io.github.khubaibkhan4.mediaplayer.VideoPlayer
fun main() {
VideoPlayer(modifier = Modifier.fillMaxWidth().height(340.dp),
url ="https://www.youtube.com/watch?v=AD2nEllUMJw", // Automatically Detect the URL, Wether to Play YouTube Video or .mp4 e.g
)
}
Extension Videos:
To Play Extension Videos like .mp4
, mkv
etc, You need to write the similar code to the Video Player Above, just need to change the URL.
import io.github.khubaibkhan4.mediaplayer.VideoPlayer
fun main() {
VideoPlayer(modifier = Modifier.fillMaxWidth().height(340.dp),
url ="https://freetestdata.com/wp-content/uploads/2022/02/Free_Test_Data_1MB_MP4.mp4", // Automatically Detect the URL, Wether to Play YouTube Video or .mp4 e.g
)
}
Audio Player:
Playing Audios is really simple as well. You just need to use the Media Player instead of the Video Player.
import io.github.khubaibkhan4.mediaplayer.VideoPlayer
fun main() {
MediaPlayer(
modifier = Modifier.fillMaxWidth(),
url = "https://commondatastorage.googleapis.com/codeskulptor-demos/DDR_assets/Kangaroo_MusiQue_-_The_Neverwritten_Role_Playing_Game.mp3",
startTime = Color.Black,
endTime = Color.Black,
volumeIconColor = Color.Black,
playIconColor = Color.Blue,
sliderTrackColor = Color.LightGray,
sliderIndicatorColor = Color.Blue
)
}
Demo:
Conclusion:
Media Player KMP is a really Impressive library to Play YouTube Videos, Simple Videos & Audios on Each Platform like Android, iOS, Web and Desktop. It provides the Playback Capability on each Platform.
Follow Me on Social Media:
LinkedIn: Muhammad Khubaib Imtiaz
GitHub: KhubaibKhan4