Android YouTube app Play Video Intent

前端 未结 18 1461
太阳男子
太阳男子 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: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);
    }
    

提交回复
热议问题