gpt4 book ai didi

android - ViewPagerAdapter : YouTube video playback stopped due to unauthorized overlay on top of player

转载 作者:太空狗 更新时间:2023-10-29 14:40:16 25 4
gpt4 key购买 nike

问这个问题是因为我搜索了几个小时后没有找到解决方案/建议。所有回答的解决方案都带有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com