Handle initialization error YouTube API Service

Refer to the exercises of "Simple example using YouTube Android Player API" and "YouTubeThumbnailView example", onInitializationFailure() methods will be called with YouTubeInitializationResult passed if fail in initialization.

If the error is is user-recoverable (isUserRecoverableError() == true), we can proceed by calling getErrorDialog(Activity, int), and then show this dialog to enable users to recover from this error.

Handle initialization error YouTube API Service

Modify MainActivity.java from last exercise "YouTubeThumbnailView example of YouTube Android Player API" to handle onInitializationFailure() for YouTubePlayer and YouTubeThumbnailView.
package com.example.androidyoutubeapiplayer;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailLoader.ErrorReason;
import com.google.android.youtube.player.YouTubeThumbnailView;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener, YouTubeThumbnailView.OnInitializedListener{

public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
public static final String VIDEO_ID = "o7VVHhK9zf0";

private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
private YouTubeThumbnailView youTubeThumbnailView;
private YouTubeThumbnailLoader youTubeThumbnailLoader;

private static final int RQS_ErrorDialog = 1;

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

youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);

youTubeThumbnailView = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview);
youTubeThumbnailView.initialize(API_KEY, this);
youTubeThumbnailView.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
if(youTubePlayer != null){
youTubePlayer.play();
}
}});
}

@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {

if (result.isUserRecoverableError()) {
result.getErrorDialog(this, RQS_ErrorDialog).show();
} else {
Toast.makeText(this,
"YouTubePlayer.onInitializationFailure(): " + result.toString(),
Toast.LENGTH_LONG).show();
}
}

@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {

youTubePlayer = player;

Toast.makeText(getApplicationContext(),
"YouTubePlayer.onInitializationSuccess()",
Toast.LENGTH_LONG).show();

if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}

@Override
public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
YouTubeInitializationResult result) {

if (result.isUserRecoverableError()) {
result.getErrorDialog(this, RQS_ErrorDialog).show();
} else {
Toast.makeText(this,
"YouTubeThumbnailView.onInitializationFailure(): " + result.toString(),
Toast.LENGTH_LONG).show();
}
}

@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {

Toast.makeText(getApplicationContext(),
"YouTubeThumbnailView.onInitializationSuccess()",
Toast.LENGTH_LONG).show();

youTubeThumbnailLoader = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());

youTubeThumbnailLoader.setVideo(VIDEO_ID);

}

private final class ThumbnailLoadedListener implements
YouTubeThumbnailLoader.OnThumbnailLoadedListener {

@Override
public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
Toast.makeText(getApplicationContext(),
"ThumbnailLoadedListener.onThumbnailError()",
Toast.LENGTH_LONG).show();
}

@Override
public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
Toast.makeText(getApplicationContext(),
"ThumbnailLoadedListener.onThumbnailLoaded()",
Toast.LENGTH_LONG).show();

}

}

}


download filesDownload the files.

Download APK to try on your device.

Next: Handle player state changes and playback events, by implementing PlayerStateChangeListener and PlaybackEventListener.


The Tutorial: YouTube Android Player API step-by-step