Skip to content

Instantly share code, notes, and snippets.

@elizacamber
Created August 8, 2017 08:58
Show Gist options
  • Save elizacamber/81ef9fef65152f06dc73f628438cdbff to your computer and use it in GitHub Desktop.
Save elizacamber/81ef9fef65152f06dc73f628438cdbff to your computer and use it in GitHub Desktop.
Intent for playing a playlist in the youtube app
public static Intent getYoutubePlayPlaylistIntent(Context context, String googleId) {
String url = String.valueOf("https://www.youtube.com/playlist?list=");
String mGoogleId = String.valueOf(googleId);
if (mGoogleId.length() != 0) {
url = url.concat(mGoogleId);
}
Uri uri = Uri.parse(url).buildUpon().appendQueryParameter("playnext", "1").build();
Intent intent = new Intent("android.intent.action.VIEW", uri).setPackage("com.google.android.youtube");
PackageManager packageManager = context.getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
String clientLibraryVersion = (new StringBuilder(+35)).append(1).append(".2.2").toString();
String appVersion = getAppVersion(context);
intent.putExtra("app_package", context.getPackageName())
.putExtra("app_version", appVersion)
.putExtra("client_library_version", clientLibraryVersion);
return intent;
} else
return new Intent(Intent.ACTION_VIEW, uri);
}
private static String getAppVersion(Context context) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException var1) {
throw new IllegalStateException("Cannot retrieve calling Context's PackageInfo", var1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment