Search in sources :

Example 71 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class SsaDecoder method createCue.

private static Cue createCue(String text, @Nullable SsaStyle style, SsaStyle.Overrides styleOverrides, float screenWidth, float screenHeight) {
    SpannableString spannableText = new SpannableString(text);
    Cue.Builder cue = new Cue.Builder().setText(spannableText);
    if (style != null) {
        if (style.primaryColor != null) {
            spannableText.setSpan(new ForegroundColorSpan(style.primaryColor), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (style.fontSize != Cue.DIMEN_UNSET && screenHeight != Cue.DIMEN_UNSET) {
            cue.setTextSize(style.fontSize / screenHeight, Cue.TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING);
        }
        if (style.bold && style.italic) {
            spannableText.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (style.bold) {
            spannableText.setSpan(new StyleSpan(Typeface.BOLD), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (style.italic) {
            spannableText.setSpan(new StyleSpan(Typeface.ITALIC), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (style.underline) {
            spannableText.setSpan(new UnderlineSpan(), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (style.strikeout) {
            spannableText.setSpan(new StrikethroughSpan(), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    @SsaStyle.SsaAlignment int alignment;
    if (styleOverrides.alignment != SsaStyle.SSA_ALIGNMENT_UNKNOWN) {
        alignment = styleOverrides.alignment;
    } else if (style != null) {
        alignment = style.alignment;
    } else {
        alignment = SsaStyle.SSA_ALIGNMENT_UNKNOWN;
    }
    cue.setTextAlignment(toTextAlignment(alignment)).setPositionAnchor(toPositionAnchor(alignment)).setLineAnchor(toLineAnchor(alignment));
    if (styleOverrides.position != null && screenHeight != Cue.DIMEN_UNSET && screenWidth != Cue.DIMEN_UNSET) {
        cue.setPosition(styleOverrides.position.x / screenWidth);
        cue.setLine(styleOverrides.position.y / screenHeight, LINE_TYPE_FRACTION);
    } else {
        // TODO: Read the MarginL, MarginR and MarginV values from the Style & Dialogue lines.
        cue.setPosition(computeDefaultLineOrPosition(cue.getPositionAnchor()));
        cue.setLine(computeDefaultLineOrPosition(cue.getLineAnchor()), LINE_TYPE_FRACTION);
    }
    return cue.build();
}
Also used : SpannableString(android.text.SpannableString) Cue(com.google.android.exoplayer2.text.Cue) ForegroundColorSpan(android.text.style.ForegroundColorSpan) StyleSpan(android.text.style.StyleSpan) UnderlineSpan(android.text.style.UnderlineSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 72 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class SubtitleViewUtilsTest method testRemoveAllEmbeddedStyling.

@Test
public void testRemoveAllEmbeddedStyling() {
    Cue.Builder cueBuilder = CUE.buildUpon();
    SubtitleViewUtils.removeAllEmbeddedStyling(cueBuilder);
    Cue strippedCue = cueBuilder.build();
    Spanned originalText = (Spanned) CUE.text;
    Spanned strippedText = (Spanned) strippedCue.text;
    // Assert all non styling properties and spans are kept
    assertThat(strippedCue.textAlignment).isEqualTo(CUE.textAlignment);
    assertThat(strippedCue.multiRowAlignment).isEqualTo(CUE.multiRowAlignment);
    assertThat(strippedCue.line).isEqualTo(CUE.line);
    assertThat(strippedCue.lineType).isEqualTo(CUE.lineType);
    assertThat(strippedCue.position).isEqualTo(CUE.position);
    assertThat(strippedCue.positionAnchor).isEqualTo(CUE.positionAnchor);
    assertThat(strippedCue.textSize).isEqualTo(Cue.DIMEN_UNSET);
    assertThat(strippedCue.textSizeType).isEqualTo(Cue.TYPE_UNSET);
    assertThat(strippedCue.size).isEqualTo(CUE.size);
    assertThat(strippedCue.verticalType).isEqualTo(CUE.verticalType);
    assertThat(strippedCue.shearDegrees).isEqualTo(CUE.shearDegrees);
    TextEmphasisSpan expectedTextEmphasisSpan = originalText.getSpans(0, originalText.length(), TextEmphasisSpan.class)[0];
    assertThat(strippedText).hasTextEmphasisSpanBetween(originalText.getSpanStart(expectedTextEmphasisSpan), originalText.getSpanEnd(expectedTextEmphasisSpan));
    RubySpan expectedRubySpan = originalText.getSpans(0, originalText.length(), RubySpan.class)[0];
    assertThat(strippedText).hasRubySpanBetween(originalText.getSpanStart(expectedRubySpan), originalText.getSpanEnd(expectedRubySpan));
    HorizontalTextInVerticalContextSpan expectedHorizontalTextInVerticalContextSpan = originalText.getSpans(0, originalText.length(), HorizontalTextInVerticalContextSpan.class)[0];
    assertThat(strippedText).hasHorizontalTextInVerticalContextSpanBetween(originalText.getSpanStart(expectedHorizontalTextInVerticalContextSpan), originalText.getSpanEnd(expectedHorizontalTextInVerticalContextSpan));
    // Assert all styling properties and spans are removed
    assertThat(strippedCue.windowColorSet).isFalse();
    assertThat(strippedText).hasNoUnderlineSpanBetween(0, strippedText.length());
    assertThat(strippedText).hasNoRelativeSizeSpanBetween(0, strippedText.length());
    assertThat(strippedText).hasNoAbsoluteSizeSpanBetween(0, strippedText.length());
}
Also used : RubySpan(com.google.android.exoplayer2.text.span.RubySpan) Cue(com.google.android.exoplayer2.text.Cue) HorizontalTextInVerticalContextSpan(com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan) TextEmphasisSpan(com.google.android.exoplayer2.text.span.TextEmphasisSpan) Spanned(android.text.Spanned) Test(org.junit.Test)

Example 73 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class WebViewSubtitleOutput method update.

@Override
public void update(List<Cue> cues, CaptionStyleCompat style, float textSize, @Cue.TextSizeType int textSizeType, float bottomPaddingFraction) {
    this.style = style;
    this.defaultTextSize = textSize;
    this.defaultTextSizeType = textSizeType;
    this.bottomPaddingFraction = bottomPaddingFraction;
    List<Cue> bitmapCues = new ArrayList<>();
    List<Cue> textCues = new ArrayList<>();
    for (int i = 0; i < cues.size(); i++) {
        Cue cue = cues.get(i);
        if (cue.bitmap != null) {
            bitmapCues.add(cue);
        } else {
            textCues.add(cue);
        }
    }
    if (!this.textCues.isEmpty() || !textCues.isEmpty()) {
        this.textCues = textCues;
        // Skip updating if this is a transition from empty-cues to empty-cues (i.e. only positioning
        // info has changed) since a positional-only change with no cues is a visual no-op. The new
        // position info will be used when we get non-empty cue data in a future update() call.
        updateWebView();
    }
    canvasSubtitleOutput.update(bitmapCues, style, textSize, textSizeType, bottomPaddingFraction);
    // Invalidate to trigger canvasSubtitleOutput to draw.
    invalidate();
}
Also used : Cue(com.google.android.exoplayer2.text.Cue) ArrayList(java.util.ArrayList)

Example 74 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class WebvttExtractorTest method read_handlesLargeCueTimestamps.

@Test
public void read_handlesLargeCueTimestamps() throws Exception {
    TimestampAdjuster timestampAdjuster = new TimestampAdjuster(/* firstSampleTimestampUs= */
    0);
    // Prime the TimestampAdjuster with a close-ish timestamp (5s before the first cue).
    timestampAdjuster.adjustTsTimestamp(384615190);
    WebvttExtractor extractor = new WebvttExtractor(/* language= */
    null, timestampAdjuster);
    // We can't use ExtractorAsserts because WebvttExtractor doesn't fulfill the whole Extractor
    // interface (e.g. throws an exception from seek()).
    FakeExtractorOutput output = TestUtil.extractAllSamplesFromFile(extractor, ApplicationProvider.getApplicationContext(), "media/webvtt/with_x-timestamp-map_header");
    // The output has a ~5s sampleTime and a large, negative subsampleOffset because the cue
    // timestamps are ~10 days ahead of the PTS (due to wrapping) so the offset is used to ensure
    // they're rendered at the right time.
    DumpFileAsserts.assertOutput(ApplicationProvider.getApplicationContext(), output, "extractordumps/webvtt/with_x-timestamp-map_header.dump");
}
Also used : TimestampAdjuster(com.google.android.exoplayer2.util.TimestampAdjuster) FakeExtractorOutput(com.google.android.exoplayer2.testutil.FakeExtractorOutput) Test(org.junit.Test)

Aggregations

Cue (com.google.android.exoplayer2.text.Cue)58 Test (org.junit.Test)40 Subtitle (com.google.android.exoplayer2.text.Subtitle)13 ArrayList (java.util.ArrayList)12 Nullable (androidx.annotation.Nullable)10 Spanned (android.text.Spanned)8 SpannableStringBuilder (android.text.SpannableStringBuilder)7 ParsableByteArray (com.google.android.exoplayer2.util.ParsableByteArray)5 FakeExtractorOutput (com.google.android.exoplayer2.testutil.FakeExtractorOutput)4 SpannableString (android.text.SpannableString)3 UnderlineSpan (android.text.style.UnderlineSpan)3 Format (com.google.android.exoplayer2.Format)3 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)3 FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)3 FakeTrackOutput (com.google.android.exoplayer2.testutil.FakeTrackOutput)3 HorizontalTextInVerticalContextSpan (com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan)3 RubySpan (com.google.android.exoplayer2.text.span.RubySpan)3 TextEmphasisSpan (com.google.android.exoplayer2.text.span.TextEmphasisSpan)3 WebvttDecoder (com.google.android.exoplayer2.text.webvtt.WebvttDecoder)3 Matcher (java.util.regex.Matcher)3