Android YouTube应用播放video意图

我已经创build了一个应用程序,您可以下载Android的YouTubevideo。 现在,我希望如果您在YouTube本地应用中播放video,也可以下载。 为此,我需要了解YouTube本机应用为了播放YouTube应用而推出的意图。
如果我的模拟器上有YouTube程序,我可以简单地做到这一点,所以我的第一个问题是:
1.我可以下载我的模拟器的YouTube应用程序,或…
2.用户selectvideo进行播放时使用的意图是什么。

那这个怎么样

public static void watchYoutubeVideo(Context context, String id){ Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id)); Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + id)); try { context.startActivity(appIntent); } catch (ActivityNotFoundException ex) { context.startActivity(webIntent); } } 

这将在一个设备上工作,但不是每个Lemmy答案 的模拟器

 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM"))); 

以下是我如何解决这个问题:

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + video_id); startActivity(intent); 

现在我已经做了更多的研究,看起来我只需要“vnd.youtube:VIDEO_ID”而不是冒号后面的两个斜线(':'和'://'):

http://it-ride.blogspot.com/2010/04/android-youtube-intent.html

我在这里尝试了大部分的build议,并且他们并没有像所有提出异议的“直接”方法那样工作得很好。 我假设,用我的方法,如果没有安装YouTube应用程序,操作系统有一个默认的回退位置,而不是崩溃的应用程序。 这个应用程序理论上只会在设备上使用YouTube应用程序,所以这应该不是问题。

使用我的代码..我能够使用此代码播放YouTubevideo…您只需要在“videoId”variables中提供YouTubevideoID ….

 String videoId = "Fee5vbFLYM4"; Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId)); intent.putExtra("VIDEO_ID", videoId); startActivity(intent); 
  Intent videoClient = new Intent(Intent.ACTION_VIEW); videoClient.setData(Uri.parse("http://m.youtube.com/watch?v="+videoId)); startActivityForResult(videoClient, 1234); 

其中videoId是必须播放的YouTubevideo的videoID。 此代码在Motorola Milestone上运行正常。

但基本上我们可以做的是检查当你启动Youtube应用程序时加载了哪些活动,并相应地replacepackageName和className。

Youtube(和Market应用程序)只能用于Google为G1和G2发布的特殊ROM。 所以不能在OpenSource-ROM中运行它们,就像仿真器中使用的那样。 那么,也许你可以,但不是以正式支持的方式。

find了:

 03-18 12:40:02.842: INFO/ActivityManager(68): Starting activity: Intent { action=android.intent.action.VIEW data=(URL TO A FLV FILE OF THE VIDEO) type=video/* comp={com.google.android.youtube/com.google.android.youtube.YouTubePlayer} (has extras) } 

编辑:下面的实现certificate至less有一些HTC设备(他们坠毁)有问题。 出于这个原因,我不使用setclassname并坚持行动select器菜单。 我强烈build议不要使用旧的实现。

以下是旧的实施:

 Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(youtubelink)); if(Utility.isAppInstalled("com.google.android.youtube", getActivity())) { intent.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity"); } startActivity(intent); 

凡实用程序是我自己的个人工具类与以下方法:

 public static boolean isAppInstalled(String uri, Context context) { PackageManager pm = context.getPackageManager(); boolean installed = false; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); installed = true; } catch (PackageManager.NameNotFoundException e) { installed = false; } return installed; } 

首先我检查是否安装了youtube,如果安装了,我告诉android我更愿意打开我的意图。

回答老问题,只是为了告诉大家,包已经改变了,inheritance人更新

 Intent videoClient = new Intent(Intent.ACTION_VIEW); videoClient.setData("VALID YOUTUBE LINK WITH HTTP"); videoClient.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity"); startActivity(videoClient); 

这工作得很好,但是当你使用ACTION_VIEW调用正常的Intent和有效的youtube URL时,用户无论如何都会得到Activityselect器。

Youtube现在有一个玩家API,你应该尝试。

https://developers.google.com/youtube/android/player/

 /** * Intent to open a YouTube Video * * @param pm * The {@link PackageManager}. * @param url * The URL or YouTube video ID. * @return the intent to open the YouTube app or Web Browser to play the video */ public static Intent newYouTubeIntent(PackageManager pm, String url) { Intent intent; if (url.length() == 11) { // youtube video id intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + url)); } else { // url to video intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); } try { if (pm.getPackageInfo("com.google.android.youtube", 0) != null) { intent.setPackage("com.google.android.youtube"); } } catch (NameNotFoundException e) { } return intent; } 

在其他应用程序上运行video的最安全的方法是首先尝试parsing该软件包,换句话说,请检查该应用程序是否安装在设备上。 所以如果你想在YouTube上播放video,你可以这样做:

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

您也可以只抓取WebViewClient

 wvClient = new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("youtube:")) { String youtubeUrl = "http://www.youtube.com/watch?v=" + url.Replace("youtube:", ""); startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(youtubeUrl))); } return false; } 

在我的应用程序工作得很好。

尝试这个:

 public class abc extends Activity implements OnPreparedListener{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM"))); @Override public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub } } } 

尝试这个,

  WebView webview = new WebView(this); String htmlString = "<html> <body> <embed src=\"youtube link\"; type=application/x-shockwave-flash width="+widthOfDevice+" height="+heightOfDevice+"> </embed> </body> </html>"; webview.loadData(htmlString ,"text/html", "UTF-8"); 

这将工作,如果youtube的应用程序安装。 如果没有,select器将显示select其他应用程序:

 Uri uri = Uri.parse( "https://www.youtube.com/watch?v=bESGLojNYSo" ); uri = Uri.parse( "vnd.youtube:" + uri.getQueryParameter( "v" ) ); startActivity( new Intent( Intent.ACTION_VIEW, uri ) ); 

这个函数对我来说工作正常…只是在函数中传递urlstring:

 void watch_video(String url) { Intent yt_play = new Intent(Intent.ACTION_VIEW, Uri.parse(url)) Intent chooser = Intent.createChooser(yt_play , "Open With"); if (yt_play .resolveActivity(getPackageManager()) != null) { startActivity(chooser); } }