Harmon Skiles
Harmon Skiles

Reputation: 21

How to Parse M3U8 file in Android

I need to parse m3u8 content in android studio. I tried several times using github library such as open-m3u8. But I can not parse this. Please resolve this problem for me.

Thanks.

I tried several times using github library such as open-m3u8. But that tells bad format. I am not sure for this exception error.

EXTM3U

EXTINF:-1 tvg-id="beIN Movies 1 HD" tvg-name="BEIN MOVIE 1" tvg-logo="https://4.bp.blogspot.com/-lLLGwGe0SU0/VvfF-Wf-PgI/AAAAAAAAD1w/4wuF8M2X9YsckWaAfPSXdIPTDVgcPaSnQ/s1600/be_in_movies_1_hd.png" group-title="Bein ENT - OSN",BEIN MOVIE 1 http://maxtvv.abdou123.com:8080/live/localhd/211VGH699/1324.m3u8

EXTINF:-1 tvg-id="beIN Movies 2 HD" tvg-name="BEIN MOVIE 2" tvg-logo="https://4.bp.blogspot.com/-QUXStVW8y4c/WDT5VaV7I0I/AAAAAAAAC20/7X43vlEpcDoZPINfDi3MonZ-LpPcsaa-QCLcB/s1600/Movies2HD.jpg" group-title="Bein ENT - OSN",BEIN MOVIE 2 http://maxtvv.abdou123.com:8080/live/localhd/211VGH699/1323.m3u8

EXTINF:-1 tvg-id="beIN Movies 3 HD" tvg-name="BEIN MOVIE 3" tvg-logo="http://aya.sy/images/services/iptv/Bein_Movies_3.png" group-title="Bein ENT - OSN",BEIN MOVIE 3 http://maxtvv.abdou123.com:8080/live/localhd/211VGH699/1322.m3u8

EXTINF:-1 tvg-id="beIN Movies 4 HD" tvg-name="BEIN MOVIE 4" tvg-logo="http://aya.sy/images/services/iptv/Bein_Movies_4.png" group-title="Bein ENT - OSN",BEIN MOVIE 4 http://maxtvv.abdou123.com:8080/live/localhd/211VGH699/1321.m3u8

I should get 4 channel data through this parse.

Upvotes: 0

Views: 5290

Answers (1)

Mick
Mick

Reputation: 25481

ExoPlayer includes a m3u8 HLS Play List parser and the source is available:

The top level looks like this - you can see it iterates over each line in the m3u8 file, sometimes referred to as the manifest, and determines the type of information in that line to allow it interpret each line correctly:

@Override
  public HlsPlaylist parse(Uri uri, InputStream inputStream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    Queue<String> extraLines = new ArrayDeque<>();
    String line;
    try {
      if (!checkPlaylistHeader(reader)) {
        throw new UnrecognizedInputFormatException("Input does not start with the #EXTM3U header.",
            uri);
      }
      while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.isEmpty()) {
          // Do nothing.
        } else if (line.startsWith(TAG_STREAM_INF)) {
          extraLines.add(line);
          return parseMasterPlaylist(new LineIterator(extraLines, reader), uri.toString());
        } else if (line.startsWith(TAG_TARGET_DURATION)
            || line.startsWith(TAG_MEDIA_SEQUENCE)
            || line.startsWith(TAG_MEDIA_DURATION)
            || line.startsWith(TAG_KEY)
            || line.startsWith(TAG_BYTERANGE)
            || line.equals(TAG_DISCONTINUITY)
            || line.equals(TAG_DISCONTINUITY_SEQUENCE)
            || line.equals(TAG_ENDLIST)) {
          extraLines.add(line);
          return parseMediaPlaylist(
              masterPlaylist, new LineIterator(extraLines, reader), uri.toString());
        } else {
          extraLines.add(line);
        }
      }
    } finally {
      Util.closeQuietly(reader);
    }
    throw new ParserException("Failed to parse the playlist, could not identify any tags.");
  }

Upvotes: 2

Related Questions