Android Youtube视频在片段中播放时被卡住

Android Youtube视频在片段中播放时被卡住,android,android-fragments,youtube-api,fragment,Android,Android Fragments,Youtube Api,Fragment,我是Android新手。我尝试在加载片段时播放youtube视频。这意味着视频应该在打开应用程序时播放。当我在活动中使用此功能时,它会工作(YouTubePlayerView),但当我使用YouTubePlayerSupportFragment视频时,它会被卡住,无法正常播放 这是youtube的代码: YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

我是Android新手。我尝试在加载片段时播放youtube视频。这意味着视频应该在打开应用程序时播放。当我在活动中使用此功能时,它会工作(YouTubePlayerView),但当我使用YouTubePlayerSupportFragment视频时,它会被卡住,无法正常播放

这是youtube的代码:

YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
    FragmentTransaction transaction =getChildFragmentManager().beginTransaction();
    transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
    youTubePlayerFragment.initialize("AIzaSyA9Kka548d7RZYVBypAy4UWgvDvbUCP16c", new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            if (!b) {
               YPlayer=youTubePlayer;

               // youTubePlayer.cueVideo("EjvfD61PXAw");
                YPlayer.loadVideo("EjvfD61PXAw");
                YPlayer.play();
            }


        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        }
    });
在xml中:

<?xml version="1.0" encoding="utf-8"?>
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_marginBottom="?android:attr/actionBarSize">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    <LinearLayout
        android:id="@+id/youtubecontainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center">

        <FrameLayout
            android:id="@+id/youtube_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:visibility="visible" />

    </LinearLayout>
    <LinearLayout
        android:id="@+id/recently_viewd"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingTop="@dimen/_10sdp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="left"
                android:gravity="left"
                android:text="Recently Viewed"
                android:textColor="@color/black"
                android:textStyle="bold" />

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingTop="@dimen/_10sdp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Recently Added"
                android:textColor="@color/black"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/viewallrecentlyadded"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:gravity="right"
                android:text="view all"
                android:textSize="@dimen/_10sdp" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

移除else块。您只需加载一次视频。@我已将其删除。但没有更改。视频正在启动,但已被卡住:(youTubePlayer是什么?您是如何启动的?请注意我在代码中所做的更改。我已初始化private youTubePlayer YPlayer;很抱歉,没有任何stacktrace或日志,我猜不出问题出在哪里。代码应该可以工作。您也可以发布活动代码。您是否正确地将片段附加到活动?
09-17 15:05:53.071 23505-23505/com.zinedroid.android.atmadarshantv D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
09-17 15:05:54.511 23505-23586/com.zinedroid.android.atmadarshantv V/RenderScript: 0x9f9c2000 Launching thread(s), CPUs 4
09-17 15:05:59.251 23505-23505/com.zinedroid.android.atmadarshantv W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.widget.FrameLayout{1292f567 V.E..... ......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).