Android应用程序中的VideoView全屏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11310764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:52:29  来源:igfitidea点击:

VideoView Full screen in android application

android

提问by Cool Compiler

i have a videoview in my application. the code is like this.

我的应用程序中有一个视频视图。代码是这样的。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/opsbuds"
    android:orientation="vertical">

    <TextView
        android:id="@+id/adtxt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"></TextView>

    <VideoView
        android:id="@+id/videoView11"
        android:layout_width="300dip"
        android:layout_height="250dip"
        android:layout_marginLeft="30dip"></VideoView>

    <LinearLayout
        android:id="@+id/llv11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"></LinearLayout>

    <Button
        android:id="@+id/button1211"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/button"
        android:text=" Continue "
        android:textColor="#800080"
        android:textSize="20dp"
        android:textStyle="bold"></Button>
</LinearLayout>
</ScrollView>

the video view width and hieght is mentioned in xml file. What i want is , once i press a button the videoview should come on full screen and once i press back button the videoview should go back to its mentioned size. please help?

xml 文件中提到了视频视图宽度和高度。我想要的是,一旦我按下一个按钮,视频视图应该全屏显示,一旦我按下后退按钮,视频视图应该回到它提到的大小。请帮忙?

回答by Srikanth

I had to make my VideoView sit in a RelativeLayout in order to make the chosen answer work.

我必须让我的 VideoView 坐在一个 RelativeLayout 中才能使所选的答案起作用。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <VideoView android:id="@+id/videoViewRelative"
         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">
    </VideoView>

</RelativeLayout>

As given here: Android - How to stretch video to fill VideoView areaToggling between screen sizes would be as simple as changing the layout parameters as given in the chosen answer.

如此处所示:Android - 如何拉伸视频以填充 VideoView 区域在屏幕尺寸之间切换就像更改所选答案中给出的布局参数一样简单。

回答by Aqif Hamid

Set full screen this way,

这样设置全屏,

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.leftMargin = 0;
videoView.setLayoutParams(params);

And back to original size, this way.

并回到原来的大小,这样。

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
params.width = (int)(300*metrics.density);
params.height = (int)(250*metrics.density);
params.leftMargin = 30;
videoView.setLayoutParams(params);

回答by Hiren Patel

I have done this way:

我已经这样做了:

Check these reference screen shots.

检查这些参考屏幕截图。

enter image description here

在此处输入图片说明

Add class FullScreenVideoView.java:

添加类FullScreenVideoView.java

import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;

public class FullScreenVideoView extends VideoView {
    public FullScreenVideoView(Context context) {
        super(context);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
}

How to bindwith xml:

如何与xml绑定

<FrameLayout
   android:id="@+id/secondMedia"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

     <com.my.package.customview.FullScreenVideoView
           android:layout_width="match_parent"
           android:layout_height="match_parent" 
           android:id="@+id/fullScreenVideoView"/>

</FrameLayout>

Hope this will help you.

希望这会帮助你。

回答by Jawad Zeb

First Method

第一种方法

when you want to open a video in full screen for that Activity you have to set the theme attribute in the Manifest. set this value that is

当您想为该活动全屏打开视频时,您必须在清单中设置主题属性。设置这个值是

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

change theme programmatically here

在此处以编程方式更改主题

Second Method

第二种方法

create another fullscreen.xml like below and setContentView(R.layout.fullscreen)on click of the button

创建另一个 fullscreen.xml 如下所示,setContentView(R.layout.fullscreen)然后单击按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <VideoView android:id="@+id/myvideoview"
        android:layout_width="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_height="fill_parent">
    </VideoView>

</RelativeLayout>

回答by Vineet Shukla

On Button click start the native video playerwhich will open in full screen:

在按钮上单击开始native video player,它将在full screen以下位置打开:

Intent intent = new Intent(Intent.ACTION_VIEW );
intent.setDataAndType(Uri.parse(path), "video/*");
startActivity(intent);

回答by Ayaz Alifov

I achieved it by switching to landscape orientation and setting layout params to MATCH_PARENT. Just before switching to fullscreen mode, we need to store current orientation mode and VideoViewparams indefaultScreenOrientationModeand defaultVideoViewParamsvariables correspondingly. So that we can use them when we exit from video fullscreen mode. Thus, when you want to open video in fullscreen mode, use makeVideoFullScreen()method, to exit - exitVideoFullScreen().

我通过切换到横向并将布局参数设置为MATCH_PARENT. 在切换到全屏模式之前,我们需要相应地存储当前的方向模式和VideoView参数defaultScreenOrientationModedefaultVideoViewParams变量。这样我们就可以在退出视频全屏模式时使用它们。因此,当您想以全屏模式打开视频时,请使用makeVideoFullScreen()方法, 退出 - exitVideoFullScreen()

Please, note I used RelativeLayoutfor my VideoView, in your case it can be another layout type.

请注意,我用于RelativeLayoutmy VideoView,在您的情况下,它可以是另一种布局类型。

private RelativeLayout.LayoutParams defaultVideoViewParams;
private int defaultScreenOrientationMode;

// play video in fullscreen mode
private void makeVideoFullScreen() {

    defaultScreenOrientationMode = getResources().getConfiguration().orientation;
    defaultVideoViewParams = (LayoutParams) videoView.getLayoutParams();

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    videoView.postDelayed(new Runnable() {

        @Override
        public void run() {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);

            videoView.setLayoutParams(params);
            videoView.layout(10, 10, 10, 10);
        }
    }, 700);
}


