我可以分配一次字幕,但之后我会尝试更改字幕。它不会接受新的,它每次都显示前一个。
以下代码我用来更改字幕。
MediaPlayer mediaPlayer;
public void changeSubtitle()
{
//disable subtitle if it has
if(textTrackIndex!=0){
mediaPlayer.deselectTrack(textTrackIndex);
textTrackIndex=0;
mediaPlayer.setOnTimedTextListener(null);
}
//try to asssign new subtitle
mediaPlayer
.addTimedTextSource(
(Environment.getExternalStorageDirectory()
.getPath()
+ "/"+"filename",
MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
int textTrackIndex = findTrackIndexFor(TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT,mediaPlayer.getTrackInfo());
if (textTrackIndex >= 0) {
mediaPlayer.selectTrack(textTrackIndex);
} else {
Log.w(TAG, "Cannot find text track!");
}
mediaPlayer.setOnTimedTextListener(this);
}
private int findTrackIndexFor(int mediaTrackType, TrackInfo[] trackInfo)
{
int index = -1;
for (int i = 0; i < trackInfo.length; i++) {
if (trackInfo[i].getTrackType() == mediaTrackType) {
return i;
}
}
return index;
}