android-videoview 相关问题

用于管理和显示视频文件的Android视图。

Android 视频视图crop_center

我有一个RelativeLayout 我有一个RelativeLayout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:foregroundGravity="center" android:gravity="center" android:orientation="horizontal" > <VideoView android:id="@+id/videoViewPanel" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:layout_centerInParent="true"/> </RelativeLayout> 我需要的是全屏显示裁剪后的视频。如果我可以与 ImageView 进行比较,我需要将其显示为crop_center。 如何使 VideoView 不自动调整视频大小以适合中心,而是裁剪中心? 在Android的VideoView中,这里有一个简单易行的方法,可以达到与ImageView.ScaleType.CENTER_CROP相同的效果 xml <?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" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/videoView" android:layout_width="@dimen/dimen_0dp" android:layout_height="@dimen/dimen_0dp" android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 在JAVA中: videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { float videoRatio = mp.getVideoWidth() / (float) mp.getVideoHeight(); float screenRatio = videoView.getWidth() / (float) videoView.getHeight(); float scaleX = videoRatio / screenRatio; if (scaleX >= 1f) { videoView.setScaleX(scaleX); } else { videoView.setScaleY(1f / scaleX); } } }); 在 Kotlin 中: videoView.setOnPreparedListener { mediaPlayer -> val videoRatio = mediaPlayer.videoWidth / mediaPlayer.videoHeight.toFloat() val screenRatio = videoView.width / videoView.height.toFloat() val scaleX = videoRatio / screenRatio if (scaleX >= 1f) { videoView.scaleX = scaleX } else { videoView.scaleY = 1f / scaleX } } 这对我有用。希望这会对某人有所帮助。 解决方案是使用 TextureView 代替 VideoView(SurfaceView)。 TextureView 不会对内容进行任何操作以使其适合屏幕。 这是解决方案的代码示例: //store the SurfaceTexture to set surface for MediaPlayer mTextureView.setSurfaceTextureListener(new SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { FullScreenActivity.this.mSurface = surface; } .... Surface s = new Surface(mSurface); mPlayer = mp; mp.setSurface(s); scaleVideo(mp);//<-- this function scales video to run cropped .... private void scaleVideo(MediaPlayer mPlayer) { LayoutParams videoParams = (LayoutParams) mTextureView .getLayoutParams(); DisplayMetrics dm = new DisplayMetrics(); FullScreenActivity.this.getWindowManager().getDefaultDisplay() .getMetrics(dm); final int height = dm.heightPixels; final int width = dm.widthPixels; int videoHeight = mPlayer.getVideoHeight(); int videoWidth = mPlayer.getVideoWidth(); double hRatio = 1; hRatio = (height * 1.0 / videoHeight) / (width * 1.0 / videoWidth); videoParams.x = (int) (hRatio <= 1 ? 0 : Math.round((-(hRatio - 1) / 2) * width)); videoParams.y = (int) (hRatio >= 1 ? 0 : Math .round((((-1 / hRatio) + 1) / 2) * height)); videoParams.width = width - videoParams.x - videoParams.x; videoParams.height = height - videoParams.y - videoParams.y; Log.e(TAG, "x:" + videoParams.x + " y:" + videoParams.y); mTextureView.setScaleX(1.00001f);//<-- this line enables smoothing of the picture in TextureView. mTextureView.requestLayout(); mTextureView.invalidate(); } 要全屏裁剪中心,您仍然可以使用 VideoView。设置VideoView的宽度和高度以匹配RelativeLayout中的父级,并将其调整为大于屏幕并设置其位置。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:id="@+id/rootLayout" tools:context="com.example.Activity"> <RelativeLayout android:layout_width="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_height="match_parent"> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:layout_centerVertical="true" /> </RelativeLayout> </RelativeLayout> 然后在onCreate中: RelativeLayout rootView=(RelativeLayout) findViewById(R.id.rootLayout); Display display=getWindowManager().getDefaultDisplay(); Point size=new Point(); display.getSize(size); FrameLayout.LayoutParams rootViewParams = (FrameLayout.LayoutParams) rootView.getLayoutParams(); int videoWidth=864; int videoHeight=1280; if ((float)videoWidth/(float)videoHeight<(float)size.x/(float)size.y) { rootViewParams.width=size.x; rootViewParams.height=videoHeight*size.x/videoWidth; rootView.setX(0); rootView.setY((rootViewParams.height-size.y)/2*-1); } else { rootViewParams.width=videoWidth*size.y/videoHeight; rootViewParams.height=size.y; rootView.setX((rootViewParams.width-size.x)/2*-1); rootView.setY(0); } rootView.setLayoutParams(rootViewParams); final VideoView mVideoView=(VideoView)findViewById(R.id.video_view); mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash)); mVideoView.requestFocus(); mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mVideoView.start(); } }); 我只是将视频放入ConstraintLayout中并带有这样的参数。这有助于拉伸视频并实现android:scaleType="centerCrop"效果。 <VideoView android:id="@+id/video_view" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_gravity="center_horizontal" android:layout_width="0dp" android:layout_height="0dp" /> 我找到了一个解决方案:默认行为就像 fitCenter ,所以我计算视频比例(宽度/高度)和屏幕比例,然后将 VideoView 缩放到全屏。结果就像centerCrop一样。 添加@Nabin 的回答。如果您在暂停时遇到问题。 如果视频暂停导致视频视图高度发生变化并显示下方黑屏。用这个。 public class CustomVideoView extends VideoView { private int originalWidth = 0; private int originalHeight = 0; public CustomVideoView(Context context) { super(context); } public CustomVideoView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (originalWidth == 0 || originalHeight == 0) { originalWidth = MeasureSpec.getSize(widthMeasureSpec); originalHeight = MeasureSpec.getSize(heightMeasureSpec); } setMeasuredDimension(originalWidth, originalHeight); } } 就我而言,我知道 screenRatio > videoRatio 并且我需要制作圆角。我的解决方案只是设置高度wrap_content和宽度match_parent: <com.google.android.material.card.MaterialCardView android:id="@+id/mcv_video" android:layout_width="0dp" android:layout_height="130dp" app:cardCornerRadius="12dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.google.android.material.card.MaterialCardView>

