Google Exoplayer 之全面认识( 三 )


除了上述的的实现方式,库还提供,和 urce 。这些实现能够通过组合能够支持更复杂的功能 。下面对一些常见的用例进行说明 。注意,尽管下面的实例使用的视频 ,它们同样适用于仅有音频 ,事实上适用于所有支持的 media types 的。
- afile
给定的视频文件和一个单独的字幕文件,可以将它们合并到用于的单一来源 。

Google Exoplayer 之全面认识

文章插图
MediaSource videoSource = new ExtractorMediaSource(videoUri, ...);MediaSource subtitleSource = new SingleSampleMediaSource(subtitleUri, ...);// Plays the video with the sideloaded subtitle.MergingMediaSource mergedSource =new MergingMediaSource(videoSource, subtitleSource);
a video
【Google Exoplayer 之全面认识】 可以实现视频的无缝循环 。下面演示了如何实现无缝循环 。也可以创建一个指定循环计数的。
MediaSource source = new ExtractorMediaSource(videoUri, ...);// Loops the video indefinitely.LoopingMediaSource loopingSource = new LoopingMediaSource(source);
aof
urce 使得支持顺序播放两个或多个单独的S,下面的示例顺序播放两段视频 。无缝切换资源,并且对被串联资源的格式是否相同没有要求 。
MediaSource firstSource = new ExtractorMediaSource(firstVideoUri, ...);MediaSource secondSource = new ExtractorMediaSource(secondVideoUri, ...);// Plays the first video, then the second video.ConcatenatingMediaSource concatenatedSource =new ConcatenatingMediaSource(firstSource, secondSource);
可以为了更不寻常的用例进一步结合复合。给定两个视频 A 和 B,下面的例子演示了如何使用和urce 按照( A,A,B)的序列进行无线循环 。
MediaSource firstSource = new ExtractorMediaSource(firstVideoUri, ...);MediaSource secondSource = new ExtractorMediaSource(secondVideoUri, ...);// Plays the first video twice.LoopingMediaSource firstSourceTwice = new LoopingMediaSource(firstSource, 2);// Plays the first video twice, then the second video.ConcatenatingMediaSource concatenatedSource =new ConcatenatingMediaSource(firstSourceTwice, secondSource);// Loops the sequence indefinitely.LoopingMediaSource compositeSource = new LoopingMediaSource(concatenatedSource);
下面的例子是等价的,这表明实现相同的结果方式不止一个 。
MediaSource firstSource = new ExtractorMediaSource(firstVideoUri, ...);MediaSource secondSource = new ExtractorMediaSource(secondVideoUri, ...);// Plays the first video twice, then the second video.ConcatenatingMediaSource concatenatedSource =new ConcatenatingMediaSource(firstSource, firstSource, secondSource);// Loops the sequence indefinitely.LoopingMediaSource compositeSource = new LoopingMediaSource(concatenatedSource);
避免在一次中用同一个实例多次,除非文档中明确允许 。在上面的例子中两次使用的就是这样的情况,由于对中 urce 明确规定,重复项是允许的 。在一般情况下,由组合物形成的对象的曲线应该是一个树 。在中使用多个等同于的实例是允许的 。