2012-06-20 65 views
10

我有一个链接到YouTube视频的移动网站。在Android上,单击此链接会弹出一个对话框,要求用户使用其“浏览器或Youtube应用程序完成操作”。强制视频在Android上的Youtube应用中打开

有没有办法绕过这个屏幕,只是在Youtube应用中播放视频? (例如使用youtube:// URL。)

谢谢!

回答

17

这里是你如何能做到这一点:

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

ID是在URL中问号后的标识符。例如:youtube.com/watch?v= ID

另一种方法是:

Intent videoIntent = new Intent(Intent.ACTION_VIEW); 
videoIntent.setData(url); 
videoIntent.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity"); 
startActivity(videoIntent); 

......

+0

谢谢,我会明天尝试vnd.youtube://网址,看看它是否有效。 – tba

+0

有没有办法检查(从移动网站)设备是否可以打开vnd.youtube:// URLs?我希望我的网站能够打开适用于Android的vnd.youtube://网址和常规的http://youtube.com/watch?适用于iOS的链接。 – tba

+0

至少没有我知道,对不起 – Tim

7

的最佳方式

try { 

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

} catch (ActivityNotFoundException e) { 

    // youtube is not installed.Will be opened in other available apps 

    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://youtube.com/watch?v=" + id)); 
    startActivity(i); 
} 
+0

我试图使用您的解决方案,但我得到“无法解析符号内容”。你能向我解释这是什么内容? –

+0

@MarcosGuimaraes更新 – user3716835

+0

非常感谢!它现在有效。 –

2

尝试使用类似以下的JavaScript重定向:

window.location = "vnd.youtube://the.youtube.video.url"; 

更全面:

if(/Android/i.test(navigator.userAgent)) { 
    // If the user is using an Android device. 
    setTimeout(function() { window.location = "market://details?id=com.google.android.youtube"; }, 25); 
    window.location = "vnd.youtube://www.youtube.com/watch?v=yourVideoId"; 
} 

如果的YouTube应用程式被禁用,超时功能将您重定向到Play商店的YouTube应用程序,让您启用该应用程序。第二个重定向将弹出并在Android Youtube应用上播放YouTube视频。

如果您已在超时间隔内切换到YouTube应用程序,则不会调用超时功能,并且您不会切换到Play商店,但会保留在YouTube应用程序中。

+0

我喜欢这个解决方案。这也适用于iOS吗? –

相关问题