I have created a app where you can download YouTube videos for android. Now, I want it so that if you play a video in the YouTube native app you can download it too. To do t
This will work if youtube app installed. If not, a chooser will show up to select other application:
Uri uri = Uri.parse( "https://www.youtube.com/watch?v=bESGLojNYSo" );
uri = Uri.parse( "vnd.youtube:" + uri.getQueryParameter( "v" ) );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
And how about this:
public static void watchYoutubeVideo(Context context, String id){
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + id));
try {
context.startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
context.startActivity(webIntent);
}
}
Note: Beware when you are using this method, YouTube may suspend your channel due to spam, this happened two times with me
Use my code .. I am able to play youtube video using this code ... you just need to provide youtube video id in the "videoId" variable ....
String videoId = "Fee5vbFLYM4";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId));
intent.putExtra("VIDEO_ID", videoId);
startActivity(intent);
The safest way to run videos on a different app is by first trying to resolve the package, in other words, check that the app is installed on the device. So if you want to run a video on youtube you'd do something like this:
public void playVideo(String key){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + key));
// Check if the youtube app exists on the device
if (intent.resolveActivity(getPackageManager()) == null) {
// If the youtube app doesn't exist, then use the browser
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + key));
}
startActivity(intent);
}
You can also just grab the WebViewClient
wvClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("youtube:")) {
String youtubeUrl = "http://www.youtube.com/watch?v="
+ url.Replace("youtube:", "");
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(youtubeUrl)));
}
return false;
}
Worked fine in my app.
Youtube now has a player api, You should try that.
https://developers.google.com/youtube/android/player/