2012-06-20 66 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嗎? –

相關問題