// close fullscreen mode
private void exitVideoFullScreen() {
    setRequestedOrientation(defaultScreenOrientationMode);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

    videoView.postDelayed(new Runnable() {

        @Override
        public void run() {
            videoView.setLayoutParams(defaultVideoViewParams);
            videoView.layout(10, 10, 10, 10);
        }
    }, 700);
}

回答by Chandra Sharma

you can achieve it by creating two separate activity. Suppose first activity is halfScreen activity. In this activity your video view having small size. On button click of full screen video start another activity 'fullScreen activity'. In second activity the video view should be match parent to the parent layout.you can also start video in full screen from where it is paused in half screen.In my code i have implemented that.Also you can resume the video in half screen on the back press of fullscreen activity. This is work for me.Hope it will work for you also.

您可以通过创建两个单独的活动来实现它。假设第一个活动是 halfScreen 活动。在此活动中,您的视频视图尺寸较小。单击全屏视频按钮启动另一个活动“全屏活动”。在第二个活动中,视频视图应该与父布局匹配。你也可以从半屏暂停的地方开始全屏视频。在我的代码中,我已经实现了。此外,你可以在半屏上恢复视频全屏活动的背面按下。这对我有用。希望它也对你有用。

Here is the code
half.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#aa99cc"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/VideoViewhalf"
        android:layout_width="match_parent"
        android:layout_height="300dp" >
    </VideoView>

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnfullScreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fullscreen" />

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



    </LinearLayout>

</LinearLayout>

HalfScreen activity
public class HalfScreen extends Activity {
    Button btn;
    VideoView videoView = null;
    final int REQUEST_CODE = 5000;
    final String videoToPlay = "http://bffmedia.com/bigbunny.mp4";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.half);
        videoView = (VideoView) findViewById(R.id.VideoViewhalf);
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        btn = (Button) findViewById(R.id.btnfullScreen);

        Uri video = Uri.parse(videoToPlay);
        videoView.setVideoURI(video);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                progressBar.setVisibility(View.GONE);
                videoView.requestFocus();
                videoView.start();
            }
        });

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent videointent = new Intent(HalfScreen.this,
                        FullScreen.class);
                videointent.putExtra("currenttime",
                        videoView.getCurrentPosition());
                videointent.putExtra("Url", videoToPlay);
                startActivityForResult(videointent, REQUEST_CODE);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            if (data.hasExtra("currenttime")) {
                int result = data.getExtras().getInt("currenttime", 0);
                if (result > 0) {
                    if (null != videoView) {
                        videoView.start();
                        videoView.seekTo(result);
                        ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
                        progressBar.setVisibility(View.VISIBLE);

                    }
                }
            }
        }
    }
}

full.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff99cc"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/VideoViewfull"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </VideoView>

</LinearLayout>

FullScreen Activity

public class FullScreen extends Activity {
    Button btn;
    VideoView videoView = null;
    int currenttime = 0;
    String Url = "";
    private static ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        if (null != extras) {
            currenttime = extras.getInt("currenttime", 0);
            Url = extras.getString("Url");
        }
        setContentView(R.layout.full);
        progressDialog = ProgressDialog.show(this, "", "Loading...", true);
        videoView = (VideoView) findViewById(R.id.VideoViewfull);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri video = Uri.parse(Url);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);

        videoView.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer arg0) {
                progressDialog.dismiss();
                videoView.start();
                videoView.seekTo(currenttime);
            }
        });
    }

    @Override
    public void finish() {
        Intent data = new Intent();
        data.putExtra("currenttime", videoView.getCurrentPosition());
        setResult(RESULT_OK, data);
        super.finish();
    }
}

回答by K. Rea

whether you want to keep the aspect ratio of a video or stretch it to fill its parent area, using the right layout manager can get the job done.

无论您是要保持视频的纵横比还是拉伸以填充其父区域,使用正确的布局管理器都可以完成工作。

Keep the aspect ratio :

保持纵横比:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

<VideoView
  android:id="@+id/videoView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_gravity="center"/>

</LinearLayout>

!!! To fill in the field:

!!!要填写该字段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <VideoView android:id="@+id/videoViewRelative"
         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">
    </VideoView>

</RelativeLayout>

回答by App Sbinnovations

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) mVideoView.getLayoutParams();
params.width = (int) metrics.widthPixels;
params.height = (int) metrics.heightPixels;
mVideoView.setLayoutParams(params);

playVideo();
aspectRatio = VideoInfo.AR_4_3_FIT_PARENT;
mVideoView.getPlayer().aspectRatio(aspectRatio);

回答by saketha yellanki

The below code worked.

下面的代码有效。

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

Add this code before calling videoView.start(). With this the video activity runs in full screen mode in most of the cases. But if the title bar is still displayed then change your theme in your manifest to this.

在调用 videoView.start() 之前添加此代码。在大多数情况下,视频活动以全屏模式运行。但是,如果标题栏仍然显示,则将清单中的主题更改为此。

    android:theme="@style/Theme.AppCompat.NoActionBar">