Android VideoView Example With Youtube Playback


Sculpture by the Sea Bondi - Best of Perth

At Truiton I was developing another app in which the requirement was to view a YouTube video inside my app. So I thought of using Android VideoView class for it. Although if we have to view local videos that are present in our device or app, still the same Android VideoView class can can be used. In this Android VideoView Example With YouTube Playback tutorial I would show how to play a streaming You Tube video in an Android device, also I would show how to place a local video.

One of the main drawbacks for using Android VideoView for streaming YouTube videos, is that it streams a very low quality video with a resolution of 176 x 144 px, in 3gp format. In my app I didn’t a needed high quality YouTube streaming hence I used Android VideoView class. But if you wish to stream high quality YouTube videos have a look on my tutorial for YouTube API in my complete series on streaming videos:

  1. Android VideoView Example With YouTube Playback
  2. Android YouTube API

Android VideoView

Lets start off with an intro of Android VideoView. In short its an Android widget which eases the process of video playback on device. It gives the user an abstraction from JNI code, i.e. the MediaPlayer-JNI class. Yes the fact is Android VideoView is just a wrapper around the MediaPlayer class of android. Now since we are using a wrapper there are some limitations regarding the types of supported file types. In android only the file types listed below can be played with Android VideoView class:

  1. H.263
    • 3GPP (.3gp)
    • MPEG-4 (.mp4)
  2. H.264 AVC
    • 3GPP (.3gp)
    • MPEG-4 (.mp4)
    • MPEG-TS (.ts, AAC audio only, not seekable, Android 3.0+)
  3. MPEG-4 SP
    • 3GPP (.3gp)
  4. VP8
    • WebM (.webm)
    • Matroska (.mkv, Android 4.0+)

Moving on lets have a look at the layout I am going to use for this Android VideoView Example With YouTube Playback :

It is not as simple as it seems to play a YouTube video on android with VideoView as the format which YouTube uses for streaming is not supported by android directly. Therefore the workaround for this, is to use 3gpp format. YouTube doesn’t provide the URL for this format directly, therefore to get this URL we need to query the “http://gdata.youtube.com/feeds/api/videos/” URL. This URL returns a file from which we can get the URL for 3gpp format video. Now the interesting part is that there’s another limitation by YouTube’s policies that download of a video is not permitted. Hence the URL returned is of RTSP (Real Time Streaming Protocol) format. Luckily this protocol is supported by our Android VideoView class and the video is streamed instead of download. Lets have a look at the code for this process:

Here in the above code what I am trying to do is stream a YouTube video in the lowest quality with Android VideoView class. Another important thing that I am doing here is to resume video streaming from where it stopped when the screen is rotated.

In the above code the RTSP URL is parsed in a separate thread as in the new Android API, network access is not allowed on main thread, if we try to access network it would throw NetworkOnMainThreadException. This task to parse URL for Android VideoView is started from lines 62-63 by RTSPUrlTask which is a AsyncTask.

Now the tricky part accomplished here, is to resume the video playback in Android VideoView from where it stopped after the screen is rotated. To resume video playback in Android VideoView from where it stopped I am using Android’s callback method onSaveInstanceState. In this method I am storing a two parameters first the current location of video playback and the second video URL. Now if you have a close look at the class above, in lines 48 – 64 I am checking whether a savedInstanceState exists or not. If it exists its obvious that in current scenario screen is rotated. Hence the YouTube playback in Android VideoView starts from the location stored in savedInstanceState.

Can’t play this video

As of now if you run this code you would probably get this error. According to me this Can’t play this video notification comes in two cases :

  1. Unsupported file format
  2. Due to missing internet access permissions

In this Android VideoView tutorial I am showing how to stream a YouTube video live from internet, hence it is required to add the following permissions in the app’s manifest to make the above notification go away:

After adding the permissions if you run the Android VideoView Example, output would be something like:

Android VideoView

Now since Android VideoView is a widget used to play videos, it can also be used to play videos that are stored locally on the device. To play a video that is locally available on device is comparatively simpler. In the above VideoViewActivity class have a look at the commented out lines 43 – 46 :

Just add a video in res/raw folder of your project, un-comment these lines and give its reference in the above setVideoURI method and your task would be accomplished. Also comment out the lines 48-64, so that YouTube code is not compiled. Although Android VideoView cannot be used to play videos in assets folder, if you wish to do that you can use the Android MediaPlayer class. With this I would conclude Android VideoView Example With Youtube Playback tutorial, if you liked this tutorial please share it with your friends on Google+ and Facebook.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

7 thoughts on “Android VideoView Example With Youtube Playback

  • Nikos Miaoulis

    What happens with memory performance? in case of a youtube video or a local video , if you want to play lets say 10 videos?

  • Giri

    hai mohit gupt: i want to run the live tv channel from yotube using corresponding url…as u said above and also i added the youtube api demo (sdk)from https://www.youtube.com/yt/dev/ i did perfectly…but i cannot get the solution!! can u help me out..!! and can anyone send the programming code for playing the live tv channel from youtube..!! i have been working 15 days !! i strucked here!!!

    sorry for my poor english and thanks in advance!!

  • bagusflyer

    Still got”Can’t play this video”after I set the permission.

    Got something:

    E/ViewRootImpl﹕ sendUserActionEvent() mView == null
    E/MediaPlayer﹕ error (1, -2147483648)
    E/MediaPlayer﹕ Error (1,-2147483648)

    What could be wrong? Or Youtube doesn’t allow us to do so now? Thanks