ViewPagerAdapter:YouTube视频播放因播放器顶部未经授权的覆盖而停止

时间:2018-03-20 12:10:30

标签: android android-viewpager youtube-data-api android-youtube-api

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


        }

    }

}

我从活动中调用适配器而不是YouTubeBaseActivity

问题: 由于未经授权覆盖播放器,YouTube视频播放停止。 YouTubePlayerView未包含在其祖先android.widget.FrameLayout中。祖先的边缘与YouTubePlayerView的边缘之间的距离为:left:0,top:0,right:0,bottom:0(这些都应该是正数)。< / p>

为什么我收到错误? 当我使用ViewPager加载多个YouTube播放器时。我们知道viewpager会加载next,previous和current项。因此,当前的YouTube视频会被初始化,下一个视频也会被初始化。但是,当前的YouTubePlayer覆盖了下一个(预先加载)。

请帮忙。或者我应该使用任何库来加载YouTube视频。

1 个答案:

答案 0 :(得分:0)

通过删除布局中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