Java Code Examples for com.google.android.exoplayer2.util.LongArray#add()

The following examples show how to use com.google.android.exoplayer2.util.LongArray#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubripDecoder.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
protected Subtitle decode(byte[] bytes, int length, boolean reset) {
  ArrayList<Cue> cues = new ArrayList<>();
  LongArray cueTimesUs = new LongArray();
  ParsableByteArray subripData = new ParsableByteArray(bytes, length);

  @Nullable 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.
    currentLine = subripData.readLine();
    if (currentLine == null) {
      Log.w(TAG, "Unexpected end");
      break;
    }

    Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine);
    if (matcher.matches()) {
      cueTimesUs.add(parseTimecode(matcher, /* groupOffset= */ 1));
      cueTimesUs.add(parseTimecode(matcher, /* groupOffset= */ 6));
    } else {
      Log.w(TAG, "Skipping invalid timing: " + currentLine);
      continue;
    }

    // Read and parse the text and tags.
    textBuilder.setLength(0);
    tags.clear();
    currentLine = subripData.readLine();
    while (!TextUtils.isEmpty(currentLine)) {
      if (textBuilder.length() > 0) {
        textBuilder.append("<br>");
      }
      textBuilder.append(processLine(currentLine, tags));
      currentLine = subripData.readLine();
    }

    Spanned text = Html.fromHtml(textBuilder.toString());

    @Nullable String alignmentTag = null;
    for (int i = 0; i < tags.size(); i++) {
      String tag = tags.get(i);
      if (tag.matches(SUBRIP_ALIGNMENT_TAG)) {
        alignmentTag = tag;
        // Subsequent alignment tags should be ignored.
        break;
      }
    }
    cues.add(buildCue(text, alignmentTag));
    cues.add(Cue.EMPTY);
  }

  Cue[] cuesArray = new Cue[cues.size()];
  cues.toArray(cuesArray);
  long[] cueTimesUsArray = cueTimesUs.toArray();
  return new SubripSubtitle(cuesArray, cueTimesUsArray);
}
 
Example 2
Source File: SsaDecoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a dialogue line.
 *
 * @param dialogueLine The line to parse.
 * @param cues A list to which parsed cues will be added.
 * @param cueTimesUs An array to which parsed cue timestamps will be added.
 */
private void parseDialogueLine(String dialogueLine, List<Cue> cues, LongArray cueTimesUs) {
  if (formatKeyCount == 0) {
    Log.w(TAG, "Skipping dialogue line before complete format: " + dialogueLine);
    return;
  }

  String[] lineValues = dialogueLine.substring(DIALOGUE_LINE_PREFIX.length())
      .split(",", formatKeyCount);
  if (lineValues.length != formatKeyCount) {
    Log.w(TAG, "Skipping dialogue line with fewer columns than format: " + dialogueLine);
    return;
  }

  long startTimeUs = SsaDecoder.parseTimecodeUs(lineValues[formatStartIndex]);
  if (startTimeUs == C.TIME_UNSET) {
    Log.w(TAG, "Skipping invalid timing: " + dialogueLine);
    return;
  }

  long endTimeUs = C.TIME_UNSET;
  String endTimeString = lineValues[formatEndIndex];
  if (!endTimeString.trim().isEmpty()) {
    endTimeUs = SsaDecoder.parseTimecodeUs(endTimeString);
    if (endTimeUs == C.TIME_UNSET) {
      Log.w(TAG, "Skipping invalid timing: " + dialogueLine);
      return;
    }
  }

  String text = lineValues[formatTextIndex]
      .replaceAll("\\{.*?\\}", "")
      .replaceAll("\\\\N", "\n")
      .replaceAll("\\\\n", "\n");
  cues.add(new Cue(text));
  cueTimesUs.add(startTimeUs);
  if (endTimeUs != C.TIME_UNSET) {
    cues.add(null);
    cueTimesUs.add(endTimeUs);
  }
}
 
Example 3
Source File: SubripDecoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected SubripSubtitle decode(byte[] bytes, int length, boolean reset) {
  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();
    if (currentLine == null) {
      Log.w(TAG, "Unexpected end");
      break;
    }

    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);
}
 
Example 4
Source File: SsaDecoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a dialogue line.
 *
 * @param dialogueLine The line to parse.
 * @param cues A list to which parsed cues will be added.
 * @param cueTimesUs An array to which parsed cue timestamps will be added.
 */
