keep maximum number of subtitles to four.

This commit is contained in:
ood_tsen 2015-06-03 20:02:42 +08:00
parent 106ebbf7df
commit fd2ebc767c
3 changed files with 20 additions and 19 deletions

View File

@ -18,15 +18,18 @@ package com.google.android.exoplayer.text.tx3g;
import java.util.Comparator;
/**
* A representation of a single tx3g.
*/
class SubtitleData implements Comparable <SubtitleData>, Comparator<SubtitleData> {
public final long startTimePosUs;
public final String strSubtitle;
public final String subtitle;
SubtitleData(long startTimePosUs, String strSubtitle)
SubtitleData(long startTimePosUs, String subtitle)
{
this.startTimePosUs = startTimePosUs;
this.strSubtitle = strSubtitle;
this.subtitle = subtitle;
}
@Override

View File

@ -19,8 +19,6 @@ import com.google.android.exoplayer.text.Subtitle;
import com.google.android.exoplayer.text.SubtitleParser;
import com.google.android.exoplayer.util.MimeTypes;
import android.util.Log;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
@ -40,13 +38,12 @@ public class TextParser implements SubtitleParser {
private static final String TAG = "TextParser";
private final List<SubtitleData> subtitleList;
private static final int MAX_SUBTITLE_COUNT = 4;
public TextParser() {
Log.i(TAG,"TextParser ");
subtitleList = new LinkedList<SubtitleData>();
}
@Override
public Subtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs)
throws IOException {
@ -54,10 +51,13 @@ public class TextParser implements SubtitleParser {
DataInputStream in = new DataInputStream(inputStream);
String text = in.readUTF();
text = (text == null) ? "" : text;
Log.i(TAG,"parse(" + text + "," + startTimeUs + ")" );
SubtitleData cue = new SubtitleData(startTimeUs, text);
while (subtitleList.size() > MAX_SUBTITLE_COUNT) {
subtitleList.remove(0);
}
subtitleList.add(cue);
Collections.sort(subtitleList, new Comparator<SubtitleData>() {
@ -70,13 +70,11 @@ public class TextParser implements SubtitleParser {
return 0;
}
});
TextSubtitle textSubtitle = new TextSubtitle(subtitleList);
return textSubtitle;
return new TextSubtitle(subtitleList);
}
@Override
public boolean canParse(String mimeType) {
boolean rtn = MimeTypes.APPLICATION_TX3G.equals(mimeType);
return rtn;
return MimeTypes.APPLICATION_TX3G.equals(mimeType);
}
}

View File

@ -34,7 +34,7 @@ public final class TextSubtitle implements Subtitle {
@Override
public long getStartTime() {
return text.get(0).getStartTimePos();
return text.get(0).startTimePosUs;
}
@Override
@ -54,12 +54,12 @@ public final class TextSubtitle implements Subtitle {
public long getEventTime(int index) {
if (index > text.size() - 1) return -1;
return text.get(index).getStartTimePos();
return text.get(index).startTimePosUs;
}
@Override
public long getLastEventTime() {
return text.get(0).getStartTimePos();
return text.get(0).startTimePosUs;
}
@Override
@ -68,7 +68,7 @@ public final class TextSubtitle implements Subtitle {
List<Cue> list = new ArrayList<>();
if (index == -1) return null;
String str = text.get(index).getsubtitleText();
String str = text.get(index).subtitle;
list.add(new Cue(str));
return list;
@ -80,10 +80,10 @@ public final class TextSubtitle implements Subtitle {
int length = text.size();
for (int i = 0; i < length ; i++) {
SubtitleData data = text.get(i);
boolean bCheckFront = data.getStartTimePos() <= timeUs ;
boolean bCheckFront = data.startTimePosUs <= timeUs ;
boolean bCheckEnd = false;
if (i + 1 < length) {
bCheckEnd = text.get(i + 1).getStartTimePos() > timeUs ;
bCheckEnd = text.get(i + 1).startTimePosUs > timeUs ;
} else if (i + 1 == length) {
bCheckEnd = true;
}