我一直在尝试在片段中添加YouTube播放器。
有一些重叠错误。尝试了不同的方法并解决了它们,现在错误非常尴尬。
我不知道这是怎么回事。
错误是com.google.android.youtube.player.YouTubePlayerView遮盖了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>
最佳答案
就我而言,问题是我两次显示相同的视频。