private void parseDialogueLine(String dialogueLine, List<Cue> cues, LongArray cueTimesUs) {
  if (formatKeyCount == 0) {
    Log.w(TAG, "Skipping dialogue line before complete format: " + dialogueLine);
    return;
  }

  String[] lineValues = dialogueLine.substring(DIALOGUE_LINE_PREFIX.length())
      .split(",", formatKeyCount);
  if (lineValues.length != formatKeyCount) {
    Log.w(TAG, "Skipping dialogue line with fewer columns than format: " + dialogueLine);
    return;
  }

  long startTimeUs = SsaDecoder.parseTimecodeUs(lineValues[formatStartIndex]);
  if (startTimeUs == C.TIME_UNSET) {
    Log.w(TAG, "Skipping invalid timing: " + dialogueLine);
    return;
  }

  long endTimeUs = C.TIME_UNSET;
  String endTimeString = lineValues[formatEndIndex];
  if (!endTimeString.trim().isEmpty()) {
    endTimeUs = SsaDecoder.parseTimecodeUs(endTimeString);
    if (endTimeUs == C.TIME_UNSET) {
      Log.w(TAG, "Skipping invalid timing: " + dialogueLine);
      return;
    }
  }

  String text = lineValues[formatTextIndex]
      .replaceAll("\\{.*?\\}", "")
      .replaceAll("\\\\N", "\n")
      .replaceAll("\\\\n", "\n");
  cues.add(new Cue(text));
  cueTimesUs.add(startTimeUs);
  if (endTimeUs != C.TIME_UNSET) {
    cues.add(null);
    cueTimesUs.add(endTimeUs);
  }
}
 
Example 5
Source File: SubripDecoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected SubripSubtitle decode(byte[] bytes, int length, boolean reset) {
  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();
    if (currentLine == null) {
      Log.w(TAG, "Unexpected end");
      break;
    }

    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);
}
 
Example 6
Source File: SubripDecoder.java    From K-Sonic with MIT License 4 votes vote down vote up
@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);
}
 
Example 7
Source File: SsaDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a dialogue line.
 *
 * @param dialogueLine The line to parse.
 * @param cues A list to which parsed cues will be added.
 * @param cueTimesUs An array to which parsed cue timestamps will be added.
 */
private void parseDialogueLine(String dialogueLine, List<Cue> cues, LongArray cueTimesUs) {
  if (formatKeyCount == 0) {
    Log.w(TAG, "Skipping dialogue line before complete format: " + dialogueLine);
    return;
  }

  String[] lineValues = dialogueLine.substring(DIALOGUE_LINE_PREFIX.length())
      .split(",", formatKeyCount);
  if (lineValues.length != formatKeyCount) {
    Log.w(TAG, "Skipping dialogue line with fewer columns than format: " + dialogueLine);
    return;
  }

  long startTimeUs = SsaDecoder.parseTimecodeUs(lineValues[formatStartIndex]);
  if (startTimeUs == C.TIME_UNSET) {
    Log.w(TAG, "Skipping invalid timing: " + dialogueLine);
    return;
  }

  long endTimeUs = C.TIME_UNSET;
  String endTimeString = lineValues[formatEndIndex];
  if (!endTimeString.trim().isEmpty()) {
    endTimeUs = SsaDecoder.parseTimecodeUs(endTimeString);
    if (endTimeUs == C.TIME_UNSET) {
      Log.w(TAG, "Skipping invalid timing: " + dialogueLine);
      return;
    }
  }

  String text = lineValues[formatTextIndex]
      .replaceAll("\\{.*?\\}", "")
      .replaceAll("\\\\N", "\n")
      .replaceAll("\\\\n", "\n");
  cues.add(new Cue(text));
  cueTimesUs.add(startTimeUs);
  if (endTimeUs != C.TIME_UNSET) {
    cues.add(Cue.EMPTY);
    cueTimesUs.add(endTimeUs);
  }
}
 
