YouTubePlayerSupportFragment在VerticalViewPager中停止

时间:2018-07-26 14:06:08

标签: android video youtube-api

我正在用VerticalViewPager内的YouTubeVideoPlayer和Images开发一个应用程序。当我滑动到视频片段播放器时,抛出以下错误。

YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by android.widget.

例如,假设我在第二页上有视频。如果我从1st滑动到2nd会起作用,但是当我从3rd滑动到2nd时会出现上述错误。我知道YouTubePlayer不支持任何视图或填充。在我的布局中,播放器顶部没有任何内容。

在FrameLayout @ + id / frame中,添加了YouTubePlayerSupportFragment。在错误日志中,它显示根布局(RelativeLayout)与播放器重叠。

这是我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<layout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:cardBackgroundColor="@color/bg"
        app:cardCornerRadius="@dimen/margin_5"
        app:cardUseCompatPadding="true">

        <LinearLayout 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="wrap_content"
            android:layout_gravity="center"
            android:background="@android:color/white"
            android:orientation="vertical">

            <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="wrap_content"
                android:layout_weight="1">

                <LinearLayout
                    android:id="@+id/linear_main"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <FrameLayout
                        android:id="@+id/frame"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/margin_200" />

                    <RelativeLayout
                        android:id="@+id/rel_control"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@android:color/black"
                        android:padding="@dimen/margin_5">

                        <ImageView
                            android:layout_marginLeft="@dimen/margin_5"
                            android:id="@+id/img_mute"
                            android:padding="@dimen/margin_5"
                            android:layout_width="@dimen/margin_23"
                            android:layout_height="@dimen/margin_23"
                            android:src="@drawable/mute" />
                        <ImageView
                            android:layout_marginRight="@dimen/margin_5"
                            android:id="@+id/img_fullscreen"
                            android:padding="@dimen/margin_5"
                            android:layout_width="@dimen/margin_22"
                            android:layout_height="@dimen/margin_22"
                            android:layout_alignParentRight="true"
                            android:src="@drawable/fullscreen" />
                    </RelativeLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:padding="@dimen/margin_10">

                        <android.support.v7.widget.AppCompatTextView
                            android:id="@+id/txt_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/dejavuserif"
                            android:lineSpacingExtra="@dimen/margin_5"
                            android:maxLines="2"
                            android:text="How to be happy? Ideas from the world's happiest Man!"
                            android:textColor="#292929"
                            android:textSize="@dimen/font_15"
                            android:textStyle="bold" />

                        <android.support.v7.widget.AppCompatTextView
                            android:id="@+id/txt_desc"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/margin_10"
                            android:fontFamily="@font/dejavusans_condensed"
                            android:lineSpacingExtra="@dimen/margin_5"
                            android:text="Buddhist monk, Matthieu Ricard has been called the World's happiest man. It was found that while meditating, Ricard's mind remained at an unusually clam state. Here he shares formula of his happiness."
                            android:textColor="#808080"
                            android:textSize="@dimen/font_14" />

                        <android.support.v7.widget.AppCompatTextView
                            android:id="@+id/txt_web"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/margin_10"
                            android:fontFamily="@font/dejavusans_extralight"
                            android:text="By:Sample Website"
                            android:textColor="#808080"
                            android:textSize="@dimen/font_10"
                            android:visibility="gone" />
                    </LinearLayout>
                </LinearLayout>

                <ImageView
                    android:id="@+id/img_more"
                    android:layout_width="@dimen/margin_50"
                    android:layout_height="@dimen/margin_50"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:padding="@dimen/margin_5"
                    android:layout_margin="@dimen/margin_5"
                    android:src="@drawable/more_arrow"
                    android:visibility="gone" />
            </RelativeLayout>

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/margin_1"
                    android:background="@color/blue_DEDEDF"
                    android:visibility="gone" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/margin_5">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center">

                        <ImageView
                            android:id="@+id/img_like"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:padding="@dimen/margin_10"
                            android:src="@drawable/sel_heart" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center">

                        <ImageView
                            android:id="@+id/img_bookmark"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:padding="@dimen/margin_10"
                            android:src="@drawable/sel_bookmark" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:gravity="center">

                        <ImageView
                            android:id="@+id/img_share"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:background="@drawable/anim_share"
                            android:padding="@dimen/margin_11" />
                    </LinearLayout>

                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>

编辑片段代码

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mYouTubePlayerSupportFragment = YouTubePlayerSupportFragment.newInstance();
    if (getUserVisibleHint()) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.frame, mYouTubePlayerSupportFragment).commit();
        mYouTubePlayerSupportFragment.initialize(Constant.DEVELOPER_KEY, this);
    }
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (!isVisibleToUser && mYoutubePlayer != null) {
        mYoutubePlayer.pause();
        mYoutubePlayer.release();

    }
    if (isVisibleToUser && mYouTubePlayerSupportFragment != null) {

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.frame, mYouTubePlayerSupportFragment).commit();
        mYouTubePlayerSupportFragment.initialize(Constant.DEVELOPER_KEY, this);

    }
}

1 个答案:

答案 0 :(得分:0)

您应该确保在不再显示播放器时暂停播放器。

另一种解决方案是使用此替代播放器android-youtube-player。它是开源的,基于YouTube的网络播放器,因此在叠加视图时没有问题。