问这个问题是因为我在搜索了几个小时后没有找到解决方案/建议。所有回答的解决方案都带有 Fragment。我正在寻找 ViewPagerAdapter 和 FrameLayout。
我的 ViewPagerAdapter xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/promotion_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/some_image"
android:layout_width="match_parent"
android:layout_height="@dimen/view_pager_height"
android:scaleType="fitXY" />
<FrameLayout
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//Some other view items
</RelativeLayout>
我的 ViewPagerAdapter Java 代码:
public class ArticleViewPagerAdapter extends PagerAdapter {
public String TAG = ArticleViewPagerAdapter.class.getSimpleName();
private ArrayList<Article> mArticleList = new ArrayList<>();
private Activity mContext;
private LayoutInflater mLayoutInflater;
private FragmentManager mFragmentManger;
private YouTubePlayerListener mYouTubePlayerListener;
public ArticleViewPagerAdapter(Activity context, ArrayList<Article> articleList, FragmentManager fragmentManager, YouTubePlayerListener youTubePlayerListener) {
this.mContext = context;
this.mArticleList = articleList;
this.mFragmentManger = fragmentManager;
this.mYouTubePlayerListener = youTubePlayerListener;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mArticleList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == (object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.activity_news, container,
false);
itemView.setTag(position);
updateView(itemView, position);
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
private void updateView(View itemView, int position) {
final int index = position;
final View finalView = itemView;
//Initializing the view items
final ImageView mNewsImage = itemView.findViewById(R.id.some_image);
final FrameLayout mYoutubeFragment = itemView.findViewById(R.id.youtube_fragment);
//Let's Test using this
final YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
ResArticle articleResponse = response.body(); //Got response from API
if (articleResponse != null && articleResponse.getData() != null) {
final Article article = articleResponse.getData();
final String pageUrl = SHARE_BASE_URL + article.getArticleId();
if (article != null) {
if (article.getArticleType()==Constants.ARTICLE_TYPE_NEWS) {
//Basically setting visibility But you can ignore this part
mNewsImage.setVisibility(View.VISIBLE);
mYoutubeFragment.setVisibility(View.GONE);
}
if(article.getArticleType()==Constants.ARTICLE_TYPE_VIDEO) {
Log.d(TAG,"Article Type is Video");
mYoutubeFragment.setVisibility(View.VISIBLE);
mNewsImage.setVisibility(View.GONE);
youTubePlayerFragment.initialize(mContext.getString(R.string.web_client_id), new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
youTubePlayer.setShowFullscreenButton(false);
youTubePlayer.cueVideo("video_id");
if (mYouTubePlayerListener!=null)
mYouTubePlayerListener.setYouTubePlayer(youTubePlayer);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.e(TAG, "Youtube Video initialization failed");
}
});
FragmentTransaction transaction = mFragmentManger.beginTransaction();
transaction.replace(R.id.youtube_fragment, youTubePlayerFragment).commit();
}
}
} else {
Toast.makeText(mContext, mContext.getString(R.string.article_info_not_found), Toast.LENGTH_SHORT).show();
Log.e(TAG, "Article info not found");
}
}
}
}
我从 Activity 而不是
YouTubeBaseActivity
调用适配器。问题:
YouTube 视频播放因未经授权覆盖在播放器顶部而停止。 YouTubePlayerView 不包含在其祖先 android.widget.FrameLayout 祖先边缘与 YouTubePlayerView 边缘之间的距离是:left: 0, top: 0, right: 0, bottom: 0(这些都应该是正数)。
为什么我收到错误?
当我使用 ViewPager 加载多个 YouTube 播放器时。正如我们所知,viewpager 会加载下一个、上一个和当前项目。所以当前的 YouTube 视频被初始化,下一个也是如此。但是作为当前的 YouTubePlayer 覆盖了下一个(预加载的)。
请帮忙。或者我应该使用任何库来加载 YouTube 视频。
最佳答案
通过删除布局中 YouTubePlayerView 中的填充来解决此问题。所以你的布局看起来像这样:
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/video_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000" />
为什么在适配器中使用
instantiateItem
而不是 getItem
?关于android - ViewPagerAdapter : YouTube video playback stopped due to unauthorized overlay on top of player,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49383873/