Tab片段中的Youtube播放器视图

问题描述 投票:0回答:1

我只是在我的一个Tab片段中使用YoutubePlayer,但无法集成它!

使用此代码,它给了我这个错误。

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

这是我的Fragment类。

public class Frag_Trailer extends Fragment {

    YouTubePlayerView mYouTubePlayerView;
    YouTubePlayer.OnInitializedListener mOnInitializatedListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_trailer, container, false);
        mYouTubePlayerView = (YouTubePlayerView)rootView.findViewById(R.id.view_youtube);
        mYouTubePlayerView.initialize(YouTubeConfig.getYoutubeApiKey(), mOnInitializatedListener);

        mOnInitializatedListener = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.loadVideo("_XgQA9Ab0Gw");
            }

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

        return rootView;
    }
}

以下是我在build.gradle中的依赖关系。

dependencies { 
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:cardview-v7:23.3.0' 
    implementation 'com.android.support:design:23.3.0' 
    implementation files('libs/YouTubeAndroidPlayerApi.jar') 
} 
android fragment
1个回答
0
投票

基于这个answer

你必须用YouTubeBaseActivity扩展你的活动。但在您的情况下,您在Fragment上实现它,因此请更改您的视图XML

<fragment
  android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
  android:id="@+id/youtubesupportfragment"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>
© www.soinside.com 2019 - 2024. All rights reserved.