Skip to content

Video Playback and Recording

Nathan Esquenazi edited this page Feb 21, 2016 · 22 revisions

Overview

In Android apps, there are often cases where we need to play video files. Checking the Supported Media Formats we can see that several video formats (H.264, MPEG-4) are playable by default.

In this guide we will take a look at how to play video content using the VideoView and capture video with the MediaRecorder.

Playing Local Video

Playing local video in a supported format can be done using the VideoView. First, setup the VideoView in your layout:

<VideoView
    android:id="@+id/video_view"
    android:layout_width="320px"
    android:layout_height="240px" />

Next, we can store local files such as small_video.mp4 in res/raw/small_video.mp4 and than play the video in the view with:

VideoView mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.small_video));
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();

See this tutorial for playing a video full-screen with a VideoView. See this other edumobile tutorial for a more detailed look at using VideoView.

Playing Streaming Video

To play back remote video in a supported format, we can still use the VideoView. First, setup the correct permissions in the Android Manifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

Now, we can play remote video with:

final VideoView mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setVideoPath("http://techslides.com/demos/sample-videos/small.mp4");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new OnPreparedListener() {
    // Close the progress bar and play the video
    public void onPrepared(MediaPlayer mp) {
        mVideoView.start();
    }
});

You can see a more complete example of remote streaming with this androidbegin tutorial.

VideoView Controls

We can remove the VideoView media controls with:

mVideoView.setMediaController(null)

We can hide or show the media UI controls at runtime with:

// Get instance to media controller
MediaController controller = new MediaController(this);
videoHolder.setMediaController(controller);
// Hide the controller
controller.setVisibility(View.GONE);
// Show the controller 
controller.setVisibility(View.VISIBLE);

VideoView Limitations and Improved Libraries

VideoView should not be embedded in a ListView or any scrolling view due to a known bug with Android. Instead of using a VideoView which extends SurfaceView, in order to enable scrolling we need to use a TextureView instead.

The easiest workaround is to use a library such as Android-ScalableVideoView, fenster or VideoPlayerManager. You can read about how fenster was developed as well.

Capturing Video

Capturing video can be done using intents to capture video using the camera. First, let's setup the necessary permissions in AndroidManifest.xml (Note: The permissions model has changed starting in Marshmallow. If your targetSdkVersion >= 23 and you are running on a Marshmallow (or later) device, you may need to enable runtime permissions. You should also read more about the runtime permissions changes):

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Next, we can trigger recording video by starting an intent triggering a video capture action:

private static final int VIDEO_CAPTURE = 101;
Uri videoUri;
public void startRecordingVideo() {
    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        File mediaFile = new File(
           Environment.getExternalStorageDirectory().getAbsolutePath() + "/myvideo.mp4");
        videoUri = Uri.fromFile(mediaFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
        startActivityForResult(intent, VIDEO_CAPTURE);
    } else {
        Toast.makeText(this, "No camera on device", Toast.LENGTH_LONG).show();
    }
}

and then we need to manage the onActivityResult for when the video has been captured:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VIDEO_CAPTURE) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "Video has been saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
            playbackRecordedVideo();
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Video recording cancelled.",  Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Failed to record video",  Toast.LENGTH_LONG).show();
        }
    }
}

public void playbackRecordedVideo() {
    VideoView mVideoView = (VideoView) findViewById(R.id.video_view);
    mVideoView.setVideoURI(videoUri);
    mVideoView.setMediaController(new MediaController(this));
    mVideoView.requestFocus();
    mVideoView.start();
}

For a more detailed look, check out the techtopia tutorial on video recording.

Streaming from 3gp Youtube Source

There are a few ways of video playback on the android device. Most of which include downloading the content to the device for playback. If you want to stream a video from a network hosted source. the youtube api gives you the ability to do so using their provided 3gp stream. The provided YouTube Android Player API allows you to do so with very little code. Check out this truiton tutorial to learn more about playing video with YouTube SDK.

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally