androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
https://developer.android.com/media/media3
Apache License 2.0
1.68k stars 403 forks source link

When updating onTimeline, how to correctly get the EXT-X-PROGRAM-DATE-TIME of the currently playing TS file? #583

Open lguipeng opened 1 year ago

lguipeng commented 1 year ago

As described in the title

marcbaechinger commented 1 year ago

If the media playlist of the HLS stream has the #EXT-X-PROGRAM-DATE-TIME set, then you can inspect the playlist to get the start time of the playlist and calculate the start time of each segement:

@Override
public void onTimelineChanged(Timeline timeline, int reason) {
  int windowIndexOfHlsLiveStream = 0;
  Object manifest =
      timeline.getWindow(windowIndexOfHlsLiveStream, new Timeline.Window()).manifest;
  if (manifest instanceof HlsManifest) {
    HlsMediaPlaylist mediaPlaylist = ((HlsManifest) manifest).mediaPlaylist;
    long segmentStartUs = mediaPlaylist.startTimeUs;
    long mediaSequence = mediaPlaylist.mediaSequence;
    Log.d("timeline", "playlist startTimeUs: " + segmentStartUs);
    for (int i = 0; i < mediaPlaylist.segments.size(); i++) {
      HlsMediaPlaylist.Segment segment = mediaPlaylist.segments.get(i);
      Log.d("timeline", "segment with sequence " + (mediaSequence++) + " starts at: " + segmentStartUs);
      segmentStartUs += segment.durationUs;
    }
  }
}

I don't think there is an API to get the currently playing TS segment though.