com.google.android.exoplayer.text.Cue

Here are the examples of the java api class com.google.android.exoplayer.text.Cue taken from open source projects.

1. WebvttParserTest#assertCue()

Project: ExoPlayer
File: WebvttParserTest.java
private static void assertCue(WebvttSubtitle subtitle, int eventTimeIndex, long startTimeUs, int endTimeUs, String text, Alignment textAlignment, float line, int lineType, int lineAnchor, float position, int positionAnchor, float size) {
    assertEquals(startTimeUs, subtitle.getEventTime(eventTimeIndex));
    assertEquals(endTimeUs, subtitle.getEventTime(eventTimeIndex + 1));
    List<Cue> cues = subtitle.getCues(subtitle.getEventTime(eventTimeIndex));
    assertEquals(1, cues.size());
    // Assert cue properties
    Cue cue = cues.get(0);
    assertEquals(text, cue.text.toString());
    assertEquals(textAlignment, cue.textAlignment);
    assertEquals(line, cue.line);
    assertEquals(lineType, cue.lineType);
    assertEquals(lineAnchor, cue.lineAnchor);
    assertEquals(position, cue.position);
    assertEquals(positionAnchor, cue.positionAnchor);
    assertEquals(size, cue.size);
}

2. Mp4WebvttParserTest#testTwoCuesSample()

Project: ExoPlayer
File: Mp4WebvttParserTest.java
public void testTwoCuesSample() throws ParserException {
    Subtitle result = parser.parse(DOUBLE_CUE_SAMPLE, 0, DOUBLE_CUE_SAMPLE.length);
    Cue firstExpectedCue = new Cue("Hello World");
    Cue secondExpectedCue = new Cue("Bye Bye");
    assertMp4WebvttSubtitleEquals(result, firstExpectedCue, secondExpectedCue);
}

3. SubripParser#parse()

Project: ExoPlayer
File: SubripParser.java
@Override
public SubripSubtitle parse(byte[] bytes, int offset, int length) {
    ArrayList<Cue> cues = new ArrayList<>();
    LongArray cueTimesUs = new LongArray();
    ParsableByteArray subripData = new ParsableByteArray(bytes, offset + length);
    subripData.setPosition(offset);
    boolean haveEndTimecode;
    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.
        haveEndTimecode = false;
        currentLine = subripData.readLine();
        Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine);
        if (matcher.find()) {
            cueTimesUs.add(parseTimecode(matcher.group(1)));
            String endTimecode = matcher.group(2);
            if (!TextUtils.isEmpty(endTimecode)) {
                haveEndTimecode = true;
                cueTimesUs.add(parseTimecode(matcher.group(2)));
            }
        } 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);
}

4. Mp4WebvttParserTest#testSingleCueSample()

Project: ExoPlayer
File: Mp4WebvttParserTest.java
// Positive tests.
public void testSingleCueSample() throws ParserException {
    Subtitle result = parser.parse(SINGLE_CUE_SAMPLE, 0, SINGLE_CUE_SAMPLE.length);
    // Line feed must be trimmed by the parser
    Cue expectedCue = new Cue("Hello World");
    assertMp4WebvttSubtitleEquals(result, expectedCue);
}

5. TtmlParserTest#testMultipleRegions()

Project: ExoPlayer
File: TtmlParserTest.java
public void testMultipleRegions() throws IOException {
    TtmlSubtitle subtitle = getSubtitle(MULTIPLE_REGIONS_TTML_FILE);
    List<Cue> output = subtitle.getCues(1000000);
    assertEquals(2, output.size());
    Cue ttmlCue = output.get(0);
    assertEquals("lorem", ttmlCue.text.toString());
    assertEquals(10.f / 100.f, ttmlCue.position);
    assertEquals(10.f / 100.f, ttmlCue.line);
    assertEquals(20.f / 100.f, ttmlCue.size);
    ttmlCue = output.get(1);
    assertEquals("amet", ttmlCue.text.toString());
    assertEquals(60.f / 100.f, ttmlCue.position);
    assertEquals(10.f / 100.f, ttmlCue.line);
    assertEquals(20.f / 100.f, ttmlCue.size);
    output = subtitle.getCues(5000000);
    assertEquals(1, output.size());
    ttmlCue = output.get(0);
    assertEquals("ipsum", ttmlCue.text.toString());
    assertEquals(40.f / 100.f, ttmlCue.position);
    assertEquals(40.f / 100.f, ttmlCue.line);
    assertEquals(20.f / 100.f, ttmlCue.size);
    output = subtitle.getCues(9000000);
    assertEquals(1, output.size());
    ttmlCue = output.get(0);
    assertEquals("dolor", ttmlCue.text.toString());
    assertEquals(10.f / 100.f, ttmlCue.position);
    assertEquals(80.f / 100.f, ttmlCue.line);
    assertEquals(Cue.DIMEN_UNSET, ttmlCue.size);
    output = subtitle.getCues(21000000);
    assertEquals(1, output.size());
    ttmlCue = output.get(0);
    assertEquals("She first said this", ttmlCue.text.toString());
    assertEquals(45.f / 100.f, ttmlCue.position);
    assertEquals(45.f / 100.f, ttmlCue.line);
    assertEquals(35.f / 100.f, ttmlCue.size);
    output = subtitle.getCues(25000000);
    ttmlCue = output.get(0);
    assertEquals("She first said this\nThen this", ttmlCue.text.toString());
    output = subtitle.getCues(29000000);
    assertEquals(1, output.size());
    ttmlCue = output.get(0);
    assertEquals("She first said this\nThen this\nFinally this", ttmlCue.text.toString());
    assertEquals(45.f / 100.f, ttmlCue.position);
    assertEquals(45.f / 100.f, ttmlCue.line);
}