Android YouTube app Play Video Intent

前端 未结 18 1136
太阳男子
太阳男子 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:48

    Here's how I solved this issue:

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + video_id));
    startActivity(intent);
    

    Now that I've done some more research, it looks like I only needed 'vnd.youtube:VIDEO_ID' instead of two slashes after the colon (':' vs. '://'):

    http://it-ride.blogspot.com/2010/04/android-youtube-intent.html

    I tried most of the suggestions here and they didn't really work very well with all of the supposed "direct" methods raising exceptions. I would assume that, with my method, if the YouTube app is NOT installed, the OS has a default fallback position of something other than crashing the app. The app is theoretically only going on devices with the YouTube app on them anyway, so this should be a non-issue.

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

    This will work on a device but not the emulator per Lemmy's answer.

    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
    
    0 讨论(0)
  • 2020-11-22 11:49

    EDIT: The below implementation proved to have problems on at least some HTC devices (they crashed). For that reason I don't use setclassname and stick with the action chooser menu. I strongly discourage using my old implementation.

    Following is the old implementation:

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(youtubelink));
    if(Utility.isAppInstalled("com.google.android.youtube", getActivity())) {
        intent.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity");
    }
    startActivity(intent);
    

    Where Utility is my own personal utility class with following methode:

    public static boolean isAppInstalled(String uri, Context context) {
        PackageManager pm = context.getPackageManager();
        boolean installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            installed = false;
        }
        return installed;
    }
    

    First I check if youtube is installed, if it is installed, I tell android which package I prefer to open my intent.

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

    You can use the Youtube Android player API to play the video if Youtube app is installed, otherwise just prompt the user to choose from the available web browsers.

    if(YouTubeIntents.canResolvePlayVideoIntent(mContext)){
                        mContext.startActivity(YouTubeIntents.createPlayVideoIntent(mContext, mVideoId));
    }else {
        Intent webIntent = new Intent(Intent.ACTION_VIEW, 
               Uri.parse("http://www.youtube.com/watch?v=" + mVideoId));
    
        mContext.startActivity(webIntent);
    }
    
    0 讨论(0)
  • 2020-11-22 11:52
    /**
     * Intent to open a YouTube Video
     * 
     * @param pm
     *            The {@link PackageManager}.
     * @param url
     *            The URL or YouTube video ID.
     * @return the intent to open the YouTube app or Web Browser to play the video
     */
    public static Intent newYouTubeIntent(PackageManager pm, String url) {
        Intent intent;
        if (url.length() == 11) {
            // youtube video id
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + url));
        } else {
            // url to video
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        }
        try {
            if (pm.getPackageInfo("com.google.android.youtube", 0) != null) {
                intent.setPackage("com.google.android.youtube");
            }
        } catch (NameNotFoundException e) {
        }
        return intent;
    }
    
    0 讨论(0)
  • 2020-11-22 11:54
    Intent videoClient = new Intent(Intent.ACTION_VIEW);
    videoClient.setData(Uri.parse("http://m.youtube.com/watch?v="+videoId));
    startActivityForResult(videoClient, 1234);
    

    Where videoId is the video id of the youtube video that has to be played. This code works fine on Motorola Milestone.

    But basically what we can do is to check for what activity is loaded when you start the Youtube app and accordingly substitute for the packageName and the className.

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