SubtitleData2

class SubtitleData2
kotlin.Any
   ↳ androidx.media2.SubtitleData2

Class encapsulating subtitle data, as received through the MediaPlayer2.EventCallback#onSubtitleData 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 MediaPlayer2.TrackInfo of the subtitle track, one of MIMETYPE_TEXT_CEA_608, MIMETYPE_TEXT_CEA_708, MIMETYPE_TEXT_VTT.

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

 MediaPlayer2 mp2 = MediaPlayer2.create(); // prepare the player with a valid data source. … final TrackInfo[] trackInfos = mp2.getTrackInfo(); for (TrackInfo info : trackInfo) { if (info.getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) { final String mime = info.getFormat().getString(MediaFormat.KEY_MIME); if (SubtitleData2.MIMETYPE_TEXT_CEA_608.equals(mime) { // subtitle encoding is CEA 608 } else if (SubtitleData2.MIMETYPE_TEXT_CEA_708.equals(mime) { // subtitle encoding is CEA 708 } else if (SubtitleData2.MIMETYPE_TEXT_VTT.equals(mime) { // subtitle encoding is WebVTT } } } 

Summary

Constants
static String

MIME type for CEA-608 closed caption data.

static String

MIME type for CEA-708 closed caption data.

static String

MIME type for WebVTT subtitle data.

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.

Constants

MIMETYPE_TEXT_CEA_608

static val MIMETYPE_TEXT_CEA_608: String

MIME type for CEA-608 closed caption data.

Value: "text/cea-608"

MIMETYPE_TEXT_CEA_708

static val MIMETYPE_TEXT_CEA_708: String

MIME type for CEA-708 closed caption data.

Value: "text/cea-708"

MIMETYPE_TEXT_VTT

static val MIMETYPE_TEXT_VTT: String

MIME type for WebVTT subtitle data.

Value: "text/vtt"

Public methods

getTrackIndex

fun getTrackIndex(): Int

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

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

getStartTimeUs

fun getStartTimeUs(): Long

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

Return
Long: the display start time for the subtitle

getData

@NonNull 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.

Return
ByteArray: the encoded subtitle data

getDurationUs

fun getDurationUs(): Long

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

Return
Long: the display duration for the subtitle