2018-05-10 22:28:31 +02:00
|
|
|
package org.schabi.newpipe.player.resolver;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2020-03-31 19:20:15 +02:00
|
|
|
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2018-05-10 22:28:31 +02:00
|
|
|
|
|
|
|
import com.google.android.exoplayer2.source.MediaSource;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.MediaFormat;
|
|
|
|
import org.schabi.newpipe.extractor.stream.AudioStream;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
|
|
|
import org.schabi.newpipe.player.helper.PlayerDataSource;
|
|
|
|
import org.schabi.newpipe.player.helper.PlayerHelper;
|
2022-03-13 06:22:47 +01:00
|
|
|
import org.schabi.newpipe.player.mediaitem.MediaItemTag;
|
|
|
|
import org.schabi.newpipe.player.mediaitem.StreamInfoTag;
|
2018-05-10 22:28:31 +02:00
|
|
|
import org.schabi.newpipe.util.ListHelper;
|
|
|
|
|
|
|
|
public class AudioPlaybackResolver implements PlaybackResolver {
|
2020-03-31 19:20:15 +02:00
|
|
|
@NonNull
|
|
|
|
private final Context context;
|
|
|
|
@NonNull
|
|
|
|
private final PlayerDataSource dataSource;
|
2018-05-10 22:28:31 +02:00
|
|
|
|
|
|
|
public AudioPlaybackResolver(@NonNull final Context context,
|
|
|
|
@NonNull final PlayerDataSource dataSource) {
|
|
|
|
this.context = context;
|
|
|
|
this.dataSource = dataSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-05-17 03:18:32 +02:00
|
|
|
@Nullable
|
2020-03-31 19:20:15 +02:00
|
|
|
public MediaSource resolve(@NonNull final StreamInfo info) {
|
2018-05-10 22:28:31 +02:00
|
|
|
final MediaSource liveSource = maybeBuildLiveMediaSource(dataSource, info);
|
2020-03-31 19:20:15 +02:00
|
|
|
if (liveSource != null) {
|
|
|
|
return liveSource;
|
|
|
|
}
|
2018-05-10 22:28:31 +02:00
|
|
|
|
|
|
|
final int index = ListHelper.getDefaultAudioFormat(context, info.getAudioStreams());
|
2020-03-31 19:20:15 +02:00
|
|
|
if (index < 0 || index >= info.getAudioStreams().size()) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-10 22:28:31 +02:00
|
|
|
|
|
|
|
final AudioStream audio = info.getAudioStreams().get(index);
|
2022-03-13 06:22:47 +01:00
|
|
|
final MediaItemTag tag = StreamInfoTag.of(info);
|
2018-05-10 22:28:31 +02:00
|
|
|
return buildMediaSource(dataSource, audio.getUrl(), PlayerHelper.cacheKeyOf(info, audio),
|
|
|
|
MediaFormat.getSuffixById(audio.getFormatId()), tag);
|
|
|
|
}
|
|
|
|
}
|