Audio description in Jetpack Compose
Videos should include audio description when important visual details are shown which you cannot hear. Audio description is an additional sound track which describes these important visual details. This allows people who are blind or have difficulty processing visual information to understand the content.
Audio description - Jetpack Compose
In Jetpack Compose, the MediaPlayer
has support for multiple audio tracks. Use the selectTrack
method to select the correct audio track.
The code example belows shows a basic implementation of selecting an audio description track embedded inside a video.
var mediaPlayer: MediaPlayer? by remember { mutableStateOf(null) }
var error by remember { mutableStateOf<String?>(null) }
val currentContext = LocalContext.current
DisposableEffect(Unit) {
val player = MediaPlayer.create(currentContext, R.raw.video)
mediaPlayer = player
try {
player.trackInfo.forEachIndexed { index, trackInfo ->
if (trackInfo.trackType == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO) {
player.selectTrack(index)
return@forEachIndexed
}
}
player.start()
} catch (e: Exception) {
e.printStackTrace()
error = e.message
}
onDispose {
player.release()
mediaPlayer = null
}
}