mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
keep maximum number of subtitles to four.
This commit is contained in:
parent
106ebbf7df
commit
fd2ebc767c
@ -18,15 +18,18 @@ package com.google.android.exoplayer.text.tx3g;
|
|||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A representation of a single tx3g.
|
||||||
|
*/
|
||||||
class SubtitleData implements Comparable <SubtitleData>, Comparator<SubtitleData> {
|
class SubtitleData implements Comparable <SubtitleData>, Comparator<SubtitleData> {
|
||||||
|
|
||||||
public final long startTimePosUs;
|
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.startTimePosUs = startTimePosUs;
|
||||||
this.strSubtitle = strSubtitle;
|
this.subtitle = subtitle;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -19,8 +19,6 @@ import com.google.android.exoplayer.text.Subtitle;
|
|||||||
import com.google.android.exoplayer.text.SubtitleParser;
|
import com.google.android.exoplayer.text.SubtitleParser;
|
||||||
import com.google.android.exoplayer.util.MimeTypes;
|
import com.google.android.exoplayer.util.MimeTypes;
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -40,13 +38,12 @@ public class TextParser implements SubtitleParser {
|
|||||||
private static final String TAG = "TextParser";
|
private static final String TAG = "TextParser";
|
||||||
|
|
||||||
private final List<SubtitleData> subtitleList;
|
private final List<SubtitleData> subtitleList;
|
||||||
|
private static final int MAX_SUBTITLE_COUNT = 4;
|
||||||
public TextParser() {
|
public TextParser() {
|
||||||
Log.i(TAG,"TextParser ");
|
|
||||||
subtitleList = new LinkedList<SubtitleData>();
|
subtitleList = new LinkedList<SubtitleData>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs)
|
public Subtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
@ -54,10 +51,13 @@ public class TextParser implements SubtitleParser {
|
|||||||
DataInputStream in = new DataInputStream(inputStream);
|
DataInputStream in = new DataInputStream(inputStream);
|
||||||
String text = in.readUTF();
|
String text = in.readUTF();
|
||||||
text = (text == null) ? "" : text;
|
text = (text == null) ? "" : text;
|
||||||
Log.i(TAG,"parse(" + text + "," + startTimeUs + ")" );
|
|
||||||
|
|
||||||
SubtitleData cue = new SubtitleData(startTimeUs, text);
|
SubtitleData cue = new SubtitleData(startTimeUs, text);
|
||||||
|
|
||||||
|
while (subtitleList.size() > MAX_SUBTITLE_COUNT) {
|
||||||
|
subtitleList.remove(0);
|
||||||
|
}
|
||||||
|
|
||||||
subtitleList.add(cue);
|
subtitleList.add(cue);
|
||||||
|
|
||||||
Collections.sort(subtitleList, new Comparator<SubtitleData>() {
|
Collections.sort(subtitleList, new Comparator<SubtitleData>() {
|
||||||
@ -70,13 +70,11 @@ public class TextParser implements SubtitleParser {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
TextSubtitle textSubtitle = new TextSubtitle(subtitleList);
|
return new TextSubtitle(subtitleList);
|
||||||
return textSubtitle;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canParse(String mimeType) {
|
public boolean canParse(String mimeType) {
|
||||||
boolean rtn = MimeTypes.APPLICATION_TX3G.equals(mimeType);
|
return MimeTypes.APPLICATION_TX3G.equals(mimeType);
|
||||||
return rtn;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ public final class TextSubtitle implements Subtitle {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getStartTime() {
|
public long getStartTime() {
|
||||||
return text.get(0).getStartTimePos();
|
return text.get(0).startTimePosUs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -54,12 +54,12 @@ public final class TextSubtitle implements Subtitle {
|
|||||||
public long getEventTime(int index) {
|
public long getEventTime(int index) {
|
||||||
if (index > text.size() - 1) return -1;
|
if (index > text.size() - 1) return -1;
|
||||||
|
|
||||||
return text.get(index).getStartTimePos();
|
return text.get(index).startTimePosUs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getLastEventTime() {
|
public long getLastEventTime() {
|
||||||
return text.get(0).getStartTimePos();
|
return text.get(0).startTimePosUs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -68,7 +68,7 @@ public final class TextSubtitle implements Subtitle {
|
|||||||
List<Cue> list = new ArrayList<>();
|
List<Cue> list = new ArrayList<>();
|
||||||
if (index == -1) return null;
|
if (index == -1) return null;
|
||||||
|
|
||||||
String str = text.get(index).getsubtitleText();
|
String str = text.get(index).subtitle;
|
||||||
|
|
||||||
list.add(new Cue(str));
|
list.add(new Cue(str));
|
||||||
return list;
|
return list;
|
||||||
@ -80,10 +80,10 @@ public final class TextSubtitle implements Subtitle {
|
|||||||
int length = text.size();
|
int length = text.size();
|
||||||
for (int i = 0; i < length ; i++) {
|
for (int i = 0; i < length ; i++) {
|
||||||
SubtitleData data = text.get(i);
|
SubtitleData data = text.get(i);
|
||||||
boolean bCheckFront = data.getStartTimePos() <= timeUs ;
|
boolean bCheckFront = data.startTimePosUs <= timeUs ;
|
||||||
boolean bCheckEnd = false;
|
boolean bCheckEnd = false;
|
||||||
if (i + 1 < length) {
|
if (i + 1 < length) {
|
||||||
bCheckEnd = text.get(i + 1).getStartTimePos() > timeUs ;
|
bCheckEnd = text.get(i + 1).startTimePosUs > timeUs ;
|
||||||
} else if (i + 1 == length) {
|
} else if (i + 1 == length) {
|
||||||
bCheckEnd = true;
|
bCheckEnd = true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user