I'm setting up Exo Player in my app and I have implemented DASH adaptive streaming in my player, and want to add the functionality to play the track of specific quality selected by the user.
My Exo player version is 2.9.3
, I tried implementing by following these Stackoverflow Post,Medium Post, but it's bit confusing and some part of the code is deprecated, and saying the truth is that I am a beginner.
Here's my code for initializing the player:-
private void initializePlayer() {
if (player == null) {
bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory factory = new PlayerTrackSelector(bandwidthMeter);
trackSelector = new DefaultTrackSelector(factory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
player.addVideoListener(this);
playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
player.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
player.addListener(this);
playerView.setPlayer(player);
}
MediaSource mediaSource = buildMediaSource(Uri.parse("http://192.168.43.238:3000/storage/video-dash/9e351142a4eb1664643bf93ba13959e8.mpd"));
player.prepare(mediaSource, true, false);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
}
And the TrackSelector
Class:-
public class PlayerTrackSelector implements TrackSelection.Factory {
private BandwidthMeter bandwidthMeter;
public PlayerTrackSelector(BandwidthMeter bandwidthMeter) {
this.bandwidthMeter = bandwidthMeter;
}
@Override
public TrackSelection createTrackSelection(TrackGroup group, BandwidthMeter bandwidthMeter, int... tracks) {
return new AdaptiveTrackSelection(group,tracks,bandwidthMeter,
AdaptiveTrackSelection.DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS,
AdaptiveTrackSelection.DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS,
AdaptiveTrackSelection.DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS,
AdaptiveTrackSelection.DEFAULT_BANDWIDTH_FRACTION,
AdaptiveTrackSelection.DEFAULT_BUFFERED_FRACTION_TO_LIVE_EDGE_FOR_QUALITY_INCREASE,
AdaptiveTrackSelection.DEFAULT_MIN_TIME_BETWEEN_BUFFER_REEVALUTATION_MS,
Clock.DEFAULT);
}
}
I expect to get a list of available quality types and let the user select one of them and play the rest of the content with the selected type, this question is maybe broad but please guide me a little.
Try following code. Call following code when you need to show list. rendererIndex -> You can try manually. TRACK_TYPE_AUDIO = 1, TRACK_TYPE_VIDEO = 2.
UPDATED:
MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
if (mappedTrackInfo != null) {
int rendererIndex = 2;
int rendererType = mappedTrackInfo.getRendererType(rendererIndex);
boolean allowAdaptiveSelections =
rendererType == C.TRACK_TYPE_VIDEO
|| (rendererType == C.TRACK_TYPE_AUDIO
&& mappedTrackInfo.getTypeSupport(C.TRACK_TYPE_VIDEO)
== MappingTrackSelector.MappedTrackInfo.RENDERER_SUPPORT_NO_TRACKS);
Pair<AlertDialog, TrackSelectionView> dialogPair =
TrackSelectionView.getDialog(xxxxxxxxxxxxxx.this, "Track Selector", trackSelector, rendererIndex);
dialogPair.second.setShowDisableOption(true);
dialogPair.second.setAllowAdaptiveSelections(allowAdaptiveSelections);
dialogPair.first.show();
}