7

当我尝试在姜饼上运行 youtube api StandAlonePlayerActivity 时出现 ActivityNotFoundException 错误。因为它在 ICS 上运行良好

这是我的原木猫

01-23 15:34:28.071: E/AndroidRuntime(3378): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.google.android.youtube.api.StandalonePlayerActivity.START (has extras) }
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1515)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.app.Activity.startActivityForResult(Activity.java:2988)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:701)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.support.v4.app.Fragment.startActivity(Fragment.java:787)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at com.example.demo.TipsFragment$2.onClick(TipsFragment.java:143)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.view.View.performClick(View.java:2533)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.view.View$PerformClick.run(View.java:9320)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.os.Handler.handleCallback(Handler.java:587)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.os.Looper.loop(Looper.java:150)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at android.app.ActivityThread.main(ActivityThread.java:4389)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at java.lang.reflect.Method.invoke(Method.java:507)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
01-23 15:34:28.071: E/AndroidRuntime(3378):     at dalvik.system.NativeStart.main(Native Method)

这是我用来启动 Player 的代码

String vid=getYoutubeVideoId(vlink);
Intent intent=YouTubeStandalonePlayer.createVideoIntent(getActivity(),DEVELOPER_KEY, vid, 0,true,false);
startActivity(intent);

我收到错误startActivity

这是 getYoutubeVideoID() 代码:

/*
     * Getting id of video from url
     */
    public static String getYoutubeVideoId(String youtubeUrl)
    {
        String video_id="";
        if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http"))
        {

            String expression = "^.*((youtu.be"+ "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
            CharSequence input = youtubeUrl;
            Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(input);
            if (matcher.matches())
            {
                String groupIndex1 = matcher.group(7);
                if(groupIndex1!=null && groupIndex1.length()==11)
                    video_id = groupIndex1;
            }
        }
        return video_id;
    }
4

6 回答 6

19

首先检查您的设备上是否可以使用 youtube 服务,然后尝试启动该活动。 YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(mContext). 还可以尝试从 Google Play 安装 youtube 应用。

文档中的小注释: Note: Users need to run version 4.2.16 of the mobile YouTube app (or higher) to use the API.

编辑:这是你如何使用它:

if(YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(mContext).equals(YouTubeInitializationResult.SUCCESS)){
   //This means that your device has the Youtube API Service (the app) and you are safe to launch it. 

}else{
   // Log the outcome, take necessary measure, like playing the video in webview :)  
}

请参阅YouTubeInitializationResult此处的枚举https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubeInitializationResult

还有关于上述课程的哲学: https ://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubeApiServiceUtil

于 2013-01-23T10:50:03.970 回答
1

设备应安装最新的 YouTube 应用程序。

于 2013-01-23T12:02:03.960 回答
0

你必须在你的设备上安装 YouTube Android 应用程序,它对我有用!

于 2014-05-15T16:38:25.477 回答
0

为了使用android 的YouTube api,您需要 3 个参数

1) YouTube开发者密钥

2)视频ID

3)最新的官方YouTube Android App(4.2.16版)

通过查看您的日志猫,我认为其中一个丢失了

参考

于 2013-01-23T10:28:28.583 回答
0

您是否在模拟器上运行该应用程序?它可能无法在模拟器上运行,因为默认情况下未安装 youtube 播放器。尝试在实际设备上运行它。

于 2013-01-23T10:14:13.767 回答
0

我会建议对@Nikola 的回答进行一些改进。可以显示本地化对话框getErrorDialog()来处理一些用户可恢复的错误isUserRecoverableError。例如,当 YouTube 应用在“设置”中被禁用时。

val result = YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(context)
when {
    result == YouTubeInitializationResult.SUCCESS -> startYouTubeActivity()
    result.isUserRecoverableError -> result.getErrorDialog(activity, 0).show()
    else -> toast("Something went wrong!")
}
于 2020-04-23T12:52:01.647 回答