我對YouTubePlayerView和YouTubePlayerFragment都有一個非常奇怪的問題。YouTubePlayer在某些設備上有時會暫停1秒
我的應用程序是一個鬧鐘,我希望我的用戶能夠將YouTube視頻設置爲他們的鬧鐘。
這裏是警戒片段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>
這是活動的佈局的XML文件,其中包含片段:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我測試了報警2個智能手機(Nexus 5的和華爲)和一個平板電腦(華碩)。起初,我試圖迫使應用程序是對景觀,如果它是通過使用youtueb報警:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
的事情是,在Nexus 5的視頻幾乎總是在1秒鐘後停止。如果我從現在開始設置鬧鐘2分鐘,它會在50%的嘗試中正常播放,如果我明天早上設置了鬧鐘,它會在1秒後停止。在華爲,它幾乎總是可以正常播放,而在平板電腦上則是50/50。 這是消息我得到當視頻停止:
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).
我沒有在報警警報片段的任何FrameLayout裏,但我有一個包含片段,所以我取代了的FrameLayout活動XML文件的FrameLayout與片段,因此XML文件看起來像這樣:
<?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" />
,但我仍然得到同樣的錯誤消息,即使目前還沒有在任何XML的FrameLayout。我的應用中有插頁式廣告,因此我取消了廣告,但錯誤仍然存在。
所以我試圖設置活動的肖像,而不是風景,現在它在聯機和華爲的大部分時間工作,但幾乎總是在1秒後停止在平板電腦上。
我還注意到,當我在我的Activity中包含YouTubePlayerFragment時,或者當我擴展YouTubeBaseActivity時,每當我開始活動時,它都會創建2個活動的實例而不是1,無論我添加到意圖或啓動模式(我不知道它是否與問題有關)
我可以在WebView上使用RTSP鏈接播放YouTube,但爲了提取RTSP鏈接,我需要使用此項目: https://github.com/HaarigerHarald/android-youtubeExtractor
開發人員告訴我,他們通過後門解壓縮RTSP網址,這可能與Yotube政策相違背。
我希望YouTube播放器能夠在所有設備上工作。有沒有人有想法?
您可以在xml文件上看到最小寬度和高度已經設置爲200 * 110dp,並且YouTube播放器沒有任何重疊。另外,沒有填充。 – SUXA