2

我一直在嘗試在片段中添加youtube播放器。 有一些覆蓋錯誤。嘗試了不同的東西並解決了它們,現在這個錯誤非常尷尬。 我不知道這裏有什麼問題。YouTubePlayerView被com.google.android.youtube.player.YouTubePlayerView遮擋掉

錯誤是YouTubePlayerView由com.google.android.youtube.player.YouTubePlayerView

完整的錯誤掩蓋:

W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by com.google.android.youtube.player.YouTubePlayerView{2db246cd V.E..... .......D 20,20-596,344}. YouTubePlayerView is completely covered, with the distance in px between each edge of the obscuring view and the YouTubePlayerView being: left: 0, top: 0, right: 0, bottom: 0.. 

MainActivity.java

package onestop.com.youtubefragmenttest; 


    import android.support.v4.app.FragmentManager; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 

public class MainActivity extends AppCompatActivity { 

    public static final String API_KEY = "AIzaSyDlS41-LGiRTToI4GDDRHglf-VY1wCxvtc"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     YoutubeFragment fragment = new YoutubeFragment(); 
     FragmentManager manager = getSupportFragmentManager(); 
     manager.beginTransaction() 
       .replace(R.id.fragment, fragment) 
       .addToBackStack(null) 
       .commit(); 


    } 
} 

YouTubeFragment。 java

package onestop.com.youtubefragmenttest; 


import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Toast; 

import com.google.android.youtube.player.YouTubeInitializationResult; 
import com.google.android.youtube.player.YouTubePlayer; 
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener; 
import com.google.android.youtube.player.YouTubePlayer.Provider; 
import com.google.android.youtube.player.YouTubePlayerSupportFragment; 



/** 
* A simple {@link Fragment} subclass. 
*/ 
public class YoutubeFragment extends Fragment { 

    public static final String API_KEY = "AIzaSyDlS41-LGiRTToI4GDDRHglf-VY1wCxvtc"; 
    public static final String VIDEO_ID = "lfG2-FFL6fY"; 

    public YoutubeFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView = inflater.inflate(R.layout.fragment_youtube, container, false); 

     YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance(); 

     FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
     transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit(); 

     youTubePlayerFragment.initialize(API_KEY, new OnInitializedListener() { 

      // YouTubeプレーヤーの初期化成功 
      @Override 
      public void onInitializationSuccess(Provider provider, YouTubePlayer yPlayer, boolean wasRestored) { 

       if (!wasRestored) { 

        yPlayer.cueVideo(VIDEO_ID); 
       } 
      } 


      // YouTubeプレーヤーの初期化失敗 
      @Override 
      public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) { 
       // YouTube error 
       String errorMessage = error.toString(); 
       Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show(); 
       Log.d("errorMessage:", errorMessage); 
      } 


     }); 

     return rootView ; 

    } 

} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="onestop.com.youtubefragmenttest.MainActivity"> 

    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:name="onestop.com.youtubefragmenttest.YoutubeFragment" 
     android:id="@+id/fragment" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 
</RelativeLayout> 

fragment_youtube.xml

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

    android:visibility="visible" 
    android:padding="10dp"> 

</FrameLayout> 
+0

activity_main.xml中用來代替片段的幀佈局 <的FrameLayout 機器人:layout_width = 「match_parent」 機器人:layout_height = 「250dp」 機器人:layout_alignParentBottom = 「真」 機器人:layout_centerHorizo​​ntal = 「真」 android:id =「@ + id/youtube_frame」> –

回答

0

您可能要檢查,如果這是一個設備特定問題,從這個SO question基於這個問題可能是由於一個透明視圖與YouTubeAndroidPlayer視圖重疊。如果播放器播放時有任何重疊,請檢查您的代碼。

YouTubePlayerFragment

Note that while videos are playing, this View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay this fragment's view with other views while a video is playing.

YouTubePlayerView

This view does not support padding. To achieve the same effect, wrap the view in another ViewGroup or give it margins

希望這有助於!

0

在我的情況下,問題是,我顯示兩次相同的視頻。