diff --git a/library/src/main/java/com/google/android/exoplayer/chunk/VideoFormatSelectorUtil.java b/library/src/main/java/com/google/android/exoplayer/chunk/VideoFormatSelectorUtil.java index 0866382fc7..f88d9076be 100644 --- a/library/src/main/java/com/google/android/exoplayer/chunk/VideoFormatSelectorUtil.java +++ b/library/src/main/java/com/google/android/exoplayer/chunk/VideoFormatSelectorUtil.java @@ -132,7 +132,7 @@ public final class VideoFormatSelectorUtil { } } - return Util.toArray(selectedIndexList); + return Util.toIntArray(selectedIndexList); } /** diff --git a/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripCue.java b/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripCue.java deleted file mode 100644 index a464316b00..0000000000 --- a/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripCue.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer.text.subrip; - -import com.google.android.exoplayer.text.Cue; - -/** - * A representation of a SubRip cue. - */ -/* package */ final class SubripCue extends Cue { - - public final long startTime; - public final long endTime; - - public SubripCue(long startTime, long endTime, CharSequence text) { - super(text); - this.startTime = startTime; - this.endTime = endTime; - } - -} diff --git a/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripParser.java b/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripParser.java index e633cdc9b3..c1261ba132 100644 --- a/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripParser.java +++ b/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripParser.java @@ -17,8 +17,10 @@ package com.google.android.exoplayer.text.subrip; import com.google.android.exoplayer.C; import com.google.android.exoplayer.ParserException; +import com.google.android.exoplayer.text.Cue; import com.google.android.exoplayer.text.SubtitleParser; import com.google.android.exoplayer.util.MimeTypes; +import com.google.android.exoplayer.util.Util; import android.text.Html; import android.text.Spanned; @@ -52,7 +54,8 @@ public final class SubripParser implements SubtitleParser { @Override public SubripSubtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs) throws IOException { - ArrayList cues = new ArrayList<>(); + ArrayList cues = new ArrayList<>(); + ArrayList cueTimesUs = new ArrayList<>(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, C.UTF8_NAME)); String currentLine; @@ -65,13 +68,11 @@ public final class SubripParser implements SubtitleParser { } // Read and parse the timing line. - long cueStartTimeUs; - long cueEndTimeUs; currentLine = reader.readLine(); Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine); if (matcher.find()) { - cueStartTimeUs = parseTimestampUs(matcher.group(1)) + startTimeUs; - cueEndTimeUs = parseTimestampUs(matcher.group(2)) + startTimeUs; + cueTimesUs.add(startTimeUs + parseTimestampUs(matcher.group(1))); + cueTimesUs.add(startTimeUs + parseTimestampUs(matcher.group(2))); } else { throw new ParserException("Expected timing line: " + currentLine); } @@ -86,14 +87,16 @@ public final class SubripParser implements SubtitleParser { } Spanned text = Html.fromHtml(textBuilder.toString()); - SubripCue cue = new SubripCue(cueStartTimeUs, cueEndTimeUs, text); - cues.add(cue); + cues.add(new Cue(text)); } reader.close(); inputStream.close(); - SubripSubtitle subtitle = new SubripSubtitle(cues, startTimeUs); - return subtitle; + + Cue[] cuesArray = new Cue[cues.size()]; + cues.toArray(cuesArray); + long[] cueTimesUsArray = Util.toLongArray(cueTimesUs); + return new SubripSubtitle(startTimeUs, cuesArray, cueTimesUsArray); } @Override diff --git a/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripSubtitle.java b/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripSubtitle.java index 65282388be..9931a1490f 100644 --- a/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripSubtitle.java +++ b/library/src/main/java/com/google/android/exoplayer/text/subrip/SubripSubtitle.java @@ -28,27 +28,20 @@ import java.util.List; */ /* package */ final class SubripSubtitle implements Subtitle { - private final List cues; - private final int numCues; private final long startTimeUs; + + private final Cue[] cues; private final long[] cueTimesUs; /** - * @param cues A list of the cues in this subtitle. - * @param startTimeUs The start time of the subtitle. + * @param startTimeUs The start time of the subtitle, in microseconds. + * @param cues The cues in the subtitle. + * @param cueTimesUs Interleaved cue start and end times, in microseconds. */ - public SubripSubtitle(List cues, long startTimeUs) { - this.cues = cues; + public SubripSubtitle(long startTimeUs, Cue[] cues, long[] cueTimesUs) { this.startTimeUs = startTimeUs; - - numCues = cues.size(); - cueTimesUs = new long[2 * numCues]; - for (int cueIndex = 0; cueIndex < numCues; cueIndex++) { - SubripCue cue = cues.get(cueIndex); - int arrayIndex = cueIndex * 2; - cueTimesUs[arrayIndex] = cue.startTime; - cueTimesUs[arrayIndex + 1] = cue.endTime; - } + this.cues = cues; + this.cueTimesUs = cueTimesUs; } @Override @@ -58,7 +51,6 @@ import java.util.List; @Override public int getNextEventTimeIndex(long timeUs) { - Assertions.checkArgument(timeUs >= 0); int index = Util.binarySearchCeil(cueTimesUs, timeUs, false, false); return index < cueTimesUs.length ? index : -1; } @@ -90,7 +82,7 @@ import java.util.List; // timeUs is earlier than the start of the first cue, or corresponds to a gap between cues. return Collections.emptyList(); } else { - return Collections.singletonList((Cue) cues.get(index / 2)); + return Collections.singletonList(cues[index / 2]); } } diff --git a/library/src/main/java/com/google/android/exoplayer/util/Util.java b/library/src/main/java/com/google/android/exoplayer/util/Util.java index 65d8197792..c5bf9d808e 100644 --- a/library/src/main/java/com/google/android/exoplayer/util/Util.java +++ b/library/src/main/java/com/google/android/exoplayer/util/Util.java @@ -459,7 +459,7 @@ public final class Util { * @param list A list of integers. * @return The list in array form, or null if the input list was null. */ - public static int[] toArray(List list) { + public static int[] toIntArray(List list) { if (list == null) { return null; } @@ -471,6 +471,24 @@ public final class Util { return intArray; } + /** + * Converts a list of longs to a primitive array. + * + * @param list A list of longs. + * @return The list in array form, or null if the input list was null. + */ + public static long[] toLongArray(List list) { + if (list == null) { + return null; + } + int length = list.size(); + long[] longArray = new long[length]; + for (int i = 0; i < length; i++) { + longArray[i] = list.get(i); + } + return longArray; + } + /** * On platform API levels 19 and 20, okhttp's implementation of {@link InputStream#close} can * block for a long time if the stream has a lot of data remaining. Call this method before