使用VideoView播放视频。

huangapple 未分类评论50阅读模式
英文:

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&#39;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&lt;VideoView&gt;(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:

&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;VideoView
    android:id=&quot;@+id/vv1&quot;
    android:layout_alignParentTop=&quot;true&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    /&gt;

</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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:orientation=&quot;vertical&quot;&gt;
    &lt;RelativeLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;VideoView
            app:layout_constraintDimensionRatio=&quot;4:3&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:layout_alignParentBottom=&quot;true&quot;
            android:layout_alignParentEnd=&quot;true&quot;
            android:layout_alignParentLeft=&quot;true&quot;
            android:layout_alignParentRight=&quot;true&quot;
            android:layout_alignParentStart=&quot;true&quot;
            android:layout_alignParentTop=&quot;true&quot;
            android:id=&quot;@+id/vv&quot;/&gt;
        &lt;ProgressBar
            android:layout_width=&quot;50dp&quot;
            android:layout_height=&quot;50dp&quot;
            android:id=&quot;@+id/progrss&quot;
            android:visibility=&quot;gone&quot;
            android:layout_centerInParent=&quot;true&quot;/&gt;

    &lt;/RelativeLayout&gt;

&lt;/LinearLayout&gt;

答案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

&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;VideoView
        android:id=&quot;@+id/vv1&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_centerVertical=&quot;true&quot;
        /&gt;
&lt;/RelativeLayout&gt;

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.

huangapple
  • 本文由 发表于 2020年5月4日 22:39:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/61594822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定