18

I'm using the YouTube Android Player API and want to overlay a view on top of a YouTubePlayerFragment in order to display contextual information and controls above the playing video.

Unfortunately it seems playback does not work correctly in either a YouTubePlayerFragment or YouTubePlayerView whenever a there is one or more views stacked above the player in the layout hierarchy. Playback occurs for less than half a second before immediately pausing.

The issue can be reproduced by using the following layout for the Simple PlayerFragment demo that ships with the SDK:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <fragment
      android:name="com.google.android.youtube.player.YouTubePlayerFragment"
      android:id="@+id/youtube_fragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="@android:style/TextAppearance.Small"
      android:layout_alignParentTop="true"
      android:text="@string/playerfragment_text"/>

</RelativeLayout>

I have tried various layout configurations using both FrameLayout and RelativeLayout but the issue is always the same. I realise this API has been released as "experimental", but if this is a known issue it is a pretty major blocker for a lot of implementations. Does anyone have a good solution or workaround?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Jeff Gilfelt
  • 26,131
  • 7
  • 48
  • 47

5 Answers5

16

Jeff - this works as designed. Overlays on top of any of our YouTube players (including the YouTube Android Player) are not supported. When an overlay is detected, playback stops and the log contains information helpful in debugging the issue. We do support Action Bar overlays, take a look at this demo to learn more: https://developers.google.com/youtube/android/player/sample-applications#Overlay_ActionBar_Demo.

Jarek Wilkiewicz
  • 1,124
  • 11
  • 20
  • Thanks Jarek. This seems like something that should be mentioned more prominently in the documentation and perhaps be handled more explicitly at runtime. I can successfully display a dialog fragment over the player and set its background drawable transparent to achieve the desired effect (with a bit more effort) so I'm not sure what this design restriction is attempting to achieve. – Jeff Gilfelt Dec 24 '12 at 02:55
  • Jeff - overlays are currently against our ToS hence the restriction. I agree we should make it more prominent and we're working on making updates to better cover it. – Jarek Wilkiewicz Jan 03 '13 at 00:30
  • @wjarek ToS speaking, is it ok to overlay views once the YouTube player is paused? – eveliotc Feb 06 '13 at 01:47
  • I think it would reset the position of the player if you tried that. The code doesn't even let you do it. – Wenger Jul 16 '13 at 20:00
  • Telling this up front in developer docs is a good idea; I spent lots of time debugging my overlay attempts before I found the limitations mentioned in forums. – Gunnar Forsgren - Mobimation Oct 11 '13 at 14:41
  • @GunnarForsgren-Mobimation can you please put a link where it is said that it is the limitation from the API? I also having this issue and need to have any conclusion on that. – Shreyash Mahajan Nov 13 '14 at 13:00
  • ok I got the link. Please check error which is clear listed in the Document with in below link: Please check *public static final YouTubePlayer.ErrorReason UNAUTHORIZED_OVERLAY* with in below link. https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer.ErrorReason – Shreyash Mahajan Nov 14 '14 at 12:47
  • Can you please explain the reason behind this decision? Right now I am implementing youtube iframe api in webview, just to be able to overlay it. – Ilya Gazman Jul 26 '15 at 16:14
  • When I add a fragment after adding a you tube fragment in container, it also shows unauthorized overlay. But I want to add fragment because in my app, use should be able to see previous watched video. Any solution to this? – Krupal Shah Oct 09 '16 at 07:12
  • @JarekWilkiewicz Thanks! I am getting same error in ViewPager Adapter. I am using framelayout with YouTubePlayerFragment. Here is the detail question https://stackoverflow.com/q/49383873/1468354 Hoping you will help/respond. – AkshayT Mar 20 '18 at 14:46
4

Youtube Player don't allow any view overlay it. Just using setVisibility(View.GONE) for all view overlay it.

BaDo
  • 540
  • 8
  • 19
3

It is not possible to add buttons as overlays above the player as specified by Google, otherwise the player will stop:

https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayerView

Note that while videos are playing, this View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay the view with other views while a video is playing.

This view does not support padding. To achieve the same effect, wrap the view in another ViewGroup or give it margins.

Padding is also not supported on YouTubePlayer.

To overlay your view on video, I will recommend you to use ExoPlayer, It is not part of the android sdk, but it's recommended by google and included in android developer documentation :

http://google.github.io/ExoPlayer/

It's also good to mention that Exoplayer is used in youtube application.

Renaud Boulard
  • 709
  • 6
  • 9
0

You can set the player theme to YouTubePlayer.PlayerStyle.CHROMELESS and then implement your own controls below.

I am having an issue where I can not find a good resource on how to re-implement the seek bar with a custom layout.

wapples
  • 183
  • 2
  • 12
0

Volla !! I have got one scenario where this issue was coming in devices lollipop and above and it was because of the status bar . I hide the status bar problem solved. Just wrote it as an answer in case if its helps others :)

Preethi Rao
  • 5,117
  • 1
  • 16
  • 29