当前位置: 首页 > 知识库问答 >
问题:

如何使VideoView全屏

段干华晖
2023-03-14

我想使用视频视图在我的活动中播放视频,并在我单击按钮时使其全屏和横向模式(隐藏虚拟按钮和状态栏)。

这是我的活动代码:

public class VideoActivity extends Activity {
private VideoView mVideoView;
private String mUrl;
private Button mFullScreen;
private static String TAG = VideoActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG,"onCreate");
    setContentView(R.layout.video);
    mVideoView = (VideoView) findViewById(R.id.video);
    mFullScreen = (Button) findViewById(R.id.fullscreen);
    File file = new File(Environment.getExternalStorageDirectory(),"video.mp4");
    mVideoView.setVideoPath(file.getPath());
    mVideoView.start();
    mFullScreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            enterFullScreen();
            mFullScreen.setVisibility(View.GONE);
        }
    });

}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG,"onDestroy");
}

private void enterFullScreen(){
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//常亮
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.MATCH_PARENT,
      RelativeLayout.LayoutParams.MATCH_PARENT
    );
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  }
}

video.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"/>
    <Button
        android:id="@+id/fullscreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:text="Fullscreen"/>
</RelativeLayout>

共有3个答案

屠嘉
2023-03-14

您可以将其用于BackButton

closeButton = (Button) findViewById(R.id.buttonClose);
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Log.d("VideoPreview", "onClick Close Button");
            VideoFullscreenActivity.super.onBackPressed();
        }
    });

看看:如何关闭VideoView活动(目前必须按两次)

仲孙思源
2023-03-14

最后,我用Kristo1990和prashantwosti的帮助解决了它。我的代码是:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT
        );
private void enterFullScreen(){
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏
        mVideoView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        mVideoView.setLayoutParams(layoutParams);
    }

private void exitFullScreen(){
          this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            mVideoView.setSystemUiVisibility(0);
            isFullScreen = false;
            mFullScreen.setVisibility(View.INVISIBLE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
                layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
                layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            }else {
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
            }
            mFullScreen.setVisibility(View.VISIBLE);

            mVideoView.setLayoutParams(layoutParams);
        }
邵和硕
2023-03-14

在你的横向模式下试试这个。

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

隐藏虚拟按钮添加此代码:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

原始尺寸

         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);

全屏尺寸

         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);
 类似资料:
  • 我正在使用VideoView在我的应用程序中下载和播放视频。服务器通过tls 1.2协议进行通信,在Android 4.4.2(KitKat)设备上默认禁用。因为我没有使用任何特殊的http客户端来下载流,而是使用android的VideoView组件,所以我找不到一种方法来设置VideoView的ssl套接字工厂。是否仍然可以为VideoView http连接启用Tls 1.2?

  • 我想用VideoView制作一个视频播放器。视频可以播放,但当我退出应用程序并重新进入时,视频不会从停止的地方恢复,而是从头开始。有人知道怎么修吗?谢谢! 这是我正在使用的代码:

  • 本文向大家介绍Android原生视频播放VideoView的使用,包括了Android原生视频播放VideoView的使用的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Android原生视频播放VideoView的具体代码,供大家参考,具体内容如下 布局文件activity_video.xml 对应的Avtivity:VideoActivity.java 以上就是本文的全部内容,希

  • 首先,我尝试在docker映像上托管一个。这是CURL输出,对我来说没有意义。 尝试:1 TCP_节点延迟设置 连接到本地主机(::1)端口13443(#0) ALPN,提供h2 ALPN,提供http/1.1 密码选择:全部:!出口:!出口40:!出口56:!阿努尔:!低:!RC4:@STRENGTH 已成功设置证书验证位置: CAfile:cfm。wdf。液公司总部:无。0(输出),TLS握手

  • 本文向大家介绍android多媒体类VideoView使用方法详解,包括了android多媒体类VideoView使用方法详解的使用技巧和注意事项,需要的朋友参考一下 一、概述 VideoView类将视频的显示和控制集于一身,我们可以借助它完成一个简易的视频播放器。VideoView和MediaPlayer也比较相似。 二、VideoView的使用方法 它主要有以下几种常用方法 步骤: 1.指定视