1.定义一个实体类
用于解析srt文件的内容
public class SRT {
private int beginTime;
private int endTime;
private String srtBody;
public int getBeginTime() {
return beginTime;
}
public void setBeginTime(int beginTime) {
this.beginTime = beginTime;
}
public int getEndTime() {
return endTime;
}
public void setEndTime(int endTime) {
this.endTime = endTime;
}
public String getSrtBody() {
return srtBody;
}
public void setSrtBody(String srtBody) {
this.srtBody = srtBody;
}
@Override
public String toString() {
return "" + beginTime + ":" + endTime + ":" + srtBody;
}
}
2.定义一个方法解析srt文件,并存放在集合中
public class SrtParser {
public static int lastEndTime;
private static ArrayList<SRT> enSrtList;
private static String content;
/**
* 解析SRT字幕文件
* 字幕路径
*/
public static void parseSrt(String name, ArrayList<SRT> srtList) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(name);
BufferedReader br = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
if (!line.equals("")) {
sb.append(line).append("@");
continue;
}
String[] parseStrs = sb.toString().split("@");
// 该if为了适应一开始就有空行以及其他不符格式的空行情况
if (parseStrs.length < 3) {
sb.delete(0, sb.length());// 清空,否则影响下一个字幕元素的解析</i>
continue;
}
SRT srt = new SRT();
// 解析开始和结束时间
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 beginTime = (begin_hour * 3600 + begin_mintue * 60 + begin_scend)
* 1000;
int end_hour = Integer.parseInt(timeTotime.substring(14, 16));
int end_mintue = Integer.parseInt(timeTotime.substring(17, 19));
int end_scend = Integer.parseInt(timeTotime.substring(20, 22));
int endTime = (end_hour * 3600 + end_mintue * 60 + end_scend)
* 1000;
//解析字幕文字
String srtBody = "";
// 可能1句字幕,也可能2句及以上。
for (int i = 2; i < parseStrs.length; i++) {
srtBody += parseStrs[2] + "\n";
}
// 删除最后一个"\n"
srtBody = srtBody.substring(0, srtBody.length() - 1);
// 设置SRT
srt.setBeginTime(beginTime);
srt.setEndTime(endTime);
srt.setSrtBody(srtBody);
srtList.add(srt);
sb.delete(0, sb.length());// 清空,否则影响下一个字幕元素的解析
}
lastEndTime = srtList.get(srtList.size() - 1).getEndTime();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//显示字幕(双语显示,如果只是单语显示,不要 if (isGB2312(srtBody)) 这个判断)
public static void showSRT(GiraffePlayer videoView, TextView tvSrtCN, TextView tvSrtEN, String s, ArrayList<SRT> srtList, int size) {
int currentPosition = videoView.getCurrentPosition();//vv是VideoView播放器
if (currentPosition > lastEndTime) {
tvSrtCN.setVisibility(View.GONE);
tvSrtEN.setVisibility(View.GONE);
}
for (int i = 0; i < srtList.size(); i++) {
SRT srtbean = srtList.get(i);
if (currentPosition >= srtbean.getBeginTime()
&& currentPosition <= srtbean.getEndTime()) {
String srtBody = srtbean.getSrtBody();
if (isGB2312(srtBody)) {
tvSrtCN.setVisibility(View.VISIBLE);
tvSrtCN.setText(srtBody);
Log.e("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "中文");
} else {
tvSrtEN.setVisibility(View.VISIBLE);
tvSrtEN.setText(srtBody);
Log.e("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "英文");
}
//显示过的就删掉,提高查询效率
// srtList.remove(i);
// break;//找到后就没必要继续遍历下去,节约资源
} else {
if (i < srtList.size() - 2 && currentPosition > srtbean.getEndTime() && currentPosition < srtList.get(i + 1).getBeginTime()) {
tvSrtCN.setVisibility(View.GONE);
tvSrtEN.setVisibility(View.GONE);
}
}
}
// }
}
public static Boolean isGB2312(String str) {
for (int i = 0; i < str.length(); i++) {
String bb = str.substring(i, i + 1);
// 生成一个Pattern,同时编译一个正则表达式,其中的u4E00("一"的unicode编码)-\u9FA5("龥"的unicode编码)
boolean cc = java.util.regex.Pattern.matches("[\u4E00-\u9FA5]", bb);
if (cc == false) {
return cc;
}
}
return true;
}
3.下载srt文件
根据服务器提供的地址,下载srt字幕文件,存储在本地,然后调用解析的方法,显示字幕,并且用Handler每个多少秒去遍历集合显示字幕
//下载的逻辑自己去处理
//本地存储的路径
String name = Environment.getExternalStorageDirectory().getPath() + "/" + srt;
//解析srt文件
SrtParser.parseSrt(name, srtList);
//发送Handler请求,每个5秒
mHandler.sendEmptyMessageDelayed(0, 500);
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case PARSE_SRT_CN:
SrtParser.showSRT(mGiraffePlayer, cn_subtitleview, en_subtitleview, "cn", srtList, size);
//每隔500ms执行一次showSRT()
mHandler.sendEmptyMessageDelayed(0, 500);
}
}
};