How To toggle FullScreen with VideoView Android

13,056

Solution 1

What i have done is that i created a CustomVideoView which extends VideoView like this:

public class CustomVideoView extends VideoView {

private int measuredWidth = 0;
private int measuredHeight = 0;

public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // TODO Auto-generated constructor stub
}

public CustomVideoView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public CustomVideoView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public void setNewDimension(int width, int height) {
    this.measuredHeight = height;
    this.measuredWidth = width;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(measuredWidth, measuredHeight);
}
}//end class

Then on my player activity, i implemented this on fullscreen button's onclick:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
            android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
            android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);
    player.setNewDimension(metrics.widthPixels, metrics.heightPixels);
    player.getHolder().setFixedSize(metrics.heightPixels,
            metrics.widthPixels);
    player.setLayoutParams(params);

so with this my problem was solved. Hope it will solve others problem as well.

NOTE: I'm sorry for not giving anybody credits as i forgot the links i found on google which lead me to make a suitable solution for myself.

Solution 2

To make the video play in full screen you have to create separate activity like below . The Half screen activity is -

    public class HalfScreen extends Activity {
        Button fullbtn;
        VideoView videoView = null;
        final int REQUEST_CODE = 5000;
        // "https://www.youtube.com/embed/olsO7UJemhE";
        final String videoToPlay = "http://bffmedia.com/bigbunny.mp4";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.half);
            videoView = (VideoView) findViewById(R.id.VideoViewhalf);
            final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
            fullbtn = (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();
                }
            });

            fullbtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    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);

                        }
                    }
                }
            }
        }
    } 


//Now the full screen 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();
            }
        }

//To make the video full screen use below layout
full.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >
    </VideoView>

</RelativeLayout>
Share:
13,056
awaistoor
Author by

awaistoor

Student of IT, Android developer, can handle server side scripting through PHP, know a little about HTML/CSS, easily handle Photoshop and seek to learn new things as well.

Updated on June 14, 2022

