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>
PPTing
  • 150
  • 1
  • 1
  • 12

4 Answers4

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);
Kristo
  • 1,339
  • 12
  • 22
  • Could you try again the xml code i made some changes. – Kristo Jan 21 '16 at 08:57
  • it can hide the white line but it can not hide that three virtual button @Kristo1990 – PPTing Jan 21 '16 at 08:57
  • 1
    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 fullscreen – PPTing Jan 21 '16 at 08:59
  • check out i added some code that you need to add for the virtual buttons – Kristo Jan 21 '16 at 09:00
  • 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 me – PPTing Jan 21 '16 at 09:05
  • That's good. If this helped you please accept my answer. Happy coding :) – Kristo Jan 21 '16 at 09:06
  • 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 it – PPTing Jan 21 '16 at 09:10
  • and in this way , it seen like i cannot exit the full screen but i want to exit full screen – PPTing Jan 21 '16 at 09:13
  • 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. – Kristo Jan 21 '16 at 09:14
  • if you want to exit the full screen then you need another button.! – Kristo Jan 21 '16 at 09:14
  • 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? – PPTing Jan 21 '16 at 09:21
  • this is your xml code it will make it full screen before I click the button ![this is your xml code](http://ww4.sinaimg.cn/large/7bdcf253gw1f0796furrpj20zm1iyk2l.jpg) – PPTing Jan 21 '16 at 09:38
  • Ok use your xml and try to do this programmatically. I will edit my answer. – Kristo Jan 21 '16 at 09:41
  • but use it programmatically it will show the white line again – PPTing Jan 21 '16 at 09:51
  • yeah I try it but it cannot fullscreen ,this is the picture you can have a see [](http://ww2.sinaimg.cn/large/7bdcf253gw1f07asslo8gj21ke0y6wtq.jpg) – PPTing Jan 21 '16 at 10:33
  • try this `requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);` – Kristo Jan 21 '16 at 10:39
  • I had try it at all , you can see it in my code uploaded before T ^ T – PPTing Jan 21 '16 at 10:46
  • 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. – Kristo Jan 21 '16 at 10:49
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.

Community
  • 1
  • 1
PPTing
  • 150
  • 1
  • 1
  • 12
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)

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • 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); – PPTing Jan 21 '16 at 09:49
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>
prashantwosti
  • 980
  • 2
  • 7
  • 17