Resizing / scaling YouTubePlayerFragment during video playback

I am trying to reproduce minifying a video in a Youtube app as shown here . For this I tried to use the Draggable Panel library... When I ran the sample, I noticed that the video doesn't scale, but rather graininess when minimized during playback. When the video is stopped (not paused) and the thumbnail is displayed, the view is scaled as intended. I read another question that YouTubePlayerView is implemented with SurfaceView. I also read in the docs that the SurfaceView behaves differently from the normal look due to the way it punches a hole in the screen. I believe YoutubePlayerView is based on SurfaceView, it doesn't scale properly. How to properly scale video playback in YoutubePlayerView to fit the size of its parent layout during playback?

+3


source to share


1 answer


In my experience with YouTubePlayerView and YouTubePlayerFragment, I've found that scaling with Nineoldandroids or ViewPropertyAnimator doesn't work properly. To adjust the size of the video being played, you must programmatically adjust the height and width of the layout options. The DraggablePanel library has two classes for resizing the top view. The default is ScaleTransformer, which does not work for video during playback as it renders part of the video being played from the view and the other is ResizeTransformer. ResizeTransformer isn't quite as smooth as ScaleTransformer, but it does work somewhat. The problem with ResizeTransformer is that the YouTubePlayerView layout sometimes clamps underneath the bottom view when dragging. Play then stops because it detectsthat the overlay blocked it. I made a compromise to cut the DraggablePanel and write a maximize and minimize method for the YouTubePlayerView container.



public void minimize() {
    RelativeLayout.LayoutParams playerParams =
            (RelativeLayout.LayoutParams) playerView.getLayoutParams();
    playerParams.width = getResources().getDimensionPixelSize(R.dimen.player_minimized_width);
    playerParams.height = getResources().getDimensionPixelSize(R.dimen.player_minimized_height);
    FrameLayout container = (FrameLayout)playerView.getParent().getParent();
    RelativeLayout.LayoutParams containerParams = (RelativeLayout.LayoutParams)container.getLayoutParams();
    containerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    containerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    containerParams.bottomMargin = getResources().getDimensionPixelSize(R.dimen.player_minimized_margin);
    containerParams.rightMargin = getResources().getDimensionPixelSize(R.dimen.player_minimized_margin);
    playerView.requestLayout();
    container.requestLayout();
    isMinimized = true;
}

public void maximize() {
    RelativeLayout.LayoutParams playerParams =
            (RelativeLayout.LayoutParams) playerView.getLayoutParams();
    playerParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    playerParams.height = getResources().getDimensionPixelSize(R.dimen.player_height);
    FrameLayout container = (FrameLayout)playerView.getParent().getParent();
    RelativeLayout.LayoutParams containerParams = (RelativeLayout.LayoutParams)container.getLayoutParams();
    containerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0);
    containerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
    containerParams.bottomMargin = 0;
    containerParams.rightMargin = 0;
    playerView.requestLayout();
    container.requestLayout();
    isMinimized = false;
}

      

+2


source







All Articles