java - 优化在android上运行youtube视频的方式

标签 java android

我们可以有任何优化方式来运行视频。下面提到的是我的代码。此代码有助于运行 youtube 视频。

if (YouTubeIntents.canResolvePlayVideoIntent(activity)){
                            //Getting ID of YouTube video out of URL
                            List<NameValuePair> params = null;
                            try {
                                params = URLEncodedUtils.parse(new URI(sourceUrl), "UTF-8");

                            } catch (URISyntaxException e) {

                                e.printStackTrace();
                            }
                            String videoID = params.get(0).getValue();
                            Log.v(TAG, "YouTube Video ID:" + videoID);
                            // createPlayVideoIntentWithOptions only works for first
                            // video playback. Update to use stand-alone player api instead.

                            Intent intent = YouTubeStandalonePlayer.createVideoIntent(
                                    activity, g.kGoogleAPIKey,
                                    videoID, 0, true, false);
                            activity.startActivity(intent);
                            Log.i(TAG, "Video Playing in YouTube App....");                         
                        } else {
                            Log.v(TAG, "YouTube generic intent");
                            activity.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(sourceUrl)));
                            Log.i(TAG, "Video Playing in whatever user selected....");
                        }

最佳答案

你的问题有点模糊..如果你只想从你的应用程序播放youtube视频,你可以轻松地调用youtube Intent ..并传递视频的url作为额外的。这将打开 youTube..

或者,您可以访问 youTube Player api:https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/package-summary

您必须获取 api key 并执行如下操作:

此示例旨在查看您的用户是否可以使用独立播放器,以及是否如我的答案第一部分所述直接调用 YouTube

  if (YouTubeIntents.canResolvePlayVideoIntent(activity)){
                            //Getting ID of YouTube video out of URL
                            List<NameValuePair> params = null;
                            try {
                                params = URLEncodedUtils.parse(new URI(sourceUrl), "UTF-8");

                            } catch (URISyntaxException e) {

                                e.printStackTrace();
                            }
                            String videoID = params.get(0).getValue();
                            Log.v(TAG, "YouTube Video ID:" + videoID);
                            // createPlayVideoIntentWithOptions only works for first
                            // video playback. Update to use stand-alone player api instead.

                            Intent intent = YouTubeStandalonePlayer.createVideoIntent(
                                    activity, g.kGoogleAPIKey,
                                    videoID, 0, true, false);
                            activity.startActivity(intent);
                            Log.i(TAG, "Video Playing in YouTube App....");                         
                        } else {
                            Log.v(TAG, "YouTube generic intent");
                            activity.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(sourceUrl)));
                            Log.i(TAG, "Video Playing in whatever user selected....");
                        }

关于java - 优化在android上运行youtube视频的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24490554/

相关文章:

java - 正则表达式查找具有任何字母的精确计数的单词

java - 如何在 Spring Security 中允许多重登录?

java - RecyclerView notifyItemChanged(position) 使应用程序崩溃并且没有使用 notifyDataSetChanged() 的动画

android - 导航回 fragment 时 fragment 中的 Viewpager 未更新

java - NumberFormat 中的小数分隔符

java - 构建整个sql并使用preparedStatement运行它

android - 在自定义 Android 中如何覆盖 STREAM_RING 作为响应音量按钮的默认 AudioService

android - 键盘出现时如何防止滑动布局

android - 在 Android 中只允许 2 个屏幕方向?

java - 如何在android中下载页面