我正在尝试向视频 View 添加字幕 而getSubtitleSource的方法是这样的:
private InputStream getSubtitleSource(String filepath) {
InputStream ins = null;
String ccFileName = filepath.substring(0,filepath.lastIndexOf('.'));
File file = new File(ccFileName);
if (file.exists() == false)
{
Log.e(TAG,"no close caption file " + ccFileName);
return null;
}
FileInputStream fins = null;
try {
fins = new FileInputStream(file);
}catch (Exception e) {
Log.e(TAG,"exception " + e);
}
ins = (InputStream)fins;
return ins;
}
这里是我如何调用该方法并使用 srt 文件:
videoView.seekTo(position);
String srt = "http://shasha/share/2018/09/07/Lucha_Mexico_.srt";
videoView.addSubtitleSource(getSubtitleSource(srt),MediaFormat.createSubtitleFormat("text/srt",Locale.ENGLISH.getLanguage()));
videoView.start();
addSubtitleSource 和 createSubtitleFormat 有问题说:
Call requires API level 19 (current min is 16): android.widget.VideoView#addSubtitleSource
所以我只是用这样的sdk版本包围它:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
videoView.seekTo(position);
String srt = "http://shasha/share/2018/09/07/Lucha_Mexico_.srt";
videoView.addSubtitleSource(getSubtitleSource(srt),MediaFormat.createSubtitleFormat("text/srt",Locale.ENGLISH.getLanguage()));
videoView.start();
}
但是当我播放视频时没有出现字幕,这是怎么回事
最佳答案
检查 Build.VERSION 只是避免您的应用程序崩溃。因此,如果您使用低于 api 级别 19 的设备,应用程序不会崩溃,但 getSubTitleSource 将不起作用。
只需在 app.gradle 中使用 api 级别 19 编辑 minSdkVersion 即可。
minSdkVersion 19
关于java - 将 addSubtitleSource 与 videoview 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233877/