Example 8
Source File: SubripDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected SubripSubtitle decode(byte[] bytes, int length, boolean reset) {
  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();
    if (currentLine == null) {
      Log.w(TAG, "Unexpected end");
      break;
    }

    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 and tags.
    textBuilder.setLength(0);
    tags.clear();
    currentLine = subripData.readLine();
    while (!TextUtils.isEmpty(currentLine)) {
      if (textBuilder.length() > 0) {
        textBuilder.append("<br>");
      }
      textBuilder.append(processLine(currentLine, tags));
      currentLine = subripData.readLine();
    }

    Spanned text = Html.fromHtml(textBuilder.toString());

    String alignmentTag = null;
    for (int i = 0; i < tags.size(); i++) {
      String tag = tags.get(i);
      if (tag.matches(SUBRIP_ALIGNMENT_TAG)) {
        alignmentTag = tag;
        // Subsequent alignment tags should be ignored.
        break;
      }
    }
    cues.add(buildCue(text, alignmentTag));

    if (haveEndTimecode) {
      cues.add(Cue.EMPTY);
    }
  }

  Cue[] cuesArray = new Cue[cues.size()];
  cues.toArray(cuesArray);
  long[] cueTimesUsArray = cueTimesUs.toArray();
  return new SubripSubtitle(cuesArray, cueTimesUsArray);
}
 
Example 9
Source File: SsaDecoder.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a dialogue line.
 *
 * @param dialogueLine The line to parse.
 * @param cues A list to which parsed cues will be added.
 * @param cueTimesUs An array to which parsed cue timestamps will be added.
 */
private void parseDialogueLine(String dialogueLine, List<Cue> cues, LongArray cueTimesUs) {
  if (formatKeyCount == 0) {
    Log.w(TAG, "Skipping dialogue line before complete format: " + dialogueLine);
    return;
  }

  String[] lineValues = dialogueLine.substring(DIALOGUE_LINE_PREFIX.length())
      .split(",", formatKeyCount);
  if (lineValues.length != formatKeyCount) {
    Log.w(TAG, "Skipping dialogue line with fewer columns than format: " + dialogueLine);
    return;
  }

  long startTimeUs = SsaDecoder.parseTimecodeUs(lineValues[formatStartIndex]);
  if (startTimeUs == C.TIME_UNSET) {
    Log.w(TAG, "Skipping invalid timing: " + dialogueLine);
    return;
  }

  long endTimeUs = C.TIME_UNSET;
  String endTimeString = lineValues[formatEndIndex];
  if (!endTimeString.trim().isEmpty()) {
    endTimeUs = SsaDecoder.parseTimecodeUs(endTimeString);
    if (endTimeUs == C.TIME_UNSET) {
      Log.w(TAG, "Skipping invalid timing: " + dialogueLine);
      return;
    }
  }

  String text = lineValues[formatTextIndex]
      .replaceAll("\\{.*?\\}", "")
      .replaceAll("\\\\N", "\n")
      .replaceAll("\\\\n", "\n");
  cues.add(new Cue(text));
  cueTimesUs.add(startTimeUs);
  if (endTimeUs != C.TIME_UNSET) {
    cues.add(Cue.EMPTY);
    cueTimesUs.add(endTimeUs);
  }
}
 
Example 10
Source File: SubripDecoder.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected SubripSubtitle decode(byte[] bytes, int length, boolean reset) {
  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();
    if (currentLine == null) {
      Log.w(TAG, "Unexpected end");
      break;
    }

    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 and tags.
    textBuilder.setLength(0);
    tags.clear();
    currentLine = subripData.readLine();
    while (!TextUtils.isEmpty(currentLine)) {
      if (textBuilder.length() > 0) {
        textBuilder.append("<br>");
      }
      textBuilder.append(processLine(currentLine, tags));
      currentLine = subripData.readLine();
    }

    Spanned text = Html.fromHtml(textBuilder.toString());

    String alignmentTag = null;
    for (int i = 0; i < tags.size(); i++) {
      String tag = tags.get(i);
      if (tag.matches(SUBRIP_ALIGNMENT_TAG)) {
        alignmentTag = tag;
        // Subsequent alignment tags should be ignored.
        break;
      }
    }
    cues.add(buildCue(text, alignmentTag));

    if (haveEndTimecode) {
      cues.add(Cue.EMPTY);
    }
  }

  Cue[] cuesArray = new Cue[cues.size()];
  cues.toArray(cuesArray);
  long[] cueTimesUsArray = cueTimesUs.toArray();
  return new SubripSubtitle(cuesArray, cueTimesUsArray);
}