Android:在Fragment中创建YoutTubePlayerView时出错

时间:2016-11-09 10:30:35

标签: android android-fragments android-youtube-api

我试图在片段中实现YouTube播放器。

当我进入屏幕时,我会看到YouTube播放器,但它只是一个黑色背景和一个文字说"初始化YouTube播放器时发生错误"。

相应的错误堆栈:

 Error creating YouTubePlayerView
 com.google.android.youtube.player.internal.w$a: Exception thrown by invoked constructor in com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer
     at com.google.android.youtube.player.internal.w.a(Unknown Source)
     at com.google.android.youtube.player.internal.w.a(Unknown Source)
     at com.google.android.youtube.player.internal.w.a(Unknown Source)
     at com.google.android.youtube.player.internal.ac.a(Unknown Source)
     at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source)
     at com.google.android.youtube.player.YouTubePlayerView$1.a(Unknown Source)
     at com.google.android.youtube.player.internal.r.g(Unknown Source)
     at com.google.android.youtube.player.internal.r$c.a(Unknown Source)
     at com.google.android.youtube.player.internal.r$b.a(Unknown Source)
     at com.google.android.youtube.player.internal.r$a.handleMessage(Unknown Source)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6077)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 Caused by: java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Constructor.newInstance0(Native Method)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
     ... 16 more
 Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class <unknown>
     at org.holoeverywhere.LayoutInflater._createView(LayoutInflater.java:307)
     at org.holoeverywhere.LayoutInflater.onCreateView(LayoutInflater.java:513)
     at org.holoeverywhere.LayoutInflater.createViewFromTag(LayoutInflater.java:359)
     at org.holoeverywhere.LayoutInflater.rInflate(LayoutInflater.java:647)
     at org.holoeverywhere.LayoutInflater.rInflate(LayoutInflater.java:650)
     at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:450)
     at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:415)
     at tad.<init>(SourceFile:57)
     at grj.<init>(SourceFile:203)
     at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.<init>(SourceFile:181)
     at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.<init>(SourceFile:156)
     at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.<init>(SourceFile:143)
     ... 18 more
 Caused by: java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Constructor.newInstance0(Native Method)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
     at org.holoeverywhere.LayoutInflater._createView(LayoutInflater.java:281)
     ... 29 more
 Caused by: java.lang.IllegalStateException: Could not find font in raw resources: roboto_medium
     at org.holoeverywhere.FontLoader$RawLazyFont.setRawResourceId(FontLoader.java:575)
     at org.holoeverywhere.FontLoader$RawLazyFont.loadTypeface(FontLoader.java:568)
     at org.holoeverywhere.FontLoader$Font.getTypeface(FontLoader.java:283)
     at org.holoeverywhere.FontLoader$FontCollector.getTypeface(FontLoader.java:368)
     at org.holoeverywhere.FontLoader$FontCollector.getTypeface(FontLoader.java:400)
     at org.holoeverywhere.FontLoader.applyInternal(FontLoader.java:154)
     at org.holoeverywhere.FontLoader.apply(FontLoader.java:128)
     at org.holoeverywhere.FontLoader.applyDefaultFont(FontLoader.java:134)
     at org.holoeverywhere.widget.TextView.setFontStyle(TextView.java:91)
     at org.holoeverywhere.widget.TextView.setFontStyle(TextView.java:156)
     at org.holoeverywhere.widget.TextView.setTextAppearance(TextView.java:133)
     at org.holoeverywhere.widget.TextView.construct(TextView.java:41)
     at org.holoeverywhere.widget.TextView.<init>(TextView.java:30)
     at org.holoeverywhere.widget.TextView.<init>(TextView.java:25)
     ... 32 more

在我的片段中我有这个方法来初始化播放器:

private void initYouTubeVideo(final String youTubeVideoId) {
    youtubeVideoContainer.setVisibility(View.VISIBLE);

    YouTubePlayerSupportFragment youTubePlayerSupportFragment = YouTubePlayerSupportFragment.newInstance();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.replace(R.id.youtube_video_fragment, youTubePlayerSupportFragment).commit();

    youTubePlayerSupportFragment.initialize(getResources().getString(R.string.youtube_key),
            new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                    YouTubePlayer youTubePlayer, boolean b) {
                    YouTubePlayer player;

                    if (!b) {
                        player = youTubePlayer;
                        player.setFullscreen(false);
                        player.loadVideo(youTubeVideoId);
                        player.play();
                    }
                }

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

我的xml布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@+id/root_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/fragment_advice_detail_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <!-- Some Views ... -->

            <RelativeLayout
                android:id="@+id/youtube_video_container"
                android:layout_width="match_parent"
                android:layout_height="240dp"
                android:visibility="gone">
                <FrameLayout
                    android:id="@+id/youtube_video_fragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerHorizontal="true"
                    android:visibility="visible"/>
            </RelativeLayout>


            <!-- More Views ... -->

        </LinearLayout>
    </ScrollView>

    <FrameLayout
        android:id="@+id/progressbar_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/progressBackgroundEnabled"
        android:clickable="true"
        android:visibility="gone">
    </FrameLayout>

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:indeterminate="true"
        android:indeterminateOnly="true"
        android:visibility="gone"/>
</FrameLayout>

我知道我的方法到目前为止工作但突然不再起作用了。

当我进入屏幕时,视频会加载很短的时间(几乎不会引人注意),之后会出现这个&#34;黑屏并显示错误消息&#34;

1 个答案:

答案 0 :(得分:0)

有时,YouTubePlayerSupportFragment无法正常运行。

请使用WebView在下面的代码中查看。这在我的应用程序中运行良好。

 youTubePlayerView = (WebView) view.findViewById(R.id.youtube_view)if (videoUrl != null && !videoUrl.isEmpty()) {
    youTubePlayerView.getSettings().setJavaScriptEnabled(true);
    youTubePlayerView.getSettings().setPluginState(WebSettings.PluginState.ON);
    youTubePlayerView.setWebChromeClient(new WebChromeClient() {
    });
    final String mimeType = "text/html";
    final String encoding = "UTF-8";
    String html = getHTML(Utils.getYouTubeVideoId(videoUrl));
    youTubePlayerView.loadDataWithBaseURL("", html, mimeType, encoding, "");}


public String getHTML(String videoId) {
String html =
        "<iframe class=\"youtube-player\" "
                + "style=\"border: 0; width: 100%; height: 95%;"
                + "padding:0px; margin:0px\" "
                + "id=\"ytplayer\" type=\"text/html\" "
                + "src=\"http://www.youtube.com/embed/" + videoId
                + "?fs=0\" frameborder=\"0\" " + "allowfullscreen autobuffer "
                + "controls onclick=\"this.play()\">\n" + "</iframe>\n";
return html;}