我在我的申请中播放一个视频,我必须在上面显示字幕。字幕位于一个单独的smi文件中,这是一种提供字幕信息的XML标记。字幕文件如下所示:
.
.
.
<sync start=104600>
<P Class=GBR><i></i><span ID=style1_0><i>Born of cold and winter air</i> </span><br><i></i><span ID=style1_0><i>and mountain rain combining.</i> </span></P>
</sync>
<sync start=107080>
<P Class=MYS> <br></P>
</sync>
<sync start=107200>
<P Class=MYS><i></i><span ID=style2_0><i>...serta hujan di pergunungan.</i> </span></P>
</sync>
<sync start=110840>
<P Class=MYS> <br></P>
<P Class=GBR> <br></P>
</sync>
<sync start=111160>
<P Class=SIM> <br></P>
</sync>
<sync start=111480>
<P Class=GBR><i></i><span ID=style1_0><i>This icy force</i> </span><br><i></i><span ID=style1_0><i>both foul and fair...</i> </span></P>
</sync>
.
.
.
.start是在视频顶部显示字幕的时间,以毫秒为单位。现在,我正在解析使用regex并创建一个ArrayList of SubtitleChunk,其中包含与其相关的startTime和内容。
class SubtitleChunk {
long startTime;
String content;
}现在,在显示视频时,我已经启动了一个单独的线程,我正在使用它,从数组获取字幕并显示它。以下是逻辑
public void run() {
while (!isFinished_) {
if (subtitleContent != null && subtitleContent.content_ != null) {
for (int i = 0; i < length; i++) {
SubtitleChunk subChunk1 = null;
SubtitleChunk subChunk2 = null;
try {
subChunk1 = subtitleContent.content_.get(i);
subChunk2 = subtitleContent.content_.get(i + 1);
long cTime = moviePlayer_.getCurrentTimeMillis();
if (cTime > subChunk1.startTime && (subChunk2 == null || cTime < subChunk2.startTime)) {
currentSub = subChunk1.content;
currentChunk = subChunk1;
break;
}
} catch (Exception e) {
DebugUtil.printLogException(VIEW_LOG_TAG, e);
currentSub = "";
}
}
} else {
currentSub = "";
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
if (moviePlayer_.isPlaying()) {
subtitleTextView.setText(Html.fromHtml(currentSub));
} else {
}
}
});
}
}正如您所看到的,我正在ArrayList上运行一个循环,并将字幕的时间与我觉得太贵的moviePlayer_.getCurrentTimeMillis()进行比较。字幕落后,与视频不同步。
以上都是对这个问题的解释。现在问题..。我该如何改进呢?以O(1)时间获得某一特定时间的字幕。
编辑
我带着间隔树,这就是我需要把字幕放进去的地方:我从这里得到的:https://github.com/phishman3579/java-algorithms-implementation
发布于 2014-03-11 19:21:05
更新:只是为了被接受:-)从我的评论中取消。
startTime相对于视频的开头。因此,相对于开始,人们知道视频中的一个在哪里。如果当前相对时间在一定范围内,那么我就可以得到字幕。我不确定来自Java库的任何数据结构是否会有帮助。看看段树/间隔树。它会给你更多的洞察力,我希望这可能会有所帮助。其想法是对SMI文件进行预处理。
https://stackoverflow.com/questions/22290874
复制相似问题