1

当我尝试在设备上播放视频时,我正在使用代码在 Android 应用程序中播放 YouTube 视频
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(youtube_video_url)));
,它会弹出一个对话框“完成操作使用”,其中包含 Internet 和 YouTube 选项。如何防止此弹出并直接在 YouTube 播放器中打开 YouTube 视频。此外,有没有办法像 Engadget 那样直接在视频播放器中播放 YouTube 视频(通过避免 YouTube 播放器)。

4

1 回答 1

0

这是WatchActivity您有兴趣调用的

<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>

这不是默认活动,即为了直接调用它,您必须尝试以下代码

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);

在最后一次调用之后,outActivities变量将保存匹配的活动(很可能只有一个)。事实上,它对我没有任何回报。

为了确保Activity与您的匹配,您Intent可以查询包

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);

或者放弃所有漫长的程序,然后继续

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

vnd.youtubeWatchActivity如您所见,是过滤器接受的方案。

于 2011-08-03T12:18:42.840 回答