I tried this sample iTech project (here you can find it here ) Its about displaying subtitles using the Android MediaPlayer class.
When I try to start a project, the media player works fine, but the subtitles do not. After debugging, I found out that getTrackInfo () returns an array with a length of 2 (this is only a video and audio track), and the timedText track was not included in the array. Does anyone know why?
For convenience, send the code snippet below:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
MediaPlayer player = MediaPlayer.create(this, R.raw.video);
try {
player.addTimedTextSource(getSubtitleFile(R.raw.sub),
MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
int textTrackIndex = findTrackIndexFor(
TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT, player.getTrackInfo());
Log.d(TAG, "textTrackIndex = "+textTrackIndex);
if (textTrackIndex >= 0) {
player.selectTrack(textTrackIndex);
} else {
Log.w(TAG, "Cannot find text track!");
}
player.setOnTimedTextListener(this);
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private int findTrackIndexFor(int mediaTrackType, TrackInfo[] trackInfo) {
int index = -1;
Log.w(TAG, "trackinfo length = "+trackInfo.length);
for (int i = 0; i < trackInfo.length; i++) {
Log.d(TAG, "track type = "+trackInfo[i].getTrackType()+" and mediaTrackType = "+mediaTrackType);
if (trackInfo[i].getTrackType() == mediaTrackType) {
return i;
}
}
return index;
}
Please note: if you try a sample project, the video will not be displayed, but there will only be sound. This is not a mistake, as I think iTech intentionally does not show the video :)
EDIT:
Nexus 10 Android 4.2.2, . , Android 4.1.2 ...