'Listening to new media added while adding mediaItem in ConcatenatingMediaSource in ExoPlayer

I'm using a ConcatenatingMediaSource to play tracks sequentially in ExoPlayer. This ConcatenatingMediaSource is single-element at first. So there is only one track. When I click the button, I add the second track to this list and remove the first one. So now I just want the newly added track to play. However, there is a pause when the media is deleted while the player is playing.

When I add the second track and remove the first, I want to remove the first when the second starts playing. How can I achieve this?

MediaSource firstSource = CreateMediaSource(R.raw.v1);
MediaSource secondSource= CreateMediaSource(R.raw.v2);

concatenatedSource = new ConcatenatingMediaSource(firstSource);
LoopingMediaSource compositeSource = new LoopingMediaSource(concatenatedSource);
player.setRepeatMode(Player.REPEAT_MODE_ALL);
player.prepare(compositeSource);
player.setPlayWhenReady(true);

Button click method

concatenatedSource.addMediaSource(secondSource);
concatenatedSource.removeMediaSource(0);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source