安卓工作室-如何避免VideoView溢出其(视图)容器



我在一个水平的Linearlayout中有3个VideoViews,我希望每个都播放不同的视频,每个视频将只显示其原始视频的一部分(因此,每个视频都包含在Linearlayout中,将显示的内容从150dp限制为150dp(。问题是视频重叠。作为一个例子,我展示了我只填充VideoView Central的代码,但它侵犯了其他VideoView的区域,我已经尝试了一切,但我无法使它们不相互侵犯。知道怎么修吗。注意:VideoView必须大于其150x150dp LinearLayout容器。

在该图中,我显示实际错误和需要的正确结果

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.widget.MediaController
import com.uncdf.unitlife.R
import com.uncdf.unitlife.util.ActBase
import com.uncdf.unitlife.util.TestCustomVideoView
import com.uncdf.unitlife.util.Utiles
class MainActivity : ActBase() {
override fun onCreate(savedInstanceState: Bundle?) {
try{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}catch (ex: java.lang.Exception){
Utiles.manageViewException(ex, baseContext, getEventCode("0001"))
}
}
fun init(){
initVideo(R.id.viewVideoX2)

}
fun initVideo(idViewVideo : Int){
val video = findViewById<TestCustomVideoView>(idViewVideo)
video?.setMediaController(MediaController(this));
video?.setOnPreparedListener(MediaPlayer.OnPreparedListener() {
// it.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
it.isLooping = true
// itemInfo.videoMediaPlayer = it
})
val metrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(metrics)
video?.setContainer(736, 412, true)
// "http://www.demonuts.com/Demonuts/smallvideo.mp4"
var rutaVideo =  "android.resource://" + packageName + "/" + R.raw.smallvideo;
video?.setVideoURI(Uri.parse(rutaVideo))
video?.requestFocus()
video?.seekTo(5)
video.setZOrderMediaOverlay(false)
// viewVideoFull?.start()
}
}

import android.content.Context;
import android.graphics.Canvas;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Size;
import android.widget.VideoView;
public class TestCustomVideoView extends VideoView {
private Size finalVideoSize = new Size(0,0);
private boolean isOut = false;
public TestCustomVideoView(Context context) {
super(context);
}
public TestCustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestCustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setContainer(int width, int height, boolean outContainer){
this.finalVideoSize = new Size(width, height);
setMeasuredDimension(finalVideoSize.getWidth(), finalVideoSize.getHeight());
}
@Override
public void setVideoURI(Uri uri) {
super.setVideoURI(uri);
if(finalVideoSize.getWidth() > 0 && finalVideoSize.getHeight() > 0)
setMeasuredDimension(finalVideoSize.getWidth(), finalVideoSize.getHeight());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
if(finalVideoSize.getWidth() > 0 && finalVideoSize.getHeight() > 0)
setMeasuredDimension(finalVideoSize.getWidth(), finalVideoSize.getHeight());
else
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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_width="match_parent"
android:layout_height="match_parent"
tools:context=".temporal.MainActivity">
<LinearLayout
android:id="@+id/viewContainer"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:orientation="horizontal"
android:background="@android:color/holo_orange_light"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent=".9">
<LinearLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
android:background="@android:color/holo_green_light"
android:layout_marginLeft="20dp">
<com.uncdf.unitlife.util.TestCustomVideoView
android:id="@+id/viewVideoX1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
android:background="@android:color/holo_green_light"
android:layout_marginLeft="20dp">
<com.uncdf.unitlife.util.TestCustomVideoView
android:id="@+id/viewVideoX2"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
android:background="@android:color/holo_green_light"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<com.uncdf.unitlife.util.TestCustomVideoView
android:id="@+id/viewVideoX3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

嘿,我偶然发现了类似的东西,我发现的问题是VideoView原生扩展了surfaceView。所以我选择不尝试用textureView播放视频,这是我使用的库,它帮助我处理了这个错误。可扩展视频视图

如果您在实施此库时遇到任何其他问题,请告诉我

相关内容

  • 没有找到相关文章

最新更新