Created
March 16, 2024 19:33
-
-
Save ShivamKumarJha/50005ea09a7b50b30189d18af2f64f76 to your computer and use it in GitHub Desktop.
VLC Android player in compose with Subtitles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.shivamkumarjha.animov.ui.player | |
import android.net.Uri | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.LaunchedEffect | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.platform.LocalContext | |
import androidx.compose.ui.viewinterop.AndroidView | |
import org.videolan.libvlc.LibVLC | |
import org.videolan.libvlc.Media | |
import org.videolan.libvlc.MediaPlayer | |
import org.videolan.libvlc.interfaces.IMedia | |
import org.videolan.libvlc.util.VLCVideoLayout | |
@Composable | |
fun VLCPlayer( | |
modifier: Modifier, | |
videoUrl: String, | |
subtitleUrl: String?, | |
) { | |
val localContext = LocalContext.current | |
val libVLC = remember(key1 = videoUrl, key2 = subtitleUrl) { LibVLC(localContext) } | |
val media = remember(key1 = videoUrl, key2 = subtitleUrl) { Media(libVLC, Uri.parse(videoUrl)) } | |
val mediaPlayer = remember(key1 = videoUrl, key2 = subtitleUrl) { | |
MediaPlayer(libVLC).apply { | |
setMedia(media) | |
} | |
} | |
if (!subtitleUrl.isNullOrBlank()) { | |
LaunchedEffect(key1 = subtitleUrl) { | |
media.addSlave( | |
IMedia.Slave( | |
IMedia.Slave.Type.Subtitle, | |
4, | |
Uri.parse(subtitleUrl).toString(), | |
) | |
) | |
} | |
} | |
AndroidView( | |
modifier = modifier, | |
factory = { context -> | |
VLCVideoLayout(context).apply { | |
mediaPlayer.attachViews(this, null, false, false) | |
mediaPlayer.play() | |
} | |
}, | |
update = { | |
mediaPlayer.play() | |
}, | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment