private static HlsMediaPlaylist parseMediaPlaylist(LineIterator iterator, Uri baseUri) throws IOException { int mediaSequence = 0; int targetDurationSecs = 0; int version = 1; // Default version == 1. boolean live = true; List<Segment> segments = new ArrayList<Segment>(); double segmentDurationSecs = 0.0; boolean segmentDiscontinuity = false; long segmentStartTimeUs = 0; String segmentEncryptionMethod = null; String segmentEncryptionKeyUri = null; String segmentEncryptionIV = null; int segmentByterangeOffset = 0; int segmentByterangeLength = C.LENGTH_UNBOUNDED; int segmentMediaSequence = 0; String line; while (iterator.hasNext()) { line = iterator.next(); if (line.startsWith(TARGET_DURATION_TAG)) { targetDurationSecs = HlsParserUtil.parseIntAttr(line, TARGET_DURATION_REGEX, TARGET_DURATION_TAG); } else if (line.startsWith(MEDIA_SEQUENCE_TAG)) { mediaSequence = HlsParserUtil.parseIntAttr(line, MEDIA_SEQUENCE_REGEX, MEDIA_SEQUENCE_TAG); segmentMediaSequence = mediaSequence; } else if (line.startsWith(VERSION_TAG)) { version = HlsParserUtil.parseIntAttr(line, VERSION_REGEX, VERSION_TAG); } else if (line.startsWith(MEDIA_DURATION_TAG)) { segmentDurationSecs = HlsParserUtil.parseDoubleAttr(line, MEDIA_DURATION_REGEX, MEDIA_DURATION_TAG); } else if (line.startsWith(KEY_TAG)) { segmentEncryptionMethod = HlsParserUtil.parseStringAttr(line, METHOD_ATTR_REGEX, METHOD_ATTR); if (segmentEncryptionMethod.equals(HlsMediaPlaylist.ENCRYPTION_METHOD_NONE)) { segmentEncryptionKeyUri = null; segmentEncryptionIV = null; } else { segmentEncryptionKeyUri = HlsParserUtil.parseStringAttr(line, URI_ATTR_REGEX, URI_ATTR); segmentEncryptionIV = HlsParserUtil.parseOptionalStringAttr(line, IV_ATTR_REGEX); if (segmentEncryptionIV == null) { segmentEncryptionIV = Integer.toHexString(segmentMediaSequence); } } } else if (line.startsWith(BYTERANGE_TAG)) { String byteRange = HlsParserUtil.parseStringAttr(line, BYTERANGE_REGEX, BYTERANGE_TAG); String[] splitByteRange = byteRange.split("@"); segmentByterangeLength = Integer.parseInt(splitByteRange[0]); if (splitByteRange.length > 1) { segmentByterangeOffset = Integer.parseInt(splitByteRange[1]); } } else if (line.equals(DISCONTINUITY_TAG)) { segmentDiscontinuity = true; } else if (!line.startsWith("#")) { segmentMediaSequence++; if (segmentByterangeLength == C.LENGTH_UNBOUNDED) { segmentByterangeOffset = 0; } segments.add(new Segment(line, segmentDurationSecs, segmentDiscontinuity, segmentStartTimeUs, segmentEncryptionMethod, segmentEncryptionKeyUri, segmentEncryptionIV, segmentByterangeOffset, segmentByterangeLength)); segmentStartTimeUs += (long) (segmentDurationSecs * C.MICROS_PER_SECOND); segmentDiscontinuity = false; segmentDurationSecs = 0.0; if (segmentByterangeLength != C.LENGTH_UNBOUNDED) { segmentByterangeOffset += segmentByterangeLength; } segmentByterangeLength = C.LENGTH_UNBOUNDED; } else if (line.equals(ENDLIST_TAG)) { live = false; break; } } return new HlsMediaPlaylist(baseUri, mediaSequence, targetDurationSecs, version, live, Collections.unmodifiableList(segments)); }