The following example code will open the Youtube link in the Youtube app if this one is available, otherwise it will open it in a browser:
public static void watchYoutubeVideo(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 {
startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
startActivity(webIntent);
}
}
EDIT: To answer your second requirement. Each time you call a new Intent with this code. It will open the application or browser for this video and it wont show the previous video loaded.
Kotlin version to open Youtube video
fun openYoutubeLink(youtubeID: String) {
val intentApp = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + youtubeID))
val intentBrowser = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + youtubeID))
try {
this.startActivity(intentApp)
} catch (ex: ActivityNotFoundException) {
this.startActivity(intentBrowser)
}
}
Just call
this.openYoutubeLink("Q-dNnMlaGNg")
Videos
Edit: This looks to be fixed for me as of the latest YouTube app update on 11/17. So far so good!
Using an iPhone 16 pro max on the latest iOS. Somehow the YouTube app has broken within the past few weeks. Now when I try to open any link on the phone outside of the app (like in messenger or in safari) it’ll open the app but it won’t go to the video. Instead it just goes to the Home Screen.
I’ve cleared my safari browser files, reinstalled the YouTube app, restarted the phone etc. I’m not sure what else to try but it sucks. Anyone have any suggestions that don’t involve factory resetting my phone?