How To toggle FullScreen with VideoView Android

╄→гoц情女王★ 提交于 2019-12-04 05:25:28
awaistoor

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.

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>

I know it's too late but I just write this for other mates.

just add this line to your manifest activity to avoid reloading video

android:configChanges="orientation|screenSize|keyboardHidden"

hope it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!