YouTube player in ScrollView android - android

When youtube player fragment nested in ScrollView I get error when rotate device to landscape:
YouTubePlayer.ErrorReason.UNAUTHORIZED_OVERLAY
And what is more interesting, is that the problem disappears when I remove ScrollView! But I can
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
/>
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="#+id/youtubeplayerfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>

YouTubePlayer.ErrorReason.UNAUTHORIZED_OVERLAY error indicates that *Playback has been stopped due to a view overlaying the player. This means that the YouTube Player has been obscured by some other view on top of it. The YouTube API can detect it and stop the Playback.
One very common reason for this to happen is when the fragment for holding the YouTube player is embedded in a scroll view. Scroll View adds additional layer of elements can be be scrolled. For.eg the in your case. The player contained in the same declaration would detect the overlap and would eventually stop giving the above error.

I have the same problem with a YoutubePlayer inside of a ScrollView and it stops with this message:
W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.widget.ScrollView{69b88e5 VFED.V... ........ 0,0-1794,1017 #7f0d0070 app:id/scrollview}. The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 21, top: 196, right: 21, bottom: -164 (these should all be positive).
This happens all the time when the video is not fully visible on the screen. When it is visible completely, rotating the device works fine. To me this looks like a bug in the Youtube Android Player. I made a workaround with the following code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// This is an ugly workaround to prevent youtube from (wrongly) thinking we have an
// overlay above the video. Having overlays is not allowed and the video would stop otherwise.
DisplayMetrics metrics = getResources().getDisplayMetrics();
int offset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, metrics);
scrollView.scrollTo(0, youtubePlayer.getView().getTop() - offset);
}
This obviously is not a good fix, because it depends on the ratio of the video and how it is shown on your display. Also your ScrollView will be scrolled to a different place after rotation (which you could re-reset manually later on).

Related

LibVLC Android: Video not scaling to VLCVideoLayout size

I'm having a problem with the video output on a VLCVideoLayout.
Video plays fine, however it plays with its original size instead of fitting inside of the VLCVideoLayout.
Here's what I already attempted but failed:
ViewGroup.LayoutParams videoParams = videoLayout.getLayoutParams();
mMediaPlayer.getVLCVout().setWindowSize(videoParams.width, videoParams.height);
Here's part of my layout:
<org.videolan.libvlc.util.VLCVideoLayout
android:id="#+id/player"
android:layout_width="200dp"
android:layout_height="120dp"
android:fitsSystemWindows="false"/>
I'm using version 3.2.6
implementation 'org.videolan.android:libvlc-all:3.2.6'
The end result it's the crop of center part of the video (or a zoom on the center?).
This is a picture with the problem.
The white part is the rest of the layout, on the center i have the VLCVideoLayout.
The video has a lot more than what the VLCVideoLayout is showing.
Anybody can help?
Thanks.

Blinking Video/SurfaceView when resizing to PiP mode

I'm working on PiP mode in my app. In fact it is working pretty fine, but I have a problem with blinking/disappearing video for a split second.
I have VideoActivity in "fullscreen"/normal mode. I'm calling enterPictureInPictureMode(); and Activity is collapsing to small PiP-frame. At the moment when animation is ending shrinking layout then video get transparent for a split second/one frame. Also exacly in this frame whole PiP-frame gets shadow under (elevation). When I touch this small PiP-frame it expands a bit to big PiP-frame (not fullscreen) and this anim is smooth, but when auto-collapse to small PiP-frame it blinks again on the end... Everytime when Activity is animating to small PiP-frame (no matter if is collapsing from big PiP-frame or fullscreen Activity) one-frame transparency-blink occurs when reaching final "small bounds". No impact to sound, video is playing steady, no calls to onPlayerStateChanged.
With newer API version we can create animation with bounds and thats how I know the video view is blinking... Using no-attribute method makes whole layout collapses and I can see blink only on area with video/player/surface view (black stripes above and below video). When I use newer method and setSourceRectHint I can see whole PiP-frame disapears without respecting black background of its parent. In fact I've added dummy View with fixed size, background color and centerInParent like PlayerView - it blinks/hides just like player when is placed below (first in parent) PlayerView layer, but placed above doesn't blink at all and is showing above PlayerView even when it disappears for split sec.
How to fix that blink or maybe though set black background, not transparent...
if (Build.VERSION.SDK_INT >= 26) {
Rect bounds = new Rect();
playerView.getVideoSurfaceView().getDrawingRect(bounds);
int toTop = (playerView.getMeasuredHeight() - bounds.height()) / 2,
toLeft = (playerView.getMeasuredWidth() - bounds.width()) / 2;
bounds.top += toTop;
bounds.left += toLeft;
bounds.bottom += toTop;
bounds.right += toLeft;
enterPictureInPictureMode(new PictureInPictureParams.Builder().
setSourceRectHint(bounds).
setAspectRatio(new Rational(bounds.width(), bounds.height())).
build());
} else
enterPictureInPictureMode();
xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/activity_video_player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#android:color/black"
app:controller_layout_id="#layout/exo_custom_control_view"
app:fastforward_increment="10000"
app:rewind_increment="10000"
app:show_timeout="100" />
...
PS. Above problem also appears when using usual VideoView played in WebView and got into fullscreen by VideoEnabledWebChromeClient. But in this case blink reveals for a split second part of WebViews content
edit: I've just found a workaround for blinking ExoPlayer - by replacing SurfaceView with TextureView, adding app:surface_type="texture_view" - but I'm reading that TextureView is a bit less efficient and also this is not fixing VideoView blink (fullscreen from WebView)
edit2: can confirm blinks on Android 8 (Xiaomi) and 9 (Pixel)