Comments

  • awaistoor
    awaistoor almost 2 years

    I am using video view for live streaming, and I want to make this VideoView to toggle fullscreen and back to small screen like MXPlayer or YouTube players do without stopping the streams.

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ad_container"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/transparent" >
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent" >
    
            <TextView
                android:id="@+id/scroll_annouc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:background="@color/red"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal"
                android:lines="1"
                android:marqueeRepeatLimit="marquee_forever"
                android:padding="@dimen/ten_dp"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:text="@string/app_name"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@android:color/white"
                android:textStyle="bold" />
    
            <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/scroll_annouc"
                android:background="@android:color/transparent"
                android:orientation="vertical" >
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="270dp"
                    android:background="@android:color/black" >
    
                    <TextView
                        android:id="@+id/error_overlay"
                        android:layout_width="match_parent"
                        android:layout_height="270dp"
                        android:layout_alignParentTop="true"
                        android:background="@color/red_trans"
                        android:gravity="center"
                        android:text="@string/error_text"
                        android:textColor="@android:color/white"
                        android:textStyle="bold"
                        android:visibility="invisible" />
    
                    <VideoView
                        android:id="@+id/player"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentTop="true" />
    
                    <RelativeLayout
                        android:id="@+id/media_controller"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:background="@color/black_trans" >
    
                        <ImageView
                            android:id="@+id/btn_playpause"
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:layout_alignParentLeft="true"
                            android:layout_centerVertical="true"
                            android:background="@drawable/btn_pause"
                            android:contentDescription="@string/app_name"
                            android:padding="@dimen/five_dp" />
    
                        <Button
                            android:id="@+id/external_player"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerHorizontal="true"
                            android:layout_centerVertical="true"
                            android:background="@null"
                            android:text="Use External Player"
                            android:textColor="@android:color/white"
                            android:textSize="@dimen/content_size"
                            android:textStyle="bold" />
    
                        <ImageView
                            android:id="@+id/btn_fullscreen"
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:contentDescription="@string/app_name"
                            android:padding="@dimen/five_dp"
                            android:src="@drawable/enter_fullscreen" />
                    </RelativeLayout>
                </RelativeLayout>
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/two_dp"
                    android:background="@color/app_blue" />
    
                <TextView
                    android:id="@+id/loading_txt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:paddingBottom="@dimen/two_dp"
                    android:paddingLeft="@dimen/ten_dp"
                    android:paddingRight="@dimen/ten_dp"
                    android:paddingTop="@dimen/two_dp"
                    android:text="@string/app_name"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/content_size"
                    android:visibility="invisible" />
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/one_dp"
                    android:background="@color/light_grey" />
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/one_dp"
                    android:background="@color/light_grey" />
    
                <LinearLayout
                    android:id="@+id/channel_links_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="horizontal"
                    android:padding="@dimen/ten_dp" >
                </LinearLayout>
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/one_dp"
                    android:background="@color/light_grey" />
    
                <HorizontalScrollView
                    android:id="@+id/horizontal_view"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:fillViewport="true"
                    android:scrollbars="horizontal" >
    
                    <LinearLayout
                        android:id="@+id/viewsContainer"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal" >
                    </LinearLayout>
                </HorizontalScrollView>
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/one_dp"
                    android:background="@color/light_grey" />
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
    

    Everything is working fine like streaming videos and all but I am unable to make this VideoView to go to landscape and show the video in fullscreen. I have tried googling it but all the examples I have found are not working for me at the moment. So please tell me how it is possible to toggle fullscreen. Thanks.

  • awaistoor
    awaistoor about 9 years
    basically im streaming live channels in that video view and i have tried this solution but when i go from small screen to full, the streaming start reloading and that i dont want
  • Erum
    Erum over 8 years
    i m doing same but the issue is i have another linearlayout on top of my videoview and that layout is align parent bottom-----> true but once my orienatation is landscape .... then video should take match parent height and width and layout should on top of vide view should adjust on bottom but the layout is not working perfectly how to resolve this ?
  • awaistoor
    awaistoor over 8 years
    @Erum well you can create a wrapper layout for both videoview and your linearlayout and then change the params of that wrapper layout on orientation change
  • Erum
    Erum over 8 years
    i have done same .... but video view is not taking full screen height in case of landscape thats why issue arise @awaistoor
  • Erum
    Erum over 8 years
    stackoverflow.com/questions/32651695/… @awaistoor here is my issue
  • awaistoor
    awaistoor over 8 years
    @Erum check my answer on your question.
  • Erum
    Erum over 8 years
    yes @awaistoor this is the main issue in case of differnt layouts
  • Milad Yarmohammadi
    Milad Yarmohammadi over 7 years
    try to make views GONE in full screen to make layout full screen
  • Aovin Mahmud
    Aovin Mahmud over 7 years
    The method setNewDimension(int, int) is undefined for the type VideoView How can i solve this problem @awaistoor & @Erum
  • awaistoor
    awaistoor over 7 years
    @AovinMahmud dont use this function .. just set the layoutParams to the videoview.
  • Aovin Mahmud
    Aovin Mahmud over 7 years
    @awaistoor thx my problem is solved.I want to add with u in facebook. Can u please give ur Fb name? it's emergency.
  • awaistoor
    awaistoor over 7 years
    the same username i have here :)
  • hiddeneyes02
    hiddeneyes02 about 6 years
    Cannot resolve player, please help me, I really need this
  • awaistoor
    awaistoor about 6 years
    @hiddeneyes02 please tell me what's your issue?
  • hiddeneyes02
    hiddeneyes02 about 6 years
    @awaistoor I can't figure what the variable player stand for in this answer
  • awaistoor
    awaistoor about 6 years
    @hiddeneyes02 player variable was of CustomVideoView object