SubtitleData

class SubtitleData
kotlin.Any
   ↳ android.media.SubtitleData

Class encapsulating subtitle data, as received through the MediaPlayer.OnSubtitleDataListener interface. The subtitle data includes:

  • the track index
  • the start time (in microseconds) of the data
  • the duration (in microseconds) of the data
  • the actual data.
The data is stored in a byte-array, and is encoded in one of the supported in-band subtitle formats. The subtitle encoding is determined by the MIME type of the MediaPlayer.TrackInfo of the subtitle track, one of MediaFormat#MIMETYPE_TEXT_CEA_608, MediaFormat#MIMETYPE_TEXT_CEA_708, MediaFormat#MIMETYPE_TEXT_VTT.

Here is an example of iterating over the tracks of a MediaPlayer, and checking which encoding is used for the subtitle tracks:

 MediaPlayer mp = new MediaPlayer(); mp.setDataSource(myContentLocation); mp.prepare(); // synchronous prepare, ready to use when method returns final TrackInfo[] trackInfos = mp.getTrackInfo(); for (TrackInfo info : trackInfo) { if (info.getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) { final String mime = info.getFormat().getString(MediaFormat.KEY_MIME); if (MediaFormat.MIMETYPE_TEXT_CEA_608.equals(mime) { // subtitle encoding is CEA 608 } else if (MediaFormat.MIMETYPE_TEXT_CEA_708.equals(mime) { // subtitle encoding is CEA 708 } else if (MediaFormat.MIMETYPE_TEXT_VTT.equals(mime) { // subtitle encoding is WebVTT } } } 

See MediaPlayer#setOnSubtitleDataListener(android.media.MediaPlayer.OnSubtitleDataListener, android.os.Handler) to receive subtitle data from a MediaPlayer object.
Requires API level 28 (Android 8.+, P)

Summary

Public methods
Int

Returns the index of the MediaPlayer track which contains this subtitle data.

Long

Returns the media time at which the subtitle should be displayed, expressed in microseconds.

ByteArray

Returns the encoded data for the subtitle content.

Long

Returns the duration in microsecond during which the subtitle should be displayed.

Public methods

getTrackIndex

added in API level 28
fun getTrackIndex(): Int

Returns the index of the MediaPlayer track which contains this subtitle data.
Requires API level 28 (Android 8.+, P)

Return
Int: an index in the array returned by MediaPlayer#getTrackInfo().

getStartTimeUs

added in API level 28
fun getStartTimeUs(): Long

Returns the media time at which the subtitle should be displayed, expressed in microseconds.
Requires API level 28 (Android 8.+, P)

Return
Long: the display start time for the subtitle

getData

added in API level 28
fun getData(): ByteArray

Returns the encoded data for the subtitle content. Encoding format depends on the subtitle type, refer to CEA 708, CEA/EIA 608 and WebVTT, defined by the MIME type of the subtitle track.
Requires API level 28 (Android 8.+, P)

Return
ByteArray: the encoded subtitle data This value will never be null.

getDurationUs

added in API level 28
fun getDurationUs(): Long

Returns the duration in microsecond during which the subtitle should be displayed.
Requires API level 28 (Android 8.+, P)

Return
Long: the display duration for the subtitle