Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Youtube video link in android app?

My question is not similar to other questions about how to open YouTube links. My question is how to open a YouTube link, and then when it opens in the app, it should close the YouTube app and again call my MainActivity which opens the YouTube app. It should however open the YouTube app from scrath and not just show the previous YouTube Activity which is running in the background.

MainAcitivy --> SecondActivity --> Youtube --> ThirdActivity --> Youtube

But I want the YouTube app to load again from scratch. But currently, I am getting the previously opened YouTube app which was in the background.

MainActivity

Intent intent = new Intent(MainActivity.this,ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

SecondActivity

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
sleep(10000);
Intent intent=new Intent(getApplicationContext(),ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

ThirdActivity

sleep(5000);
Toast.makeText(getApplicationContext(),"third",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

I want to load it each time again from scratch, but it's showing me the state where it was paused. If you don't understand my question, please be free to comment and I will try to elaborate more. Thanks in advance.

like image 496
vishal gupta Avatar asked Feb 03 '17 12:02

vishal gupta


3 Answers

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.

like image 186
jbrulmans Avatar answered Nov 08 '22 19:11

jbrulmans


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")
like image 35
Kevin ABRIOUX Avatar answered Nov 08 '22 17:11

Kevin ABRIOUX


The below code will open the youtube app in your phone

Intent intent = new Intent(Intent.ACTION_VIEW, "your youtube url here"); startActivity(intent);

if you want to load the url in your activity put a webview and run the url in the webview

like image 41
Dina Gar Avatar answered Nov 08 '22 19:11

Dina Gar