视频超出了 Android 中 VideoView 的边界

Posted

技术标签:

【中文标题】视频超出了 Android 中 VideoView 的边界【英文标题】:video extending past the bounds of a VideoView in Android 【发布时间】:2019-08-22 15:28:14 【问题描述】:

我目前正在处理一些旧代码,这些代码以前在应用程序中用作弹出窗口,在某些文本旁边显示图像。我正在更改布局,将其从 ImageView 更改为 VideoView,以使其显示视频。我已将视频视图设置为仅跨越屏幕的某个部分,但视频并未停留在这些范围内。它按预期从这些边界的左上角开始,但跨越所有其他方向并越过屏幕的底部和右侧边缘。

Here is the layout view from android Studio, The red line shows how the video is showing. It should stay (scale?) within the square video view where the top left corner of the video begins

旧代码在另一个带有重叠文本的 ImageView 旁边使用 ImageView 以构成弹出窗口。这些元素的父级是 PercentRelativeLayout。我尝试添加另一个相对布局,它是弹出窗口的总大小,然后在其中添加视频和图像视图(结构:百分比(全屏)-> 百分比(弹出窗口大小)-> 视频(父级的左半部分) & 图像(父母的右半边))。视频仍在扩展范围内。 我也尝试过使用约束布局而不是百分比布局,但它没有效果。 我在使用相同操作系统但硬件更快的不同设备上运行相同的代码,并且代码按预期工作。 除了设置视频源之外,我不会在代码中更改视频视图。

<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:background="@android:color/transparent"
    tools:context="com.my.package.questionnaire.fragments.InterestQuestionFragment">

    <android.support.percent.PercentRelativeLayout
        android:layout_gravity="center"
        app:layout_aspectRatio="177%"
        app:layout_widthPercent="100%">

        <android.support.percent.PercentRelativeLayout
            android:background="#000000"
            app:layout_heightPercent="64%"
            app:layout_marginStartPercent="13%"
            app:layout_marginTopPercent="18%"
            app:layout_widthPercent="74%">

            <ImageView
                android:src="@drawable/info_popup_background"
                app:layout_heightPercent="100%"
                app:layout_marginStartPercent="50%"
                app:layout_widthPercent="50%" />

            <VideoView
                android:id="@+id/info_popup_accessory_video_view"
                app:layout_heightPercent="100%"
                app:layout_widthPercent="50%" />

        </android.support.percent.PercentRelativeLayout>

    </android.support.percent.PercentRelativeLayout>

</android.support.percent.PercentFrameLayout>

预期结果是视频保持在 VideoView 的范围内。视频不大,应该已经与我正在创建的 VideoView 大小相同。它似乎被拉长了,但它不应该。

【问题讨论】:

【参考方案1】:

以防万一其他人遇到此问题,我将列出我找到的解决方案。 事实证明,这个问题出现在 Android 4.4.2 而不是 4.4.4。为了找到解决此问题的方法,我转向了可扩展视频视图库,它本质上只是 mediaPlayer 的一个轻包装器,它通过视频侦听器更改其纹理视图,可以在以下位置找到 https://github.com/yqritc/Android-ScalableVideoView.

这在 textureView 中使用了 MediaPlayer。 该库修复了视频缩放问题,但引入了一个不同的记录错误,该错误会在发布媒体播放器时冻结应用程序。 https://issuetracker.google.com/issues/36905656

为了解决这个问题,我深入到库中,剥离了 MediaPlayer 和所有相关的侦听器和方法,并实现了 Google 的 ExoPlayer。图书馆现在完全不同了,所以如果有人有兴趣,我可以提供。

现在,一切都按预期工作,没有缩放或冻结问题。

如果有人想查看此代码,请告诉我,我可以详细说明!

【讨论】:

以上是关于视频超出了 Android 中 VideoView 的边界的主要内容,如果未能解决你的问题,请参考以下文章

使用VideoView播放视频

Android 使用VideoView实现简单视频播放

Android开发之使用VideoView播放视频

如何在 Android 中捕获 VideoView 的屏幕截图或视频帧

Android - VideoView 不会播放有声视频

[Android基础] VideoView