【问题标题】:YouTube player in ScrollView androidScrollView android 中的 YouTube 播放器
【发布时间】:2015-08-08 21:04:35
【问题描述】:

当 youtube 播放器片段嵌套在 ScrollView 中时,将设备旋转到横向时出现错误:

YouTubePlayer.ErrorReason.UNAUTHORIZED_OVERLAY

更有趣的是,当我删除 ScrollView 后问题就消失了!但我可以

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        />
        <fragment
            android:name="com.google.android.youtube.player.YouTubePlayerFragment"
            android:id="@+id/youtubeplayerfragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</ScrollView>

【问题讨论】:

  • scrollview 应该只有一个孩子

标签: android youtube youtube-api


【解决方案1】:

YouTubePlayer.ErrorReason.UNAUTHORIZED_OVERLAY 错误表示 *Playback 由于覆盖播放器的视图而停止。这意味着 YouTube 播放器已被其顶部的其他视图遮挡。 YouTube API 可以检测到它并停止播放。

发生这种情况的一个非常常见的原因是当用于保存 YouTube 播放器的片段嵌入到滚动视图中时。 Scroll View 增加了可以滚动的附加层元素。例如,在您的情况下。包含在同一声明中的播放器将检测到重叠并最终停止给出上述错误。

【讨论】:

    【解决方案2】:

    我在 ScrollView 中的 YoutubePlayer 也遇到了同样的问题,并且它在此消息中停止:

    W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.widget.ScrollView{69b88e5 VFED.V... ........ 0,0-1794,1017 #7f0d0070 app:id/scrollview}. The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 21, top: 196, right: 21, bottom: -164 (these should all be positive).
    

    当视频在屏幕上不完全可见时,这种情况总是会发生。当它完全可见时,旋转设备可以正常工作。对我来说,这看起来像是 Youtube Android Player 中的一个错误。我用以下代码做了一个解决方法:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // This is an ugly workaround to prevent youtube from (wrongly) thinking we have an
        // overlay above the video. Having overlays is not allowed and the video would stop otherwise.
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int offset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, metrics);
        scrollView.scrollTo(0, youtubePlayer.getView().getTop() - offset);
    }
    

    这显然不是一个好的解决方法,因为它取决于视频的比例以及它在您的显示器上的显示方式。此外,您的 ScrollView 将在旋转后滚动到不同的位置(您可以稍后手动重新设置)。

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 1970-01-01
      • 2014-11-12
      • 2019-01-11
      • 1970-01-01
      • 2017-11-12
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多