Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to align durations of MergingMediaSource elements #8422

Closed
joel1st opened this issue Jan 2, 2021 · 2 comments
Closed

Add option to align durations of MergingMediaSource elements #8422

joel1st opened this issue Jan 2, 2021 · 2 comments
Assignees

Comments

@joel1st
Copy link

joel1st commented Jan 2, 2021

I want to set up a default audio clip to play for video playback when there is no audio track present. I want this default audio source to play for the same duration as the video (downloaded mp4 file) MediaSource. My first thought was to use a ClippingMediaSource + MergingMediaSource, but the nature of this application means I do not know what the precise video duration is (unless there is a performant way to easily calculate this without playing the file). If there was a way to dynamically update the ClippingMediaSource duration this could work, but I have not found a way.

So I guess there are two questions:
Is there a way to dynamically update the duration of a ClippingMediaSource?
or is there a way to set the ClippingMediaSource duration to the be same duration as another MediaSource?

@andrewlewis
Copy link
Collaborator

If the media is downloaded, loading it to find its duration is not going to be excessively inefficient so that might be fine for now. Whether it's performant enough will depend on your specific requirements I guess.

#3163 tracks having a way to update the clipping start/end points dynamically.

@tonihei Please could you take a look and add or correct anything? Thanks.

@andrewlewis andrewlewis assigned tonihei and unassigned andrewlewis Jan 5, 2021
@tonihei
Copy link
Collaborator

tonihei commented Jan 5, 2021

That's pretty much it:

  • Loading a downloaded or local file to find its duration is not super inefficient and a good workaround if required:
Player player = new SimpleExoPlayer.Builder(context).build();
player.setMediaItem(MediaItem.fromUri(localFileUri));
player.addListener(new EventListener() {
  @Override
  public void onTimelineChanged(Timeline timeline, int reason) {
    if (!timeline.isEmpty()) {
      long durationMs = timeline.getWindow(0, new Window().getDurationMs;
      if (durationMs != C.TIME_UNSET) {
        player.release();
        reportDuration(durationMs);
      }
    } 
  }

  @Override 
  public void onPlayerError(ExoPlaybackException error) {
    player.release();
    reportDurationExtractionFailed();
  }
};
player.prepare()
  • Updating the clipped duration is tracked by Allow end user to trim media in playlist #3163, but there are no immediate plans to finish this feature.

  • Clipping elements in a MergingMediaSource to match the duration of the shortest merged source is a sensible feature request. We already have a feature to ensure the timestamps of merged sources match, so it also makes sense to match the durations if required. Marking as an enhancement accordingly.

@tonihei tonihei changed the title How to clip the second source to be the same duration as the first source with a MergingMediaSource? Add option to align durations of MergingMediaSource elements Jan 5, 2021
icbaker pushed a commit that referenced this issue Jan 8, 2021
Without this feature it's impossible to nicely merge multiple sources
with different durations if these durations are not known exactly
before the start of playback.

Issue: #8422
PiperOrigin-RevId: 350567625
@ojw28 ojw28 closed this as completed Feb 4, 2021
@google google locked and limited conversation to collaborators Apr 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants