1

I am playing YouTube videos in android application using code
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(youtube_video_url)));
when i try to play video on device, it pop up a dialog box "complete action using" with options Internet and YouTube. how can i prevent this pop up and open the YouTube video directly in YouTube player. Further more, is there any way to play YouTube videos directly in video player (by avoiding the YouTube player ) like Engadget do.

Asif
  • 21
  • 4
  • 9
  • Why would you want that? What if a user does not have that specific player, or wants another one? The choice should be the users'. If you want to open youtube as default, de user can actually just add that setting (the default app setting) for an intent like this. It is taking default options away from the user, which is never a good thing. – Nanne Aug 03 '11 at 12:27
  • In my case i have to use YouTube as default, how can i make that settings, because one of YouTube application running on android device is not showing popup, but my application does. i need to avoid that "complete action using" popup – Asif Aug 03 '11 at 12:47
  • My argument is that you don't need to avoid it. If the user wants to avoid it for movies, let them do so by setting up their handset correctly (the default application setting). If the user wants to see youtube movies in the browser, let them. If they do not, they can Choose not to? you have not provided any argument why you should want this? – Nanne Aug 03 '11 at 13:07
  • Default application settings works, but I want to avoid setting the default options manually, because my app will have only YouTube videos so it should be played directly in YouTube player. Also this is customer requirement to play YouTube video directly in a full screen video player other than YouTube, or at least avoid that popup if it has to be played in YouTube. – Asif Aug 04 '11 at 12:38
  • Another reason to avoid the "complete action using" is for kiosks or COSU devices (Corporate Owned Single Use). I've run into this problem trying to get TextToSpeech work without requiring the user to make a choice. Even more so, since the user may have initiated the TextToSpeech action by giving a voice command, so they are not looking at the device. – Stephen M -on strike- Jan 24 '19 at 19:45

1 Answers1

0

Here's the WatchActivity which you are interested to invoke

<activity android:name="WatchActivity" android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation" android:allowTaskReparenting="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/watch" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/v/" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/e/" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="vnd.youtube" />
    </intent-filter>
</activity>

It's not the default activity, that is in order to call it directly, you'll have to try the following code

String youtubePackage = "com.google.android.youtube";
ArrayList<IntentFilter> intentFilters = new ArrayList<IntentFilter>();
/*
 * Filter to match
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/watch" />
    </intent-filter>
 */
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_VIEW);
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addCategory(Intent.CATEGORY_BROWSABLE);
//filter.addDataScheme("http");
//filter.addDataAuthority("www.youtube.com", null);//port null will allow any port
//filter.addDataPath("/watch", PatternMatcher.PATTERN_PREFIX);
intentFilters.add(filter);
ArrayList<ComponentName> outActivities = new ArrayList<ComponentName>();
getPackageManager().getPreferredActivities(intentFilters, outActivities, youtubePackage);

After the final call, the outActivities variable will hold matching activities (most likely just one). As a matter of fact, it doesn't return anything for me.

To make sure what Activitys match your Intent you can query the package

Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.youtube.com/watch?v=EUXnJraKM3k"));         
List<ResolveInfo> matchingActivities = getPackageManager().queryIntentActivities(videoIntent, PackageManager.MATCH_DEFAULT_ONLY);

Or ditch all that long procedure and just go with

Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:<video id>"));
startActivity(videoIntent);

vnd.youtube is a scheme accepted by the WatchActivity filter as you see.

Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
  • When you `startActivityForResult`, you pass a number as the second argument which is your own identifier for this activity. Ignore the last line, use plain `startActivity(videlClient)` – Ilya Saunkin Aug 03 '11 at 12:31
  • Should following line remains same or i should change it to my own package and activity ? videoClient.setClassName("com.google.android.youtube", "com.google.android.youtube.PlayerActivity"); because i tried the code as you mentioned but it throws exception. – Asif Aug 03 '11 at 12:51
  • I guess that exceptions tells you something like `have you declared this activity in your AndroidManifest.xml?`, doesn't it? Which actually gives you an insight that you have to declare the `com.google.android.youtube.PlayerActivity` activity in your manifest, pretty much like the rest of your activities. – Ilya Saunkin Aug 03 '11 at 13:05
  • and no, the line has to be what it is, `com.google.android.youtube` package and the `PlayerActivity` activity. – Ilya Saunkin Aug 03 '11 at 13:07
  • I have specified activity in AndroidManifest.xml as following but it still gives an exception: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.android.youtube/com.google.android.youtube.PlayerActivity}; have you declared this activity in your AndroidManifest.xml? Here is some discussion about the same issue. [com.google.android.youtube intent not found](http://stackoverflow.com/questions/6303223/com-google-android-youtube-intent-not-found) – Asif Aug 04 '11 at 06:11
  • getPackageManager().getLaunchIntentForPackage("com.google.android.youtube") is null when executes – Asif Aug 04 '11 at 07:07
  • Does getPackageManager() or getLaunchIntentForPackage("com.google.android.youtube") return null? – Ilya Saunkin Aug 04 '11 at 07:09
  • getPackageManager() is okay but getLaunchIntentForPackage("com.google.android.youtube") return null. – Asif Aug 04 '11 at 07:10
  • Do you have youtube installed on your device? I think it doesn't exist on emulator... Otherwise, if you do have it, find a way to figure out the package in its AndroidManifest. – Ilya Saunkin Aug 04 '11 at 07:27
  • you are right, the object was null because YouTube is not installed on emulator, however it is installed on device, so when i play video on device it opens YouTube application with its home page, and does not show/play my video, although i have provided correct YouTube video url. – Asif Aug 04 '11 at 08:19
  • alright, we need to nail the `WatchActivity` now, check out the edit and let's see if it works. – Ilya Saunkin Aug 04 '11 at 09:39
  • actually, the last three lines are the important ones – Ilya Saunkin Aug 04 '11 at 09:44
  • above code plays video in YouTube player with a "complete action using" popup which is already happening in my case. i was searching for a solution to avoid the popup grammatically, secondly i was searching for a solution to play video directly in a android native player. The problem remains at the same stage. – Asif Aug 04 '11 at 12:27