I have a very strange problem with both YouTubePlayerView and the YouTubePlayerFragment.
My app is an alarm clock and i want my users to be able to set a youtube video as their alarm.
Here is the alert fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/standard_button_size"
android:gravity="center_vertical">
<TextView
android:id="@+id/tvSep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:text="-"
android:textColor="@color/white_secondary_text"
android:textSize="@dimen/title_bold" />
<TextView
android:id="@+id/tvNotificationHourYoutube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_toStartOf="@id/tvSep"
android:gravity="end"
android:text="7:00"
android:textColor="@color/white_secondary_text"
android:textSize="@dimen/title_bold" />
<TextView
android:id="@+id/tvAlarmLabelYoutube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@id/tvSep"
android:gravity="center"
android:maxLines="1"
android:text="Wake up"
android:textColor="@color/white_secondary_text"
android:textSize="@dimen/title_bold" />
</RelativeLayout>
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="@+id/ypvAlert"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="@dimen/half_activity_vertical_margin"
android:layout_marginTop="@dimen/half_activity_vertical_margin"
android:minWidth="200dp"
android:minHeight="110dp"/>
<LinearLayout
android:id="@+id/llYoutubeButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatButton
android:id="@+id/btnStopAlarmBigYoutube"
android:layout_width="0dp"
android:layout_height="@dimen/standard_button_size"
android:layout_weight="1"
android:backgroundTint="@color/dark_primary_accent"
android:text="@string/stop_alarm"
android:textColor="?android:colorBackground"
android:textSize="@dimen/title_bold"
android:visibility="visible" />
<Button
android:id="@+id/btnSnoozeYoutube"
android:layout_width="0dp"
android:layout_height="@dimen/standard_button_size"
android:layout_weight="1"
android:text="@string/snooze"
android:textSize="@dimen/title_bold" />
</LinearLayout>
</LinearLayout>
And this is the layout xml file of the activity that contains the fragment:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I tested the alarm on 2 smartphones (Nexus 5 and huawei) and on one tablet (Asus). At first i tried to force the app to be on landscape if it is a youtueb alarm by using:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
The thing is that on the nexus 5 the video is almost always stops after 1 second. If i set an alarm for 2 minutes from now it will play ok for 50% of the attempts, and if i set an alarm for tomorrow morning it always stops after 1 second. On the Huawei it almost always play ok and on the tablet it's 50/50. This is the message i get when the video stops:
YouTube video playback stopped due to unauthorized overlay on top of player.
The YouTubePlayerView is not contained inside its ancestor
android.widget.FrameLayout{89c240f V........ ......I. 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).
I don't have any FrameLayout on the alarm alert fragment but i have a FrameLayout on the activity xml file that contains the fragment so i replaced the FrameLayout with fragment, so the xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/articles_list"
android:name="com.sux.alarmclock.AlarmNotificationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_alarm_alert_material_youtube" />
But i still get the same error message even though now there is not any FrameLayout in the xml. I have interstitial ads in my app so i canceled the ads but the error still occurs.
So i tried to set the activity to portrait instead of landscape and now it's working on the nexus and on huawei most of the times but almost always stops on the tablet after 1 second.
I also noticed that when i include the YouTubePlayerFragment inside my Activity , or when i extend the YouTubeBaseActivity, every time i start the activity, it creates 2 instances of the activity instead of 1, regardless to the flags i add to the intent or the launch mode (I don't know if it has something to do with the issue)
I can play YouTube on WebView using the RTSP links, but in order to extract the RTSP link i need to use this project: https://github.com/HaarigerHarald/android-youtubeExtractor
And the developer told me that they are extracting the RTSP urls through backdoor which might be aganist the Yotube policies.
I want the YouTube player to work on all devices all the times. Does anyone has an idea?