【问题标题】:VideoView Full screen in android applicationandroid应用程序中的VideoView全屏
【发布时间】:2012-07-03 12:18:47
【问题描述】:

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

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

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

【问题讨论】:

标签: android


【解决方案1】:

为了使选择的答案起作用,我必须让我的 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>

此处给出:Android - How to stretch video to fill VideoView area 在屏幕尺寸之间切换就像更改所选答案中给出的布局参数一样简单。

【讨论】:

  • 是的。是真的。只有 RelativeLayout 才能使其全屏显示。感谢您的亮点。
  • 非常感谢,顺便说一句,因为现在是 2018 年,请改用 "match_parent",因为 "fill_parent" 现在已弃用。
  • 非常感谢,顺便说一句,既然是 2020 年,请改用“layout_alignParentStart”和“layout_alignParentEnd”,因为现在不推荐使用“layout_alignParentLeft”和“layout_alignParentRight”。
【解决方案2】:

这样设置全屏,

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

然后回到原来的大小,这样。

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

【讨论】:

  • 这对我不起作用...我的视频视图位于片段中,设置布局参数后没有任何反应(参数正确)
  • 它对我不起作用。我认为它没有拉伸视频视图。
  • 您好,这可行,但它总是以相同的方向(左)旋转视频。是否可以根据用户想要旋转的方式手动旋转它?
  • @Androidicus 当我的 videoview 在 relativelayout 中时,我将如何设置为 matchparent
  • @Erum 要将视频视图的宽度和高度设置为 match_parent,您可以调用: videoview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));这是你想要的吗?当然,要让 videoview 填满屏幕,父布局也必须填满屏幕。
【解决方案3】:

我是这样做的:

查看这些参考屏幕截图。

添加类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);
    }
}

如何绑定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>

希望这会对你有所帮助。

【讨论】:

  • 我有旋转屏幕的问题
【解决方案4】:

第一种方法

当您想全屏打开该 Activity 的视频时,您必须在 Manifest 中设置主题属性。设置这个值是

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

change theme programmatically here

第二种方法

点击按钮创建另一个 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>

【讨论】:

    【解决方案5】:

    在按钮上单击启动native video player,它将在full screen 中打开:

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

    【讨论】:

      【解决方案6】:

      以下代码有效。

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

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

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

      【讨论】:

      • 应该是答案。为我工作! ;-)
      【解决方案7】:

      您可以通过创建两个单独的活动来实现它。 假设第一个活动是 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();
          }
      }
      

      【讨论】:

        【解决方案8】:

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

        请注意,我使用RelativeLayout 代替我的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);
        }
        

        【讨论】:

          【解决方案9】:

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

          保持纵横比:

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

          !!!填写该字段:

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

          【讨论】:

            【解决方案10】:
            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);
            

            【讨论】:

              【解决方案11】:

              在这里尝试下面的代码。

              if (!isFullScreen())
                      {
                          Log.v("Full screen", "-----------is full screen------------");  
                          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
                          DisplayMetrics displaymetrics = new DisplayMetrics();
                            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                            int height = displaymetrics.heightPixels;
                            int width = displaymetrics.widthPixels;
                            android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
                            params.width = width;
                            params.height=height;
                            params.setMargins(0, 0, 0, 0);
              
                      }
                      else{
                          Log.v("Full screen", "-----------small screen------------");    
              
                          DisplayMetrics displaymetrics = new DisplayMetrics();
                            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                            int height = displaymetrics.heightPixels;
                            int width = displaymetrics.widthPixels;
                            android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
                            params.width = width;
                            params.height=height / 3;
                            params.setMargins(0, 0, 0, 0);
                      }
              

              【讨论】:

                【解决方案12】:

                此代码适用于全屏横向视频

                AndroidManifext.xml(设置方向)

                        <activity
                        android:name=".Video1"
                        android:screenOrientation="landscape" />
                

                Video1.java 代码:

                import android.net.Uri;
                import android.os.Bundle;
                import android.support.v7.app.AppCompatActivity;
                import android.view.WindowManager;
                import android.widget.MediaController;
                import android.widget.VideoView;
                
                public class Video1 extends AppCompatActivity {
                
                private VideoView videoView;
                private MediaController mediaController;
                
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_video1);
                
                    videoView = findViewById(R.id.videoView);
                    String fullScreen =  getIntent().getStringExtra("fullScreenInd");
                    if("y".equals(fullScreen)){
                        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
                        getSupportActionBar().hide();
                    }
                
                    Uri videoUri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.YOUR_VIDEO_NAME);
                
                    videoView.setVideoURI(videoUri);
                
                    mediaController = new FullScreenMediaController(this);
                    mediaController.setAnchorView(videoView);
                
                    videoView.setMediaController(mediaController);
                    videoView.start();
                    }
                }
                

                FullScreenMediaControler.java 代码:

                import android.app.Activity;
                import android.content.Context;
                import android.content.Intent;
                import android.view.Gravity;
                import android.view.View;
                import android.widget.FrameLayout;
                import android.widget.ImageButton;
                import android.widget.MediaController;
                
                public class FullScreenMediaController extends MediaController {
                
                private ImageButton fullScreen;
                private String isFullScreen;
                
                public FullScreenMediaController(Context context) {
                    super(context);
                }
                
                @Override
                public void setAnchorView(View view) {
                
                    super.setAnchorView(view);
                
                    //image button for full screen to be added to media controller
                    fullScreen = new ImageButton (super.getContext());
                
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    params.gravity = Gravity.RIGHT;
                    params.rightMargin = 80;
                    addView(fullScreen, params);
                
                    //fullscreen indicator from intent
                    isFullScreen =  ((Activity)getContext()).getIntent().
                            getStringExtra("fullScreenInd");
                
                    if("y".equals(isFullScreen)){
                        fullScreen.setImageResource(R.drawable.ic_fullscreen_exit);
                    }else{
                        fullScreen.setImageResource(R.drawable.ic_fullscreen);
                    }
                
                    //add listener to image button to handle full screen and exit full screen events
                    fullScreen.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                
                            Intent intent = new Intent(getContext(),Video1.class);
                
                            if("y".equals(isFullScreen)){
                                intent.putExtra("fullScreenInd", "");
                            }else{
                                intent.putExtra("fullScreenInd", "y");
                            }
                            ((Activity)getContext()).startActivity(intent);
                        }
                    });
                  }
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-12-20
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多