Ga naar hoofdinhoud
Logo Appt Light

Captions on Android

Captions should be provided to enable users to understand what is being said in videos. Captions are a text based alternative for media, such as videos. The spoken dialogue and sound effects are displayed on screen as the video plays. Captions mostly benefit people who are deaf, hard of hearing, or deafblind. But captions are also useful to anyone who is temporarily unable to perceive sound, for example inside a library.

Op Android kun je ondertiteling toevoegen door gebruik te maken van TimedText binnen een MediaPlayer.

Het codevoorbeeld toont een versimpelde manier om ondertiteling toe te voegen.

val player = MediaPlayer.create(this, R.raw.video)
try {
    player.addTimedTextSource("/assets/appt.srt", MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP)
    player.trackInfo.forEachIndexed { index, trackInfo ->
        if (trackInfo.trackType == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) {
            player.selectTrack(index)
            return@forEachIndexed
        }
    }
    player.setOnTimedTextListener(this)
    player.start()
} catch (e: Exception) {
    e.printStackTrace()
}
Bijdragen

Feedback?

Laat 't ons weten!