通过把 字幕文件传给MediaPlayer , media 可以在自动的适配字幕, 在正确的时间点, 给我们 当前应该显示的字幕回调
不过只能在 API 16 以上才能使用
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.addTimedTextSource(path, MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
} catch (IOException e) {
e.printStackTrace();
}
MediaPlayer.TrackInfo[] trackInfos = mMediaPlayer.getTrackInfo();
if (trackInfos != null && trackInfos.length > 0) {
for (int i = 0; i < trackInfos.length; i++) {
final MediaPlayer.TrackInfo info = trackInfos[i];
LogUtil.w(TAG, "TrackInfo: " + info.getTrackType() + " "
+ info.getLanguage());
if (info.getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO) {
// mMediaPlayer.selectTrack(i);
} else if (info.getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) {
mMediaPlayer.selectTrack(i);
}
}
}
mMediaPlayer.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
@Override
public void onTimedText(MediaPlayer mp, TimedText text) {
if (text != null) {
LogUtil.d(TAG, "text = " + text.getText());
}
}
});
当然也可手动解析字幕
下面是解析字幕的相关代码 (解析的 是 srt 字幕)
public class SrtHelper {
public static final String TAG = "SrtHelper";
private List<SrtModel> mSrtModelList;
public String getTimeText(int timeMils) {
String currentTimeTxt = "";
if (mSrtModelList != null && !mSrtModelList.isEmpty()) {
for (SrtModel srtModel : mSrtModelList) {
if (timeMils > srtModel.getBeginTime() && timeMils < srtModel.getEndTime()) {
// 时当前该显示的字幕
currentTimeTxt = srtModel.getSrtBody();
}
}
}
return currentTimeTxt;
}
public int getTimeTextIndex(int timeMils) {
int index = -1;
if (mSrtModelList != null && !mSrtModelList.isEmpty()) {
for (int i = 0; i < mSrtModelList.size(); i++) {
SrtModel srtModel = mSrtModelList.get(i);
if (timeMils > srtModel.getBeginTime() && timeMils < srtModel.getEndTime()) {
// 时当前该显示的字幕
index = i;
break;
}
}
}
return index;
}
public SrtModel get(int index) {
if (mSrtModelList != null && !mSrtModelList.isEmpty()) {
return mSrtModelList.get(index);
}
return null;
}
public List<String> getSrtStringList() {
List<String> list = new ArrayList<>();
if (mSrtModelList != null && !mSrtModelList.isEmpty()) {
for (SrtModel srtModel : mSrtModelList) {
list.add(srtModel.getSrtBody());
}
}
return list;
}
public void parseSrt(String srtPath) {
File srtFil = new File(srtPath);
try {
if (mSrtModelList == null) {
mSrtModelList = new ArrayList<>();
} else {
mSrtModelList.clear();
}
FileReader fr = new FileReader(srtFil); //创建文件输入流
BufferedReader br = new BufferedReader(fr);
String line;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
if (!line.equals("")) {
sb.append(line).append("@");
continue;
}
String[] parseStrs = sb.toString().split("@");
if (parseStrs.length < 3) {
sb.delete(0, sb.length());// 清空,否则影响下一个字幕元素的解析</i>
continue;
}
SrtModel srt = new SrtModel();
String timeTotime = parseStrs[1];
int begin_hour = Integer.parseInt(timeTotime.substring(0, 2));
int begin_mintue = Integer.parseInt(timeTotime.substring(3, 5));
int begin_scend = Integer.parseInt(timeTotime.substring(6, 8));
int begin_milli = Integer.parseInt(timeTotime.substring(9, 12));
int beginTime = (begin_hour * 3600 + begin_mintue * 60 + begin_scend) * 1000 + begin_milli;
int end_hour = Integer.parseInt(timeTotime.substring(17, 19));
int end_mintue = Integer.parseInt(timeTotime.substring(20, 22));
int end_scend = Integer.parseInt(timeTotime.substring(23, 25));
int end_milli = Integer.parseInt(timeTotime.substring(26, 29));
int endTime = (end_hour * 3600 + end_mintue * 60 + end_scend) * 1000 + end_milli;
LogUtil.d(TAG, "开始:"
+ begin_hour + ":"
+ begin_mintue + ":"
+ begin_scend + ":"
+ begin_milli + "="
+ beginTime + "ms");
LogUtil.d(TAG, "结束:"
+ end_hour + ":"
+ end_mintue + ":"
+ end_scend + ":"
+ end_milli + "="
+ endTime + "ms");
String srtBody = "";
for (int i = 2; i < parseStrs.length; i++) {
srtBody += parseStrs[i] + "\n";
}
LogUtil.d(TAG, "srtBody = " + srtBody);
// 删除最后一个"\n"
srtBody = srtBody.substring(0, srtBody.length() - 1);
srt.setBeginTime(beginTime);
srt.setEndTime(endTime);
srt.setSrtBody(new String(srtBody.getBytes(), "UTF-8"));
mSrtModelList.add(srt);
sb.delete(0, sb.length());// 清空,否则影响下一个字幕元素的解析
}
} catch (Exception e) {
e.printStackTrace();
}
}
}