【问题标题】:Problem about YouTubePlayer API for an application within a nested Fragment关于嵌套片段中应用程序的 YouTubePlayer API 的问题
【发布时间】:2019-02-22 13:25:48
【问题描述】:

我正在尝试在 Fragment 中使用 recyclerview 设置 Youtube 视频列表。 我在那个活动中有一个活动,一个片段在那里管理底部导航选择,另一个片段用于管理选项卡选择的 tablayout,最后是适配器,我有我的 recyclerview,它正在设置视频列表一切都按我的计划工作,但是当我播放视频它 YoutubePlayerAPI 暂停视频。 我尝试通过在 Google 上搜索来解决此问题,但无法解决。

下面是我的 video_recycler_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    android:background="@android:color/white"
    android:clipToPadding="false"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:padding="4dp"
    android:orientation="vertical">
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/ivFeedCenter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            fresco:placeholderImage="@color/colorPrimary"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#4f000000"/>
        <ImageView
            android:id="@+id/play_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/ic_video_play"/>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/video_container"
            android:tag="@string/video_component_tag">

        </FrameLayout>
    </RelativeLayout>
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

谁能告诉我我做错了什么?

我在 logcat 中收到了我在下面提到的消息

W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.support.v7.widget.RecyclerView{f61352e VFED..... ........ 0,0-1080,1560 #7f0a01df app:id/rvFeed}. The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 24, top: 1402, right: 24, bottom: -422 (these should all be positive).

【问题讨论】:

  • 这个错误通常是由他们的 API 引发的。长话短说,你不必用任何东西覆盖视频。根据您的 xml,我猜布局底部的一个/两个 TextViews 可能会覆盖播放器..先检查一下。我找到了this 可能对您有所帮助的示例
  • 我删除了那些TextViews 但没有解决我的问题
  • 看看我编辑的评论。那个 git 样本
  • 我已经看到了这个示例,我正在从相同的示例中获取示例,但无法正常工作
  • The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 24, top: 1402, right: 24, bottom: -422 (these should all be positive). 您要在屏幕上播放的视频还是什么?看起来它在recyclerview的可见部分下方滚动。另外,您的 XML 中的 YoutubePlayerView 在哪里?

标签: android android-fragments youtube-api android-youtube-api


【解决方案1】:

为了避免暂停,您需要隐藏覆盖您的框架布局的所有视图,因为我看到您的框架是 match_parent。当您单击播放视频时,请隐藏所有内容并尝试。还要看看你是否没有 FloatingActionButton 或其他视图覆盖。

【讨论】:

    【解决方案2】:

    我认为当您点击播放按钮时,framLayout 的高度为 0 dp,并且 youtubePlayer.init("somevideID") 将尝试播放视频,但由于高度为 0 dp,它只需插入视频并暂停视频.

    这就是发生的事情:

    **Insert video 
    Play the Video 
    Height is Zero at insertion time 
    So it pauses the video instantly**
    

    所以做一件事添加高度,比如 200dp 到 frameLayout 并使其不可见。现在之前调用 youtubePlayer.init() 使 frameLayout 可见。然后然后调用 youtubePlayer.init()。这将解决您的问题。

    【讨论】:

    • 我按照你的建议试过了,但同样的问题W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by android.widget.RelativeLayout{f0fb057 V.E...... ........ 0,0-1080,1848}. YouTubePlayerView is completely covered, with the distance in px between each edge of the obscuring view and the YouTubePlayerView being: left: 12, top: 646, right: 12, bottom: 602..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多