Android VideoView With Examples

In this tutorial, we’ll learn about Android VideoView. We will create an android app in Android Studio and play video from URL, we will also do some customizations to it’s control panel.

Android VideoView

Android VideoView class is used to display Video files in them. Following are the acceptable formats:

  • 3gp
  • MP4 – Only H.263, H.264, H.264 codecs work.

VideoViews can play videos either from resource files, local data or url specified. Following are some of the methods used on VideoView:

  • setVideoURI() – This is used to set the url path. It needs to be parsed as a URI.
  • setVideoPath() – Used for local paths.
  • start()
  • stopPlayback()
  • seekTo(int milliSec)
  • pause()
  • resume()
  • isPlaying()
  • canPause()
  • canSeekForward()
  • canSeekBackward()
  • setOnCompletedListener()
  • addSubtitleSource()
  • setMediaController() : Used to add MediaControls on the Video. Pause/Play, Seek
  • getDuration()
  • setOnPreparedListener() : Gets invoked once the video starts.

Note: VideoView does not retain its full state when going into the background.

In the following section, we’ll be creating an application that runs Videos from urls one after the other.
We’ll see how the MediaController works with the VideoView.

Android VideoView Example


Do not forget to add the Internet Permission in your AndroidManifest.xml file.

Android VideoView Example Code


The code for the looks like this:

We’ve added two urls in an ArrayList. We set the anchorView() on the VideoView to keep the MediaControl inside the VideoView.

The output looks something like this:


Why is the MediaControl outside the VideoView?

Well, the MediaControl doesn’t know the dimensions of the VideoView until the video is started.

For having media control inside the video, we need to set the onPreparedListener on our VideoView and then set the anchor inside it. This way the MediaController is set inside the VideoView.

Our updated class looks like this now:

mp.release() is set to release the media player resources. It should be done to prevent memory leaks. Any videoView calls after this line would lead to a CRASH.

Now the output of the above application in action is given below:
android studio videoview tutorial

This brings an end to android video view tutorial. You can download the final Android VideoView project from the link below.

By admin

Leave a Reply

%d bloggers like this: