Android Exo Player,在视频顶部的特定位置添加一个视图

问题描述

我正在 ExoPlayer 上加载视频。在播放视频时,我想在正在播放的视频之上加载一些 UI 元素。为了实现这一点,我有一个如下所示的布局,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000">
        <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/exoplayerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />
        <RelativeLayout
            android:id="@+id/overlayContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"/>
    </RelativeLayout>
</FrameLayout>

现在我正在尝试将自定义 UI 元素放置在覆盖容器上。我有实际尺寸为 originalWidthoriginalHeight 的视频,我试图从这个回调中获取,

simpleExoplayer.addVideoListener(object: VideoListener {
            override fun onSurfaceSizeChanged(width: Int,height: Int) {
                super.onSurfaceSizeChanged(width,height)
                originalVideoWidth = width
                originalVideoHeight = height
            })

现在,当视频在设备上呈现时,我使用此回调获取当前的宽度和高度,

override fun onVideoSizeChanged(
                eventTime: AnalyticsListener.EventTime,width: Int,height: Int,unappliedRotationDegrees: Int,pixelWidthHeightRatio: Float
            ) {
                super.onVideoSizeChanged(eventTime,width,height,unappliedRotationDegrees,pixelWidthHeightRatio)

                newVideoWidth = width
                newVideoHeight = height
                widthHeightRatio = pixelWidthHeightRatio
}

在原始视频中,项目被放置在 xy,当视频被渲染时,它在设备上获得了新的宽度和高度,这些宽度和高度来自上面的方法。现在我必须根据渲染视频的纵横比找到新坐标 x1y1

为此,我正在使用此代码,

topMargin = (offsetY + (itemToBePlaced.top?.div(originalVideoHeight.toFloat()) ?: 0.0f).times(newVideoHeight)).toInt()
leftMargin = (offsetX + ((itemToBePlaced.left?.div(originalVideoWidth.toFloat()) ?: 0.0f) * newVideoWidth)).toInt()

在这种情况下,我不确定如何获取 offsetY 的正确值,例如,我必须放置的视图在原始视频中的坐标位置为 (10,10)。现在,当它在设备上呈现时,我必须将项目准确地放置在相对于新视频比率的位置 (10,10)。

当视频在设备上呈现时,它并不总是从 (0,0) 开始,有时视频从 (0,20) 开始基于宽高比。所以如果视频从 (0,20) 开始,我的元素必须放在 (0,30) 处。

我不确定如何在 exoplayer 视图中成功找到视频的来源,在上面的示例中,视频从 y=20 开始。

如何正确找到视频开始的 y 位置?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...