Android development: Play Youtube video in your app, “Can’t play this video” and troubleshooting

17 Mar

Playing Youtube video in your app is a frequently requested feature, and some time ago, it is trivial to do this using VideoView:


In the layout file, add below:

     android:layout_height=wrap_content />

And in the Activity’s onCreate() function, add below code:

final VideoView mVideoView = (VideoView) findViewById(;
MediaController mediaController = new MediaController(this);
mVideoView.setOnPreparedListener(new OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {

Well this is the implementation that works some time ago, but it might no longer works as expected, and you are likely to get a “Can’t play this video.” error, as shown above:

To solve this, a recommended approach is to use YouTube Android Player API, which seems to be very involved to use, but actually it is not!

To illustrate this, I will not go through the process sequentially, but rather dive in directly to show how to use it first, and how easy it is, assuming you have got everything installed (which scared most users and make it feel like very heavy and unwieldy):

Create new project with an empty Activity

1. In the layout XML, add below code:

            android:layout_height=wrap_content />

2. In the YourActivity.Java class, change the base class from Acitivity to YouTubeBaseActivity:

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener

3. In the onCreate() function in your activity, add below code:

protected void onCreate(Bundle savedInstanceState) {

    YouTubePlayerView youTubeView = (YouTubePlayerView)
    youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);

Here you need a developer key, which is saved in the static class DeveloperKey:

public class DeveloperKey {
       * Please replace this with a valid API key which is enabled for the
       * YouTube Data API v3 service. Go to the
       * <a href=”“>Google APIs Console</a> to
       * register a new developer key.
       public static final String DEVELOPER_KEY = “abcdedefghijklmnopqrstuvwxyz”;

4. Implement the interface YouTubePlayer.OnInitializedListener:

public void onInitializationSuccess(YouTubePlayer.Provider provider,
             YouTubePlayer player, boolean wasRestored) {
         if (!wasRestored) player.cueVideo(“XSMOykMIO3c”); // your video to play
public void onInitializationFailure(Provider arg0,
            YouTubeInitializationResult arg1){

5. In AndroidManfest.xml, add below code:

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

Compile and run it on your android device, yeah! That is it!


That said, once you get the developer API key, playing the video seems no harder than the VideoView! I will not cover how to get an API key here, which has been covered here in detail.

This is the simplest use of many features Google Youtube API, you might be interested in getting more from the examples. For instance, you may make your activity implement YouTubeFailureRecoveryActivity, which deals with recovering from errors that may occur during API initialization, but can be corrected through user action.

Yes, no fear, it is quite straightforward, abandon your VideoView, say goodbye to “Can’t play this video”! Happy coding.


Posted by on March 17, 2014 in Android, Programming


Tags: , , , , , , , ,

9 responses to “Android development: Play Youtube video in your app, “Can’t play this video” and troubleshooting

  1. Ahmed

    November 19, 2014 at 5:15 pm

    Thank you very much !!! I will try this out : )

  2. Hassan

    November 25, 2014 at 2:16 pm

    Nice tutorial great effort!

  3. Mitja MIKLAV

    January 8, 2015 at 5:41 pm

    Can i download this source code, because i don’t realy known how to do this, because some errors are shown in code? Do i need to download YoutubePlayer API from the link you paste it and what should i do with it?

  4. quana

    April 27, 2015 at 11:05 am

    How can i pass url of video?
    thank for helping

  5. xinyustudio

    April 28, 2015 at 9:11 am


  6. harish Mohanan

    April 30, 2015 at 12:25 am

    I have tried these. But there are certain features that are absent in YouTubeAPI which makes the videoView my prefered way of implementation. So.. The question is … Is there any way, I can use videoView to view YouTube videos. Even if the answer is a bit complex I will still prefer going for it, because I am hardly left with any other option.
    Please please help!

  7. xinyustudio

    April 30, 2015 at 3:16 pm

    Though videoView is your preferred implementation, however it is no longer supported. Pls correct me if this is not the case.

  8. Raza

    October 24, 2015 at 2:47 pm

    i am tired and now requesting you to please help me, i am doing this work in android studio and in my MainActivity it says ‘can’t resolve symbol YouTubeBaseActivity’ i knows it is shown when we don’t have reference libraries but i did provided libraries in my gradle file. any help please please please

  9. Ionut J. Bejan

    July 3, 2017 at 11:01 pm

    Well, this is working fine, but what happens when you HAVE TO use this YouTubePlayerView in a class that can not extend YoutubeBaseActivity ?


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: