Showing Fullscreen Video in Android

Showing fullscreen video is not difficult. But, you have to be mindful of several little requirements.

  • The player should be in fullscreen mode. That means, the activity should not have any action bar.
  • User should be able to change orientation without interrupting the video.
  • After the video stops, the player should go away.

Let’s see how we can take care of all of these needs.

First, create the layout. The activity should have a VideoView.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

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

</LinearLayout>

Next, we develop the activity class. It does a few things:

  • Gets the video URL from the “url” extra parameter.
  • Creates a MediaController and sets it up with the VideoView.
  • Sets itself up as the video completion listener. When the video ends, it shuts itself down.
  • Starts the video.

So, here we go:

public class VideoPlayerActivity extends Activity implements OnCompletionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.video_player_activity);

    VideoView v = (VideoView) findViewById(R.id.videoView);

    String url = null;
    if (getIntent().getExtras() != null) {
        url = getIntent().getExtras().getString("url");

        if (url != null) {
            v.setMediaController(new MediaController(this));
            v.setOnCompletionListener(this);
            v.setVideoURI(Uri.parse(url));
            v.start();
        }
    }

    if (url == null) {
        throw new IllegalArgumentException("Must set url extra paremeter in intent.");
    }
}

@Override
public void onCompletion(MediaPlayer v) {
    finish();
}

//Convenience method to show a video
public static void showRemoteVideo(Context ctx, String url) {
    Intent i = new Intent(ctx, VideoPlayerActivity.class);

    i.putExtra("url", url);
    ctx.startActivity(i);
}
}

Finally, we configure the activity in AndroidManifest.xml to hide action bar and ignore orientation change events.

<activity
    android:name="com.mobiarch.controller.VideoPlayerActivity"
    android:label="@string/title_activity_video_player" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
    android:configChanges="orientation|screenSize">
</activity>

One thought on “Showing Fullscreen Video in Android

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s