Last active
August 14, 2022 00:38
-
-
Save kafri8889/30397c32347364c646791e757fede726 to your computer and use it in GitHub Desktop.
Remove audio from video with ExoPlayer
This file contains 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
// 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