ExoPlayer - play two audio tracks simultaneously

3.2k Views Asked by At

Using ExoPlayer i'm trying to play two audio tracks simultaneously.
My idea was to create two MediaSource, and to "combine" between them using MergingMediaSource.
This is what I have done:

    Uri uriAudio1 = Uri.parse(AUDIO_URL_1);
    Uri uriAudio2 = Uri.parse(AUDIO_URL_2);

    MediaSource audioSource1  = new ExtractorMediaSource(uriAudio1, dataSourceFactory, extractorsFactory, mainHandler, null);
    MediaSource audioSource2  = new ExtractorMediaSource(uriAudio2, dataSourceFactory, extractorsFactory, mainHandler, null);


    MergingMediaSource mergedAudioSource = new MergingMediaSource(audioSource1, audioSource2);


    mPlayer.prepare(mergedAudioSource);

How ever instead of hearing both audio tracks in parallel, i hear only the first audioSource1.

Any ideas? Thanks!

4

There are 4 best solutions below

0
Paul Lammertsma On

It seems that this functionality isn't currently supported, according to issue #2200.

1
Bhumi On

You can use ConcatenatingMediaSource :

  List<MediaSource> mediaSources = new ArrayList<>();
                    for (Track track : tracks) {
                        MediaSource mediaSource = ...//use your uri media source from tack.getPath
                        mediaSources.add(mediaSource);
                    }
                    mediaPlayer.prepare(new ConcatenatingMediaSource(mediaSources.toArray(new MediaSource[mediaSources.size()])));
0
Charles On

Try to set channel count like this

val trackSelector = DefaultTrackSelector()
trackSelector.setParameters(
                trackSelector.buildUponParameters()
                .setMaxVideoSizeSd()
                .setMaxAudioChannelCount(2)
            )
0
ZP007 On

I ran into the same problem, solved it by using two different instances of Exoplayer, one for each audio asset, and playing them at the same time.