Search in sources :

Example 1 with MergingMediaSource

use of com.google.android.exoplayer2.source.MergingMediaSource in project NewPipe by TeamNewPipe.

the class VideoPlayer method sourceOf.

@Override
@Nullable
public MediaSource sourceOf(final PlayQueueItem item, final StreamInfo info) {
    final MediaSource liveSource = super.sourceOf(item, info);
    if (liveSource != null)
        return liveSource;
    List<MediaSource> mediaSources = new ArrayList<>();
    // Create video stream source
    final List<VideoStream> videos = ListHelper.getSortedStreamVideosList(context, info.getVideoStreams(), info.getVideoOnlyStreams(), false);
    final int index;
    if (videos.isEmpty()) {
        index = -1;
    } else if (playbackQuality == null) {
        index = getDefaultResolutionIndex(videos);
    } else {
        index = getOverrideResolutionIndex(videos, getPlaybackQuality());
    }
    final VideoStream video = index >= 0 && index < videos.size() ? videos.get(index) : null;
    if (video != null) {
        final MediaSource streamSource = buildMediaSource(video.getUrl(), PlayerHelper.cacheKeyOf(info, video), MediaFormat.getSuffixById(video.getFormatId()));
        mediaSources.add(streamSource);
    }
    // Create optional audio stream source
    final List<AudioStream> audioStreams = info.getAudioStreams();
    final AudioStream audio = audioStreams.isEmpty() ? null : audioStreams.get(ListHelper.getDefaultAudioFormat(context, audioStreams));
    // Merge with audio stream in case if video does not contain audio
    if (audio != null && ((video != null && video.isVideoOnly) || video == null)) {
        final MediaSource audioSource = buildMediaSource(audio.getUrl(), PlayerHelper.cacheKeyOf(info, audio), MediaFormat.getSuffixById(audio.getFormatId()));
        mediaSources.add(audioSource);
    }
    // If there is no audio or video sources, then this media source cannot be played back
    if (mediaSources.isEmpty())
        return null;
    // Create subtitle sources
    for (final Subtitles subtitle : info.getSubtitles()) {
        final String mimeType = PlayerHelper.mimeTypesOf(subtitle.getFileType());
        if (mimeType == null)
            continue;
        final Format textFormat = Format.createTextSampleFormat(null, mimeType, SELECTION_FLAG_AUTOSELECT, PlayerHelper.captionLanguageOf(context, subtitle));
        final MediaSource textSource = dataSource.getSampleMediaSourceFactory().createMediaSource(Uri.parse(subtitle.getURL()), textFormat, TIME_UNSET);
        mediaSources.add(textSource);
    }
    if (mediaSources.size() == 1) {
        return mediaSources.get(0);
    } else {
        return new MergingMediaSource(mediaSources.toArray(new MediaSource[mediaSources.size()]));
    }
}
Also used : AudioStream(org.schabi.newpipe.extractor.stream.AudioStream) Format(com.google.android.exoplayer2.Format) MediaFormat(org.schabi.newpipe.extractor.MediaFormat) MediaSource(com.google.android.exoplayer2.source.MediaSource) MergingMediaSource(com.google.android.exoplayer2.source.MergingMediaSource) MergingMediaSource(com.google.android.exoplayer2.source.MergingMediaSource) Subtitles(org.schabi.newpipe.extractor.Subtitles) ArrayList(java.util.ArrayList) VideoStream(org.schabi.newpipe.extractor.stream.VideoStream) PlayerHelper.getTimeString(org.schabi.newpipe.player.helper.PlayerHelper.getTimeString) Nullable(android.support.annotation.Nullable)

Aggregations

Nullable (android.support.annotation.Nullable)1 Format (com.google.android.exoplayer2.Format)1 MediaSource (com.google.android.exoplayer2.source.MediaSource)1 MergingMediaSource (com.google.android.exoplayer2.source.MergingMediaSource)1 ArrayList (java.util.ArrayList)1 MediaFormat (org.schabi.newpipe.extractor.MediaFormat)1 Subtitles (org.schabi.newpipe.extractor.Subtitles)1 AudioStream (org.schabi.newpipe.extractor.stream.AudioStream)1 VideoStream (org.schabi.newpipe.extractor.stream.VideoStream)1 PlayerHelper.getTimeString (org.schabi.newpipe.player.helper.PlayerHelper.getTimeString)1