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


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

image

In the layout file, add below:

<VideoView
     android:id=@+id/videoView1
     android:layout_width=match_parent
     android:layout_height=wrap_content />

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

final VideoView mVideoView = (VideoView) findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath(“https://http://www.youtube.com/watch?v=XSMOykMIO3c“);
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {
    mVideoView.start();
}});

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:

<com.google.android.youtube.player.YouTubePlayerView
            android:id=@+id/videoView1
            android:layout_width=match_parent
            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:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    YouTubePlayerView youTubeView = (YouTubePlayerView)
                     findViewById(R.id.videoView1);
    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=”https://code.google.com/apis/console/“>Google APIs Console</a> to
       * register a new developer key.
       */
       public static final String DEVELOPER_KEY = “abcdedefghijklmnopqrstuvwxyz”;
}

4. Implement the interface YouTubePlayer.OnInitializedListener:

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
             YouTubePlayer player, boolean wasRestored) {
         if (!wasRestored) player.cueVideo(“XSMOykMIO3c”); // your video to play
}
@Override
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!

image

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.

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

  1. Mitja MIKLAV

    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?

  2. 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!

  3. xinyustudio

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

  4. Raza

    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

  5. Pingback: How to play Youtube videos in Android Video View? - Code Solution

Leave a comment