Android:无法让 YouTube 播放器 API 在片段内工作

Android: Can't get YouTube Player API work inside of a fragment

我正在尝试让我的应用以片段形式播放 YouTube 视频,正如 This Official Documentation 所说,您可以片段形式播放 YouTube 视频。

但是我做不出来

这是我的代码:

SingleArticleFragment:

public class SingleArticleFragment extends YouTubePlayerSupportFragment implements
        YouTubePlayer.OnInitializedListener {
    public static final String API_KEY = "my api key";
    public static final String YOUTUBE_VIDEO_CODE = "_oEA18Y8gM0";
    // YouTube player view
    private YouTubePlayerView youTubeView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.article, container, false);
        return v;
    }

    @Override
    public void onViewCreated (View view, Bundle savedInstanceState) {
    youTubeView = (YouTubePlayerView) getActivity().findViewById(R.id.youtube_view);

    // Initializing video player with developer key
    youTubeView.initialize(API_KEY, this);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
        if (!b) {

            // loadVideo() will auto play video
            // Use cueVideo() method, if you don't want to play it automatically
            youTubePlayer.cueVideo(YOUTUBE_VIDEO_CODE);

            // Hiding player controls
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        if (youTubeInitializationResult.isUserRecoverableError()) {
            youTubeInitializationResult.getErrorDialog(getActivity(), 1).show();
        } else {
            String errorMessage = "There was an error initializing the YouTubePlayer";
            Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
        }
    }
}

这是article.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/white">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:orientation="vertical">
<!-- Cover Video -->
        <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtube_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="30dp" />

        <!-- Article Cover Photo -->
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/single_article_cover_photo"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:layout_weight=".14"/>

        <!-- Article Title -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/single_article_title"
            android:layout_gravity="center"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:textStyle="bold"
            android:padding="10dp"
            android:layout_weight=".14"/>
    </LinearLayout>
</ScrollView>

LogCat 错误:

android.view.InflateException: Binary XML file line #11: Error inflating class com.google.android.youtube.player.YouTubePlayerView

.
.
.

Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity  which extends YouTubeBaseActivity as its context. 

那么 LogCat 告诉我的是什么:YouTube 仅适用于活动!,但 android 文档另有说明。

有人可以帮忙吗?

提前致谢。

您正在混合使用两种不同的方法

您可以使用 YouTubePlayerView together with a YouTubeBaseActivity or you can simply use a YouTubePlayerFragment/YouTubePlayerSupportFragment。在 YouTubePlayerFragment 中使用 YouTubePlayerView 是完全错误的。

如果您使用第一种方法(视图 + activity),您需要将视图放在 XML 中,然后将 YouTubePlayer 设置为在该视图中播放。

如果您使用第二种方法,您只需将片段加载到适当的容器中,初始化 YouTubePlayer 并播放视频。


解决方案 1

从您的 XML 和您的代码中删除 YouTubePlayerView,并将其替换为包含 YouTubeSupportFragmentFrameLayout。然后,使用 ChildFragmentManagerFrameLayout.

中加载 YouTubeSupportFragment

解决方案 2

只需让您的 Activity 扩展 YouTubeBaseActivity,其他一切保持原样。