Android YouTubePlayerView unauthorized overlay and too small error

I'm using Youtube Api for Android in my app, and i create new YouTubePlayerView dynamic in my activity such as:
YouTubePlayerView youTubePlayerView = new YouTubePlayerView(PlayerActivity.this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(640, 360);
layoutParams.setMargins(100, 200, 0, 0);
youTubePlayerView.setLayoutParams(layoutParams);
root.addView(youTubePlayerView);
As the code, "root" is a RelativeLayout, and the size and margin of youTubePlayerView is different with each youTubePlayerView;
this code sometimes work, but sometimes not, it returns error below:
W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor com.google.android.youtube.player.YouTubePlayerView{37ab2582 V.E..... ......ID 0,0-0,0}. The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 0, top: 0, right: 0, bottom: 0 (these should all be positive).
or this one
W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to the player's view being too small. The YouTubePlayerView is 0dp wide (minimum is 200dp) and 0dp high (minimum is 110dp).
is there any way to avoid it happen, or let it play video successfully.
i appreciate any help.
Regards
Based on the Official Google Documentation, setting the flag leaves to you the task of handling the UI elements visibility.
YouTube player will detect that a view is being displayed over it and will throw that warning.
You can try with one of the following additional flags:
FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE:causes the player to automatically enter fullscreen whenever the device enters landscape orientation.
FULLSCREEN_FLAG_CONTROL_ORIENTATION: enables automatic control of the orientation.
FULLSCREEN_FLAG_CONTROL_SYSTEM_UI: enables automatic control of system UI.
FULLSCREEN_FLAG_CUSTOM_LAYOUT:disables the default fullscreen layout handler, enabling you to control the transition to fullscreen layout manually.

YouTubePlayerView black outline when scrolling

The question has been asked before here, yet I would like to provide a little more information.
The YouTubePlayerView often gives a black outline when scrolling, this can happen after pausing and changing rotation.
This appears to happen when I scroll and it gets overlaid, so it pauses (to prevent that controls get hidden). The pausing isn't a problem since the user isn't watching the video but the black screen is.
This happens on a Galaxy Tab 2 running Android 4.0.3, also on a Xperia T on 4.1.2, on a One X running 4.2.2 and Desire running 2.2.2 does the problem not occur.
Some more info:
The video-image that is paused appears to be stretching (On Xperia T) with the top of the screen while scrolling down. The black field starts scrolling over other content when it looks like it reached its minimum size.
Warning text:
YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.widget.ScrollView#41290ca0. The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 20, top: 0, right: 20, bottom: -11 (these should all be positive).
This is my code:
<TextView
android:id="#+id/textView"
android:gravity="center"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/header" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp" >
<LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <!-- In a linear layout so it is possible to set it in the middle -->
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtubeplayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
</com.google.android.youtube.player.YouTubePlayerView>
</LinearLayout>
</ScrollView>
So my question is, is there a way to get rid or prevent the black patch from showing?
Currently I have a large workaround, when the user starts scrolling, the youtubeplayer will be released and a image with a play button will take its place, when the user presses the button the youtubeplayer will reinitialize and continue the video.
Actually there is a workaround for this problem. You need to put a listener to your ScrollView and catch the Y distance. When the Y distance is close to the height of the YoutubePlayerView you need to pause the video.
scrollView.getViewTreeObserver()
.addOnScrollChangedListener(() -> {
onScroll(scrollView.getScrollY());
}
});
private void onScroll(int position)
{
if (position>300 && position <400)
{
canPauseOnScroll=false;
}
if (canPauseOnScroll)
{
canPauseOnScroll=false;
ytHelper.getPlayer().pause();
}
}

YouTubePlayerSupportFragment within a SlidingDrawer

I am trying to place a YouTubePlayerSupportFragment within a SlidingDrawer view. However, when it runs the video is stopped after 1 second and the follow is posted in LogCat:
W/YouTubeAndroidPlayerAPI(1778): YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by ViewName{41726520 V.E..... ........ 0,0-720,1038 #7f060040 app:id/drawer}. YouTubePlayerView is completely covered, with the distance in px between each edge of the obscuring view and the YouTubePlayerView being: left: 0, top: 241, right: 0, bottom: 392..
The SlidingDrawer is opened and the video is completely visible and not obscured. Does anyone know a solution to this?
Thanks
I had the same issue, I was changing the margins of the YouTubePlayerSupportFragment,where the distance was negative until it worked. Not sure why this happens though.
from ducumentation youtube player shoudbe topest element and have minimal size of 200*110 dp. If it is smaller - player will stop. If any view (even invisible) will overlap the youtube player - player will stop

Categories

Resources