|
11 | 11 | import com.google.android.exoplayer2.source.MediaSource; |
12 | 12 |
|
13 | 13 | import org.schabi.newpipe.extractor.stream.AudioStream; |
| 14 | +import org.schabi.newpipe.extractor.stream.Stream; |
14 | 15 | import org.schabi.newpipe.extractor.stream.StreamInfo; |
| 16 | +import org.schabi.newpipe.extractor.stream.VideoStream; |
15 | 17 | import org.schabi.newpipe.player.helper.PlayerDataSource; |
16 | 18 | import org.schabi.newpipe.player.mediaitem.MediaItemTag; |
17 | 19 | import org.schabi.newpipe.player.mediaitem.StreamInfoTag; |
@@ -41,22 +43,50 @@ public MediaSource resolve(@NonNull final StreamInfo info) { |
41 | 43 | return liveSource; |
42 | 44 | } |
43 | 45 |
|
44 | | - final List<AudioStream> audioStreams = getNonTorrentStreams(info.getAudioStreams()); |
45 | | - |
46 | | - final int index = ListHelper.getDefaultAudioFormat(context, audioStreams); |
47 | | - if (index < 0 || index >= info.getAudioStreams().size()) { |
| 46 | + final Stream stream = getAudioSource(info); |
| 47 | + if (stream == null) { |
48 | 48 | return null; |
49 | 49 | } |
50 | 50 |
|
51 | | - final AudioStream audio = info.getAudioStreams().get(index); |
52 | 51 | final MediaItemTag tag = StreamInfoTag.of(info); |
53 | 52 |
|
54 | 53 | try { |
55 | 54 | return PlaybackResolver.buildMediaSource( |
56 | | - dataSource, audio, info, PlaybackResolver.cacheKeyOf(info, audio), tag); |
| 55 | + dataSource, stream, info, PlaybackResolver.cacheKeyOf(info, stream), tag); |
57 | 56 | } catch (final ResolverException e) { |
58 | 57 | Log.e(TAG, "Unable to create audio source", e); |
59 | 58 | return null; |
60 | 59 | } |
61 | 60 | } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get a stream to be played as audio. If a service has no separate {@link AudioStream}s we |
| 64 | + * use a video stream as audio source to support audio background playback. |
| 65 | + * |
| 66 | + * @param info of the stream |
| 67 | + * @return the audio source to use or null if none could be found |
| 68 | + */ |
| 69 | + @Nullable |
| 70 | + private Stream getAudioSource(@NonNull final StreamInfo info) { |
| 71 | + final List<AudioStream> audioStreams = getNonTorrentStreams(info.getAudioStreams()); |
| 72 | + if (!audioStreams.isEmpty()) { |
| 73 | + final int index = ListHelper.getDefaultAudioFormat(context, audioStreams); |
| 74 | + return getStreamForIndex(index, audioStreams); |
| 75 | + } else { |
| 76 | + final List<VideoStream> videoStreams = getNonTorrentStreams(info.getVideoStreams()); |
| 77 | + if (!videoStreams.isEmpty()) { |
| 78 | + final int index = ListHelper.getDefaultResolutionIndex(context, videoStreams); |
| 79 | + return getStreamForIndex(index, videoStreams); |
| 80 | + } |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + @Nullable |
| 86 | + Stream getStreamForIndex(final int index, @NonNull final List<? extends Stream> streams) { |
| 87 | + if (index >= 0 && index < streams.size()) { |
| 88 | + return streams.get(index); |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
62 | 92 | } |
0 commit comments