Skip to content

Instantly share code, notes, and snippets.

@coreform
Last active December 10, 2015 01:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreform/4357728 to your computer and use it in GitHub Desktop.
Save coreform/4357728 to your computer and use it in GitHub Desktop.
Android YouTube API with ActionBarSherlock (via SherlockFragmentActivity). Because the existence of youTubeBaseActivity would have you think the API is missing a YouTubeBaseFragmentActivity and youTubeBaseSupportFragmentActivity.
public class YouTubeExampleFragmentActivity extends SherlockFragmentActivity implements YouTubePlayer.OnInitializedListener {
private static final String mDeveloperKey = null; //make sure to change this to your project's API key
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_console);
YouTubePlayerSupportFragment youTubePlayerSupportFragment = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
youTubePlayerSupportFragment.initialize(mDeveloperKey, this);
}
/*
* YouTubeFailureRecoveryActivity.java example stuff
*
*/
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(mDeveloperKey, this);
}
}
public YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_youtube);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.loadVideo("nCgQDjiotG0");
}
}
@coreform
Copy link
Author

I've no idea why gist is messing with the formatting like it is...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment