Skip to content

Instantly share code, notes, and snippets.

@kafri8889
Last active August 14, 2022 00:38
Show Gist options
  • Save kafri8889/30397c32347364c646791e757fede726 to your computer and use it in GitHub Desktop.
Save kafri8889/30397c32347364c646791e757fede726 to your computer and use it in GitHub Desktop.
Remove audio from video with ExoPlayer
// inside the composable function
val context = LocalContext.current
var exoPlayer by remember { mutableStateOf<ExoPlayer?>(null) }
val transformerListener = remember {
object : Transformer.Listener {
override fun onTransformationCompleted(inputMediaItem: MediaItem, transformationResult: TransformationResult) {
super.onTransformationCompleted(inputMediaItem, transformationResult)
exoPlayer = ExoPlayer.Builder(context)
.setLooper(Looper.getMainLooper())
.build()
.apply {
setMediaItem(
MediaItem.fromUri(
File(
context.cacheDir,
"$id.mp4"
).toUri()
)
)
prepare()
}
}
}
}
val transformer = remember {
Transformer.Builder(context).apply {
setLooper(Looper.getMainLooper())
setRemoveAudio(true)
addListener(transformerListener)
}.build()
}
DisposableEffect(transformer) {
transformer.startTransformation(
MediaItem.fromUri(videoUri),
File(
context.cacheDir,
"$id.mp4"
).path
)
onDispose {
transformer.removeAllListeners()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment