MediaSource Composition is a powerful concept in ExoPlayer 2 that enables features such as side-loading captions, loaning, and playing a series of videos.Copy the code
To play media in ExoPlayer 2, the application must create a corresponding MedisSource object to provide to the player. For example, to play MP4, the application must create an ExtractorMediaSource: ExtractorMediaSource works with regular media files (MP4, WebM, MKV, etc.)
MediaSource videoSource = newExtractorMediaSource(mp4VideoUri, ...) ;// This resource is provided for player playback.
player.prepare(videoSource, true);
Copy the code
ExtractorMediaSource works with regular media files (MP4, WebM, MKV, etc.). The ExoPlayer library also provides MediaSource implementations for DASH (DashMediaSource), SmoothStreaming (SsMediaSource), and HLS (HlsMediaSource).
In addition to the MediaSource implementation mentioned above, the ExoPlayer library provides MergingMediaSource, LoopingMediaSource, and ConcatenatingMediaSource. These are sample MediaSource implementations that can be synthesized for more complex playback capabilities. We describe some common use cases below. Note that although the following examples are described in the context of video playback, they apply equally to audio-only playback, and indeed to playback of any supported media type.
The subtitle file is loaded on the side
Specify video files and individual subtitle files that can be combined into a single source for playback using MergingMediaSource.
MediaSource videoSource = newExtractorMediaSource(videoUri, ...) ; MediaSource subtitleSource =newSingleSampleMediaSource(subtitleUri, ...) ;// Load subtitles while playing videos
MergingMediaSource mergedSource = new MergingMediaSource(videoSource, subtitleSource);
Copy the code
Play video in a seamless loop
You can use LoopingMediaSource to play video in a seamless loop. The following example plays a video in an infinite loop. A finite number of loops can also be specified when creating a LoopingMediaSource.
MediaSource source = newExtractorMediaSource(videoUri, ...) ;// Infinite loop video.
LoopingMediaSource loopingSource = new LoopingMediaSource(source);
Copy the code
Seamlessly play a series of videos
ConcatenatingMediaSource Can play two or more individual MediaSource sequentially. The following example plays two videos in sequence. Switching between sources is seamless. The connected source does not need to have the same format (for example, video files containing 480p H264 can be played in tandem with video files containing 720p VP9). The source can even be of a different type (for example, supporting video in tandem with a pure audio stream)
MediaSource firstSource = newExtractorMediaSource(firstVideoUri, ...) ; MediaSource secondSource =newExtractorMediaSource(secondVideoUri, ...) ;// Play the first video and then the second
ConcatenatingMediaSource concatenatedSource =new ConcatenatingMediaSource(firstSource, secondSource);
Copy the code
Advanced Composition
Composite MediaSource can be further combined for more unusual use cases. Given two videos A and B, the following example shows how LoopingMediaSource and ConcatenatingMediaSource can be used together to loop the sequence indefinitely (A, A, B)
MediaSource firstSource = newExtractorMediaSource(firstVideoUri, ...) ; MediaSource secondSource =newExtractorMediaSource(secondVideoUri, ...) ;// Play the first video twice.
LoopingMediaSource firstSourceTwice = new LoopingMediaSource(firstSource, 2);
// Play the first video twice, then play the second video.
ConcatenatingMediaSource concatenatedSource = new ConcatenatingMediaSource(firstSourceTwice, secondSource);
// Loop this sequence indefinitely.
LoopingMediaSource compositeSource = new LoopingMediaSource(concatenatedSource);
Copy the code
The following example is equivalent and shows that there are multiple ways to get the same result.
MediaSource firstSource = newExtractorMediaSource(firstVideoUri, ...) ; MediaSource secondSource =newExtractorMediaSource(secondVideoUri, ...) ;// Play the first video twice, then play the second video.
ConcatenatingMediaSource concatenatedSource = new ConcatenatingMediaSource(firstSource, firstSource, secondSource);
// Loop this sequence indefinitely.
LoopingMediaSource compositeSource = new LoopingMediaSource(concatenatedSource);
Copy the code
Important: It is important to avoid using the same MediaSource instance more than once in a composite unless expressly allowed in the documentation. This is the case in the example above where firstSource is used twice, because the Javadoc of ConcatenatingMediaSource explicitly states that duplicate entries are allowed. However, in general, a composite object graph should be a tree structure. Multiple equivalent MediaSource instances are allowed in composition.
In summary, MediaSource Composition is a powerful concept in ExoPlayer 2 that enables features such as side-loading captions, loaning, and playing video sequences. It is important to avoid using the same MediaSource instance more than once in a composition unless explicitly allowed in the documentation.
ExoPlayer 2 – MediaSource Composition