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

戏子无情 提交于 2019-12-05 02:09:06

问题


Asking this question because I did not find solution/suggestions after searching for hours. All answered solutions are with Fragment. What I am looking for ViewPagerAdapter and FrameLayout.

My 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>

My ViewPagerAdapter Java code:

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");
        }


        }

    }

}

And I am calling the adapter from the Activity NOT the YouTubeBaseActivity.

Problem: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.widget.FrameLayout The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 0, top: 0, right: 0, bottom: 0 (these should all be positive).

Why I am getting the error? As I am loading Multiple YouTube Player using the ViewPager. As we know the viewpager loads next, previous and current item. So current YouTube video gets initialized and so does the next one. But As current YouTubePlayer overlays the next one(which is pre-loaded).

Please help. Or should I use any library to load YouTube videos.


回答1:


fix this by removing the padding in the YouTubePlayerView in the layout. So your layout looks like this:

<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/video_player"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000" />

why you are using instantiateItem instead of getItem in adapter?



来源:https://stackoverflow.com/questions/49383873/viewpageradapter-youtube-video-playback-stopped-due-to-unauthorized-overlay-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!