3

I am using the following code to play a stream (which has both a video and an audio). But what if I want a video stream and a separate audio stream for the video stream at the same time synchronously? For example, suppose there are

Both video.mp4 and audio.mp3 belong to video1 and have the same length, 1:23:34. Can I play video.mp4 and audio.mp3 synchronously as if they were one stream, with one ExoPlayer? The user must be able to pause/play/seek, and the same action should be applied to both streams.

If not, and I have to use two ExoPlayers, one for video and one for audio, how can I synchronise the audio and the video?

    var uri = Uri.parse(url);
    var df = DefaultHttpDataSourceFactory(url);
    var ms = ExtractorMediaSource(uri, df, DefaultExtractorsFactory(), null, null);
    exoPlayer.playWhenReady=true;
    exoPlayer.prepare(ms);
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

1 Answers1

6

You can! This is done in ExoPlayer by creating a MergingMediaSource. The example in the link merges a video source with a subtitle source but it's even easier for audio and video:

MediaSource videoSource = new ProgressiveMediaSource.Factory(...)
    .createMediaSource(videoUri);
MediaSource audioSource = new ProgressiveMediaSource.Factory(...)
    .createMediaSource(audioUri);

MergingMediaSource mergedSource = new MergingMediaSource(videoSource, audioSource);
Sandi
  • 2,415
  • 1
  • 13
  • 11
  • If I use this method to play `video.mp4` and `audio.mp3` concurrently, can I replace the `audio.mp3` with `audio2.mp3` on-the-fly? – Damn Vegetables Oct 17 '19 at 20:08
  • 1
    I have tried this one but not working.. I have one audio.mp3 and video.mp4 which I need to play both at a time but the player only plays the video. – Sumit Chakraborty Jul 05 '22 at 13:11