use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.
the class TtmlDecoderTest method testFontSizeWithInvalidValueIsIgnored.
public void testFontSizeWithInvalidValueIsIgnored() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_INVALID_TTML_FILE);
assertEquals(6, subtitle.getEventTimeCount());
List<Cue> cues = subtitle.getCues(10 * 1000000);
assertEquals(1, cues.size());
SpannableStringBuilder spannable = (SpannableStringBuilder) cues.get(0).text;
assertEquals("invalid", String.valueOf(spannable));
assertEquals(0, spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class).length);
assertEquals(0, spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class).length);
cues = subtitle.getCues(20 * 1000000);
assertEquals(1, cues.size());
spannable = (SpannableStringBuilder) cues.get(0).text;
assertEquals("invalid", String.valueOf(spannable));
assertEquals(0, spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class).length);
assertEquals(0, spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class).length);
cues = subtitle.getCues(30 * 1000000);
assertEquals(1, cues.size());
spannable = (SpannableStringBuilder) cues.get(0).text;
assertEquals("invalid dot", String.valueOf(spannable));
assertEquals(0, spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class).length);
assertEquals(0, spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class).length);
}
use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.
the class Mp4WebvttDecoderTest method testTwoCuesSample.
public void testTwoCuesSample() throws SubtitleDecoderException {
Mp4WebvttDecoder decoder = new Mp4WebvttDecoder();
Subtitle result = decoder.decode(DOUBLE_CUE_SAMPLE, DOUBLE_CUE_SAMPLE.length);
Cue firstExpectedCue = new Cue("Hello World");
Cue secondExpectedCue = new Cue("Bye Bye");
assertMp4WebvttSubtitleEquals(result, firstExpectedCue, secondExpectedCue);
}
use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.
the class SubripDecoder method decode.
@Override
protected SubripSubtitle decode(byte[] bytes, int length) {
ArrayList<Cue> cues = new ArrayList<>();
LongArray cueTimesUs = new LongArray();
ParsableByteArray subripData = new ParsableByteArray(bytes, length);
String currentLine;
while ((currentLine = subripData.readLine()) != null) {
if (currentLine.length() == 0) {
// Skip blank lines.
continue;
}
// Parse the index line as a sanity check.
try {
Integer.parseInt(currentLine);
} catch (NumberFormatException e) {
Log.w(TAG, "Skipping invalid index: " + currentLine);
continue;
}
// Read and parse the timing line.
boolean haveEndTimecode = false;
currentLine = subripData.readLine();
Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine);
if (matcher.matches()) {
cueTimesUs.add(parseTimecode(matcher, 1));
if (!TextUtils.isEmpty(matcher.group(6))) {
haveEndTimecode = true;
cueTimesUs.add(parseTimecode(matcher, 6));
}
} else {
Log.w(TAG, "Skipping invalid timing: " + currentLine);
continue;
}
// Read and parse the text.
textBuilder.setLength(0);
while (!TextUtils.isEmpty(currentLine = subripData.readLine())) {
if (textBuilder.length() > 0) {
textBuilder.append("<br>");
}
textBuilder.append(currentLine.trim());
}
Spanned text = Html.fromHtml(textBuilder.toString());
cues.add(new Cue(text));
if (haveEndTimecode) {
cues.add(null);
}
}
Cue[] cuesArray = new Cue[cues.size()];
cues.toArray(cuesArray);
long[] cueTimesUsArray = cueTimesUs.toArray();
return new SubripSubtitle(cuesArray, cueTimesUsArray);
}
use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.
the class TtmlNode method getCues.
public List<Cue> getCues(long timeUs, Map<String, TtmlStyle> globalStyles, Map<String, TtmlRegion> regionMap) {
TreeMap<String, SpannableStringBuilder> regionOutputs = new TreeMap<>();
traverseForText(timeUs, false, regionId, regionOutputs);
traverseForStyle(globalStyles, regionOutputs);
List<Cue> cues = new ArrayList<>();
for (Entry<String, SpannableStringBuilder> entry : regionOutputs.entrySet()) {
TtmlRegion region = regionMap.get(entry.getKey());
cues.add(new Cue(cleanUpText(entry.getValue()), null, region.line, region.lineType, Cue.TYPE_UNSET, region.position, Cue.TYPE_UNSET, region.width));
}
return cues;
}
use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.
the class WebvttDecoder method decode.
@Override
protected WebvttSubtitle decode(byte[] bytes, int length) throws SubtitleDecoderException {
parsableWebvttData.reset(bytes, length);
// Initialization for consistent starting state.
webvttCueBuilder.reset();
definedStyles.clear();
// Validate the first line of the header, and skip the remainder.
WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {
}
int event;
ArrayList<WebvttCue> subtitles = new ArrayList<>();
while ((event = getNextEvent(parsableWebvttData)) != EVENT_END_OF_FILE) {
if (event == EVENT_COMMENT) {
skipComment(parsableWebvttData);
} else if (event == EVENT_STYLE_BLOCK) {
if (!subtitles.isEmpty()) {
throw new SubtitleDecoderException("A style block was found after the first cue.");
}
// Consume the "STYLE" header.
parsableWebvttData.readLine();
WebvttCssStyle styleBlock = cssParser.parseBlock(parsableWebvttData);
if (styleBlock != null) {
definedStyles.add(styleBlock);
}
} else if (event == EVENT_CUE) {
if (cueParser.parseCue(parsableWebvttData, webvttCueBuilder, definedStyles)) {
subtitles.add(webvttCueBuilder.build());
webvttCueBuilder.reset();
}
}
}
return new WebvttSubtitle(subtitles);
}
Aggregations