diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java b/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java index 4b413d2ad6..1f7f7eddf2 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java @@ -140,6 +140,7 @@ public final class WebvttCueParser { private static final String TAG = "WebvttCueParser"; private static final Map DEFAULT_COLORS; + private static final Map DEFAULT_BACKGROUND_COLORS; static { DEFAULT_COLORS = new HashMap<>(); @@ -151,6 +152,16 @@ public final class WebvttCueParser { DEFAULT_COLORS.put("red", 0xFFFF0000); DEFAULT_COLORS.put("white", 0xFFFFFFFF); DEFAULT_COLORS.put("yellow", 0xFFFFFF00); + + DEFAULT_BACKGROUND_COLORS = new HashMap<>(); + DEFAULT_BACKGROUND_COLORS.put("bg_black", 0xFF000000); + DEFAULT_BACKGROUND_COLORS.put("bg_blue", 0xFF0000FF); + DEFAULT_BACKGROUND_COLORS.put("bg_cyan", 0xFF00FFFF); + DEFAULT_BACKGROUND_COLORS.put("bg_lime", 0xFF00FF00); + DEFAULT_BACKGROUND_COLORS.put("bg_magenta", 0xFFFF00FF); + DEFAULT_BACKGROUND_COLORS.put("bg_red", 0xFFFF0000); + DEFAULT_BACKGROUND_COLORS.put("bg_white", 0xFFFFFFFF); + DEFAULT_BACKGROUND_COLORS.put("bg_yellow", 0xFFFFFF00); } /** @@ -551,9 +562,13 @@ public final class WebvttCueParser { int start, int end) { for (String className : classes) { if (DEFAULT_COLORS.containsKey(className)) { - int color = DEFAULT_COLORS.get(Util.toLowerInvariant(className)); + int color = DEFAULT_COLORS.get(className); text.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } + if (DEFAULT_BACKGROUND_COLORS.containsKey(className)) { + int color = DEFAULT_BACKGROUND_COLORS.get(className); + text.setSpan(new BackgroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } } } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java b/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java index ab8ac4ed9a..b263ba91c9 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java @@ -95,6 +95,22 @@ public final class WebvttCueParserTest { assertThat(text).hasNoSpans(); } + @Test + public void testBackgroundTextColorClass() throws Exception { + Spanned text = parseCueText("In this sentence this text has a red background"); + + assertThat(text.toString()).isEqualTo("In this sentence this text has a red background"); + assertThat(text).hasBackgroundColorSpanBetween("In this sentence ".length(), "In this sentence this text".length()); + } + + @Test + public void testUnsupportedColorForBackgroundTextColorClass() throws Exception { + Spanned text = parseCueText("In this sentence this text doesn't have a papaya background"); + + assertThat(text.toString()).isEqualTo("In this sentence this text doesn't have a papaya background"); + assertThat(text).hasNoSpans(); + } + @Test public void testParseWellFormedUnclosedEndAtCueEnd() throws Exception { Spanned text = parseCueText("An unclosed u tag with "