How to make VideoView full screen
Asked Answered
P

4

4

I want to play a video in my Activity using a VideoView,and make it fullscreen and landscape mode (with hiding virtual button and status bar)when I click a Button.

But it can not hide the virtual button and it has a white line in bottom. my app

This my activity code:

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>
Pak answered 21/1, 2016 at 8:48 Comment(0)
M
3

Try this on your landscape mode.

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

Hide virtual buttons add this code:

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

Original size

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

full screen size

         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);
Mn answered 21/1, 2016 at 8:51 Comment(18)
Could you try again the xml code i made some changes.Mn
it can hide the white line but it can not hide that three virtual button @Kristo1990Pak
and in this way ,it always be full screen but it want it not full screen ,after i click the fullscreen button it change to fullscreenPak
check out i added some code that you need to add for the virtual buttonsMn
yeah the code that you add for the virural button works, and i konw how to do it now ,i can make the VideoView's padding was 0,0,0,0 after i click the button like mVideoView.setPadding(0,0,0,0) All in all thank you very much to help mePak
That's good. If this helped you please accept my answer. Happy coding :)Mn
I find a bug now is when i click other button it will make the padding as usual that has a white line in the bottom i think i should find another way to solve itPak
and in this way , it seen like i cannot exit the full screen but i want to exit full screenPak
I am pretty sure that the xml that i gave you works for fullscreen videoview. I cant help you if you are going to do this another way around it.Mn
if you want to exit the full screen then you need another button.!Mn
yeah ,I know the xml is work for fullscreen videoview ,but it will make it fullscreen before i click the fullscreen button,so I want setpadding(0,0,0,0) in the button listener,it can work at the first time but when my finger click the screen it has the white line as usual. Another way ,I want to know how to exit fullscreen .what code should i write?Pak
this is your xml code it will make it full screen before I click the button !this is your xml codePak
Ok use your xml and try to do this programmatically. I will edit my answer.Mn
but use it programmatically it will show the white line againPak
yeah I try it but it cannot fullscreen ,this is the picture you can have a see [](ww2.sinaimg.cn/large/7bdcf253gw1f07asslo8gj21ke0y6wtq.jpg)Pak
try this requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);Mn
I had try it at all , you can see it in my code uploaded before T ^ TPak
Well dont know how i can help you anymore. i have used this snippets before and they worked for me. maybe you need to change something in your xml in order to remove that white line. Good luck.Mn
P
2

At Last, I solve it with Kristo1990 and prashantwosti 's help My code is :

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

And at last I override the KEYCODE_BACK to exit fullscreen I hope it can help you all and thank you again.

Pak answered 22/1, 2016 at 3:43 Comment(0)
T
0

You can use this for 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();
        }
    });

Take a look: How to close a VideoView Activity (currently have to press back twice)

Thurifer answered 21/1, 2016 at 9:24 Comment(1)
this way is to finish the activity but I don't want to finish the activity , I write the code to exit fullscreen ,code like getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT);Pak
I
0

Here's some snippets from my working app:

in player activity:

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

your player_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/video_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:keepScreenOn="true"
android:orientation="vertical">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".PlayerActivity">

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

</FrameLayout>


</RelativeLayout>

values/styles.xml:

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
Italianism answered 21/1, 2016 at 9:44 Comment(1)
Thank you very much ,your code help me to find a new way to solve itPak

© 2022 - 2024 — McMap. All rights reserved.