英文:
playing video with videoview
问题
我正在尝试从MP4 HTTP URL在VideoView中播放视频。
该活动仅配置为纵向模式,而VideoView的宽度和高度均设置为与父级匹配。
当视频播放时,即使手机是纵向放置且方向已锁定为纵向,视频仍然显示为横向。
当我尝试将VideoView的旋转设置为90度时,视频不会播放,屏幕仅显示为黑屏。
这是XML布局部分:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/vv1"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
这是我在onCreate事件中使用的代码:
VideoView vv = FindViewById<VideoView>(Resource.Id.vv1);
MediaController mediaController = new MediaController(this);
mediaController.SetAnchorView(vv);
vv.SetMediaController(mediaController);
vv.SetVideoURI(Android.Net.Uri.Parse(url));
vv.RequestFocus();
vv.Start();
有任何想法吗?
<details>
<summary>英文:</summary>
I'm trying to play a video in videoview from mp4 http url.
the activity is configured for portrait only and the videoview set to match parent in both width and height.
when the video is playing it is landscape even the phone is portrait and the orientation is locked on portrait.
when I try to set the videoview rotation to 90, the video is not played and the screen just stays black
this is the xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/vv1"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
this is the code that I use in onCreate event
VideoView vv = FindViewById<VideoView>(Resource.Id.vv1);
MediaController mediaController = new MediaController(this);
mediaController.SetAnchorView(vv);
vv.SetMediaController(mediaController);
vv.SetVideoURI(Android.Net.Uri.Parse(url));
vv.RequestFocus();
vv.Start();
any idea?
</details>
# 答案1
**得分**: 0
```xml
好的,我认为由于您的布局,
一个视图不能在四个不同的方向上对齐,您是否尝试过:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/vv1"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
或者只是用线性布局并匹配高度宽度。
如果您有疑问,请随意回来。
英文:
Well, I think that because of your Layout,
A View cannot be aligned in four different directions, have you tried:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/vv1"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Or just with a linear layout and match the height width.
Feel free to get back if you have queries
答案2
得分: 0
请在您的VideoView活动中尝试以下代码:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
布局应该如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
app:layout_constraintDimensionRatio="4:3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="@+id/vv"/>
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/progrss"
android:visibility="gone"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
英文:
Try this code in your videoView activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
and layout should be like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
app:layout_constraintDimensionRatio="4:3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="@+id/vv"/>
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/progrss"
android:visibility="gone"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
答案3
得分: 0
你可以尝试这段代码:
XML布局部分:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/vv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"/>
</RelativeLayout>
而要实现全屏视图,你可以在你的活动中添加以下代码:
private void hideSystemUi() {
playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
我认为这段代码应该可以正常工作。如果你有其他疑问,请在下方评论。
英文:
You can try this code
For XML Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/vv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
/>
</RelativeLayout>
And for full screen view, you can add this code on your activity
private void hideSystemUi() {
playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
I think the code work fine. If you have any other query comment below.
专注分享java语言的经验与见解,让所有开发者获益!
评论