Android YouTube app Play Video Intent

前端 未结 18 1120
太阳男子
太阳男子 2020-11-22 11:17

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

相关标签:
18条回答
  • 2020-11-22 11:38

    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 ) );
    
    0 讨论(0)
  • 2020-11-22 11:40

    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

    0 讨论(0)
  • 2020-11-22 11:43

    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); 
    
    0 讨论(0)
  • 2020-11-22 11:44

    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);
    }
    
    0 讨论(0)
  • 2020-11-22 11:46

    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.

    0 讨论(0)
  • 2020-11-22 11:47

    Youtube now has a player api, You should try that.

    https://developers.google.com/youtube/android/player/

    0 讨论(0)
提交回复
热议问题