Live captions - Android

On Android, we recommend using a library such as Media3, also known as ExoPlayer, to support live captions. Media3 is developed by Google and is an open-source alternative to Android's MediaPlayer for audio and video playback. Many code examples can be found in the Camera & Media dev center. You can use DefaultTrackSelector in combination with DefaultHttpDataSource.Factory to show subtitles.

val trackSelector = DefaultTrackSelector(baseContext).apply {
    setParameters(buildUponParameters().setPreferredTextLanguage("nl"))
}
val player = ExoPlayer.Builder(context)
    .setTrackSelector(trackSelector)
    .build()
val dataSourceFactory = DefaultHttpDataSource.Factory()
    .setUserAgent(Util.getUserAgent(context, "Appt"))
    .setAllowCrossProtocolRedirects(true)

val mediaUri = Uri.parse("https://appt.org/live-video")
val mediaSource = HlsMediaSource.Factory(dataSourceFactory)
    .createMediaSource(MediaItem.fromUri(mediaUri))

player.setMediaSource(mediaSource)
player.prepare()
player.playWhenReady = true