Search in sources :

Example 16 with TrackSelectionArray

use of com.google.android.exoplayer2.trackselection.TrackSelectionArray in project Slide by ccrama.

the class ExoVideoView method setupPlayer.

/**
 * Initializes the view to render onto and the SimpleExoPlayer instance
 */
private void setupPlayer() {
    // Create a view to render the video onto and an AspectRatioFrameLayout to size the video correctly
    AspectRatioFrameLayout frame = new AspectRatioFrameLayout(context);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    params.addRule(CENTER_IN_PARENT, TRUE);
    frame.setLayoutParams(params);
    frame.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
    SurfaceView renderView = new SurfaceView(context);
    frame.addView(renderView);
    addView(frame);
    // Create a track selector so we can set specific video quality for DASH
    trackSelector = new DefaultTrackSelector(context);
    if ((SettingValues.lowResAlways || (NetworkUtil.isConnected(context) && !NetworkUtil.isConnectedWifi(context) && SettingValues.lowResMobile)) && SettingValues.lqVideos) {
        trackSelector.setParameters(trackSelector.buildUponParameters().setForceLowestBitrate(true));
    } else {
        trackSelector.setParameters(trackSelector.buildUponParameters().setForceHighestSupportedBitrate(true));
    }
    // Create the player, attach it to the view, make it repeat infinitely
    player = new SimpleExoPlayer.Builder(context).setTrackSelector(trackSelector).build();
    player.setVideoSurfaceView(renderView);
    player.setRepeatMode(Player.REPEAT_MODE_ALL);
    // Mute by default
    player.setVolume(0f);
    // Create audio focus helper
    audioFocusHelper = new AudioFocusHelper(ContextCompat.getSystemService(context, AudioManager.class));
    player.addListener(new Player.Listener() {

        // Make the video use the correct aspect ratio
        @Override
        public void onVideoSizeChanged(@NonNull VideoSize videoSize) {
            frame.setAspectRatio(videoSize.height == 0 || videoSize.width == 0 ? 1 : videoSize.width * videoSize.pixelWidthHeightRatio / videoSize.height);
        }

        // Logging
        @Override
        public void onTracksChanged(@NonNull TrackGroupArray trackGroups, @NonNull TrackSelectionArray trackSelections) {
            StringBuilder toLog = new StringBuilder();
            for (int i = 0; i < trackGroups.length; i++) {
                for (int j = 0; j < trackGroups.get(i).length; j++) {
                    toLog.append("Format:\t").append(trackGroups.get(i).getFormat(j)).append("\n");
                }
            }
            // FIXME: How do I make onTracksChanged work with ExoTrackSelection?
            /*for (TrackSelection i : trackSelections.getAll()) {
                    if (i != null)
                        toLog.append("Selected format:\t").append(i.getSelectedFormat()).append("\n");
                }*/
            Log.v(LogUtil.getTag(), toLog.toString());
        }
    });
}
Also used : Player(com.google.android.exoplayer2.Player) SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) TrackSelectionArray(com.google.android.exoplayer2.trackselection.TrackSelectionArray) AspectRatioFrameLayout(com.google.android.exoplayer2.ui.AspectRatioFrameLayout) VideoSize(com.google.android.exoplayer2.video.VideoSize) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) SurfaceView(android.view.SurfaceView)

Aggregations

TrackSelectionArray (com.google.android.exoplayer2.trackselection.TrackSelectionArray)11 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)9 TrackGroup (com.google.android.exoplayer2.source.TrackGroup)5 TrackSelection (com.google.android.exoplayer2.trackselection.TrackSelection)5 Player (com.google.android.exoplayer2.Player)3 SimpleExoPlayer (com.google.android.exoplayer2.SimpleExoPlayer)3 AnalyticsListener (com.google.android.exoplayer2.analytics.AnalyticsListener)3 Metadata (com.google.android.exoplayer2.metadata.Metadata)3 MediaSource (com.google.android.exoplayer2.source.MediaSource)3 MappedTrackInfo (com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo)3 ExoPlaybackException (com.google.android.exoplayer2.ExoPlaybackException)2 PlaybackParameters (com.google.android.exoplayer2.PlaybackParameters)2 Timeline (com.google.android.exoplayer2.Timeline)2 ExtractorMediaSource (com.google.android.exoplayer2.source.ExtractorMediaSource)2 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)2 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)2 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)2 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)2 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)2 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)2