2016-11-09 96 views
0

我試着去實現一個片段內的YouTube播放器。安卓:錯誤創建YoutTubePlayerView內片段

當我進入屏幕時,我看到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> 

我知道我的工作方法,到目前爲止卻突然不工作了。

當我輸入屏幕上的視頻加載了很短的時間內(幾乎不顯着),後面還有就是這種「黑屏與錯誤信息」 YouTubePlayerSupportFragment工作不正常

回答

0

有時。

請檢查網頁視圖下面的代碼吧。這在我的應用程序中工作正常。

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;} 
+0

嗯,我不想首先使用WebView。 –