回答 7 投票 0

Android Studio:视频比屏幕长 - 无法向下滚动?

我有一个应用程序,其中包含一个循环视频,其长度比屏幕的长度长,但是无法向下滚动并查看视频的底部部分(位于机器人外部...

回答 1 投票 0

如果我们有链接,我们可以从 Android 应用程序打开 Youtube 直播链接吗?如果是的话请解释一下如何?

如果我们有链接,我们可以从 Android 应用程序打开 YouTube 直播链接吗? 如果是,请解释一下如何? 或者 假设我有直播链接,我可以打开 Youtube 应用程序然后去...

回答 1 投票 0

jetback在使用VideoView时合成黑色背景

当我使用此代码在jetback compose中显示Imageview时,它可以正常工作 柱子( 修饰符 .wrapContentHeight() .fillMaxWidth(), ){ 图像( 修饰符 = Modifier.size(180.dp).align(

回答 1 投票 0

如何从VideoView中删除黑色视图?

我在VideoView中使用了教程视频。但视频在播放时不会填充 VideoView,并且 VideoView 中有黑色部分,因为它具有矩形视图。我无法解决它。我试图掩盖

回答 1 投票 0

android studio 中的横向模式和视频视图

我编写了一个程序,当我在手机上运行该程序时,就会开始播放视频。 现在,播放视频后,如果我将手机旋转到横向模式,我的应用程序也会旋转到横向模式...

回答 1 投票 0

对于本地存储的视频,ExoPlayer、MediaPlayer 和 VideoView 有什么区别?

我正在开发一个应用程序,可以播放本地存储的视频文件并使用它及其字幕。我知道 ExoPlayer 非常擅长处理流媒体视频,但就我而言,它们是存储的

回答 1 投票 0

VideoView Android 上的黑色背景

我有一个水平显示的视频视图。我显示的视频是垂直的。我的 ViewView 中的真实视频之外有一个空白区域。视频是这样显示的。 红色长方形...

回答 1 投票 0

视频观看暂停和恢复

我是android开发新手,正在编写游戏。我的游戏有在每个关卡开始之前播放的过场动画,这些过场动画是通过视频视图完成的。我的问题是,在申请时...

回答 7 投票 0

VideoView 循环在第一次循环后显示黑屏

我正在使用 VideoView 循环播放一个小视频,在模拟器上一切正常,但是当我将其部署到电视上时,第一个循环视频变黑后,但声音继续播放。这是代码: @结束...

回答 5 投票 0

我想同时使用视频视图和相机预览

我目前正在开发一个基本的应用程序,该应用程序使用Android集成摄像头和Camera X来录制视频,尽管我正在努力,但我想查看一个完全不相关的视频...

回答 1 投票 0

如何在exoplayer中始终显示进度条和时间

我试图始终在 exoplayer 中显示进度条。我的视图如下所示,我为播放器使用了自定义用户界面。 我试图始终在 exoplayer 中显示进度条。我的视图如下所示,我为播放器使用了自定义用户界面。 <com.google.android.exoplayer2.ui.PlayerView android:id="@+id/id_player_view" android:layout_width="match_parent" android:layout_height="200dp" android:background="@android:color/darker_gray" android:src="@android:drawable/ic_media_play" app:controller_layout_id="@layout/custom_exoplayer_controller" app:fastforward_increment="5000" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:resize_mode="fixed_height" app:rewind_increment="5000" app:use_controller="true"> 下面是我一直想展示的自定义组件。 <LinearLayout android:id="@+id/progressbar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignWithParentIfMissing="false" android:layout_alignParentBottom="true" android:layout_centerHorizontal="false" android:layout_centerVertical="false" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:gravity="center_vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <TextView android:id="@id/exo_position" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:paddingLeft="4dp" android:paddingRight="4dp" android:text="00:00" android:textColor="#FFBEBEBE" android:textSize="14sp" android:textStyle="bold" /> <com.google.android.exoplayer2.ui.DefaultTimeBar android:id="@id/exo_progress" android:layout_width="0dp" android:layout_height="26dp" android:layout_weight="1" app:played_color="#4589f2" /> <TextView android:id="@id/exo_duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:paddingLeft="4dp" android:paddingRight="4dp" android:text="2:00:00" android:textColor="#FFBEBEBE" android:textSize="14sp" android:textStyle="bold" /> </LinearLayout> 但默认情况下,如果屏幕5秒左右没有触摸,媒体控制器和进度条将被隐藏。 如何实现这一目标? 您必须将controllerShowTimeoutMs设置为-1以防止控制器自动消失。 来自方法文档: 设置播放控件超时。经过这段时间后,如果没有用户输入并且正在进行播放或缓冲,播放控件将自动隐藏。 @paramcontrollerShowTimeoutMs 超时(以毫秒为单位)。非正值将导致控制器无限期地保持可见。 show_timeout="0" 来自文档: show_timeout - 最后一次用户交互与 控件自动隐藏,以毫秒为单位。如果满足以下条件,则使用零 控件不应自动超时。 对应方法:setShowTimeoutMs(int) 默认:DEFAULT_SHOW_TIMEOUT_MS 来源:https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/ui/PlayerControlView.html

回答 2 投票 0

在VideoView中播放来自url的视频[Android]

我发现了类似的问题,但对我来说没有任何作用。 我尝试从这个网址播放视频: http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4 我的java代码: 视频视图 视频视图= (视频视图)

回答 7 投票 0

如何在Android上确定视频宽度和高度

我有一个视频文件,我想获取视频的宽度和高度。我不想玩它,只是为了获得尺寸。 我尝试过使用 MediaPlayer: MediaPlayer mp = new MediaPlayer(); mp.setDataSource(uriS...

回答 8 投票 0

访问内部存储和外部 SD 卡中的视频 - Android

我开发了一个简单的Android应用程序,它将从 path="storage/MicroSD/Videos" 并将它们显示在列表视图中,选择视频后,将播放所选视频。维...

回答 2 投票 0

按下后退按钮时关闭 VideoView 中的视频

我想在按下后退按钮并且视频正在播放时关闭VideoView。 这是我的代码: 公共类 MainActivity 扩展 AppCompatActivity { 公共整数[] mVideo = { ...

回答 3 投票 0

Android VideoView 恢复和seekTo

我正在我的应用程序中使用 VideoView 播放视频。单击按钮后,它会记录视频的当前位置,然后应用程序会打开浏览器并显示一些网址。按后退按钮...

回答 2 投票 0

如何在 android 的 videoview 中播放 .mp4 视频?

我正在开发视频播放器应用程序,我想在本机视频视图中播放 .mp4 视频。我无法使用 URL 播放视频。我收到错误消息“抱歉,无法播放此视频&

回答 6 投票 0

如何在 Android 中为 VideoView 字幕设置最大宽度

我目前有以下视频视图 我目前有以下视频观看 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myVideoView" android:layout_width="match_parent" android:layout_height="match_parent" > <VideoView android:id="@+id/my_video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" <RelativeLayout android:id="@+id/nav_bar" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:layout_alignParentBottom="true"> <ImageButton android:id="@+id/home" android:layout_width="49dp" android:layout_height="49dp" android:background="@drawable/exit_button_outline" android:src="@mipmap/btn_rdv_close" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:clickable="true" android:contentDescription="@string/home_btn_desc"/> <Button android:id="@+id/closed_captions" android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/closed_caption_button" android:textStyle="bold" android:textSize="20dp" android:textColor="@color/white" android:background="@drawable/round_button_off" android:layout_alignParentBottom="true" android:layout_toLeftOf="@+id/home" android:layout_marginRight="10dp" android:nextFocusRight="@id/home" android:nextFocusLeft="@id/video_view" android:nextFocusDown="@id/video_view" android:nextFocusUp="@id/video_view" android:contentDescription="@string/closed_caption_button_content_description" /> </RelativeLayout> 在代码中,我有以下内容: videoView.addSubtitleSource( getClosedCaptionsFromFile(closedCaptionsFileName), MediaFormat.createSubtitleFormat("text/vtt", deviceLang) ) videoView.setOnInfoListener { mp, what, extra -> var tracksInfo: Array<TrackInfo> = mp.trackInfo for ((index, track) in tracksInfo.withIndex()) { if (track.trackType == MEDIA_TRACK_TYPE_SUBTITLE) { if (closedCaptions) { mp.selectTrack(index) } else { mp.deselectTrack(index) } } } true } 我们使用 VTT 文件作为字幕。问题是如果 VTT 文件中的文本很长,那么文本会与“closed_captions”按钮重叠。 有没有办法设置隐藏式字幕的最大宽度,使其不与字幕按钮重叠?如果标题超过最大宽度,它们应该换行。 图片: Android 似乎没有内置的方法来限制宽度。 但是,由于我使用的是 VTT 文件,因此可以通过更改 size 参数 通过 VTT 语法来完成此操作 https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API

回答 1 投票 0

Android VideoView scaleX, scaleY 不工作

我想放大视频。这是目前的实施 我想放大视频。这是目前的实现 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" > <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:nextFocusRight="@id/home" android:nextFocusLeft="@id/closed_captions" android:nextFocusUp="@id/home" android:nextFocusDown="@id/closed_captions" /> </RelativeLayout> // reset zoom videoView.setScaleX(2f) videoView.setScaleY(2f) 这在一个设备上工作,但在另一个 android 设备上不工作。所以我不确定这里是否有问题..或者有其他选择吗? 请发布您的线路正在使用的设备 因为他们根本不应该。 VideoView 扩展了 SurfaceView,这是 View 的“特例”,无法设置动画。和方法改变,例如缩放、旋转、alpha 等。这是常见问题,THIS SO 中的更多信息以及搜索引擎中的更多资源 对于这样的动画(更改“外观属性”),您需要使用一些可播放视频的View,它扩展了TextureView

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.