desperateStudent
desperateStudent

Reputation: 81

Make Video play in landscape and full screen

Working on an app that have VideoView. But the video is in portrait and not full screen. I want to play it in landscape and if possible full screen. This is my code in xml

    <VideoView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/FBG"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true"/>

and this code in the class

public class lectFbg extends AppCompatActivity {
VideoView video;
ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lect_fbg);

    video = (VideoView) findViewById(R.id.FBG);
    String uriPath = "android.resource://" + getPackageName() + "/" + R.raw.tugs4;
    Uri uri = Uri.parse(uriPath);
    /*video.setVideoURI(uri);*/
    pDialog = new ProgressDialog(this);
    // Set progressbar title
    pDialog.setTitle("FBG");
    // Set progressbar message
    pDialog.setMessage("Buffering...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    // Show progressbar


    try {
        // Start the MediaController
        MediaController mediacontroller = new MediaController(
                this);
        mediacontroller.setAnchorView(video);
        // Get the URL from String VideoURL
        video.setMediaController(mediacontroller);
        video.setVideoURI(uri);

    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

    video.requestFocus();
    video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setVolume(50f,50f);
            pDialog.dismiss();
            video.start();
        }

    });


    video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {


        public void onCompletion(MediaPlayer mp) {
            Intent in = new Intent (getApplicationContext(),lectureFBG.class);
            startActivity(in);
         }
      });
    }
 }

Upvotes: 0

Views: 11604

Answers (2)

Mikhael Lauda
Mikhael Lauda

Reputation: 71

Full Screen Landscape Video

AndroidManifext.xml (Setting the orientation)

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

Video1.xml Code :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Video1">

<VideoView
    android:id="@+id/videoView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">
</VideoView>

Video1.java Code :

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

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

Upvotes: 1

Beppi&#39;s
Beppi&#39;s

Reputation: 2129

To make it Landscape, you can do it programmatically if you need to change the orientation dynamically:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Or static, by adding it inside the Manifest.xml file:

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

For the full screen you can define a theme inside the Manifest.xml:

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

or use the immersive full-screen mode as here described:

https://developer.android.com/training/system-ui/immersive.html

that is described much better than what I could ever write here.

Upvotes: 4

Related Questions