Search in sources :

Example 1 with AspectRatioFrameLayout

use of com.google.android.exoplayer2.ui.AspectRatioFrameLayout 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

SurfaceView (android.view.SurfaceView)1 Player (com.google.android.exoplayer2.Player)1 SimpleExoPlayer (com.google.android.exoplayer2.SimpleExoPlayer)1 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)1 DefaultTrackSelector (com.google.android.exoplayer2.trackselection.DefaultTrackSelector)1 TrackSelectionArray (com.google.android.exoplayer2.trackselection.TrackSelectionArray)1 AspectRatioFrameLayout (com.google.android.exoplayer2.ui.AspectRatioFrameLayout)1 VideoSize (com.google.android.exoplayer2.video.VideoSize)1