2016-01-20 167 views
-1

我嘗試在我的應用程序(Appcelerator,Android)中顯示YT視頻。我發現,最好的辦法是顯示的WebView嵌入式視頻,所以我有這樣的代碼做到這一點:嵌入Youtube視頻到WebView(Appcelerator)不顯示(黑屏)

var webView = Ti.UI.createWebView({ 
    url: 'https://www.youtube.com/embed/LTRfmqc0KBg', 
    enableZoomControls: false, 
    scalesPageToFit: true, 
    scrollsToTop: false, 
    showScrollbars: false 
}); 

但視頻沒有加載(我只看到黑色畫面 - 代替的WebView白色)。 WebView正常工作,因爲它顯示其他頁面。

+0

嗨!你有沒有找到這個答案? –

回答

0

我們發現了這種方法的一些問題,並最終放棄了它。我們回到使用Appcelerator原生的視頻播放器。

我想你可能會發現你使用的URL不正確。儘管您自己查看視頻時會使用此功能,但不適合嵌入到應用程序中。

我們最終使用Youtube API獲取嵌入到Youtube中的視頻的原始URL。最終我們放棄了這一點,因爲我們不希望YouTube上的廣告內容粘貼在頂部。

+0

那麼你的回答是什麼,因爲你給我寫了一些建議,最後你說他們都是錯的...... – kreatywny

0

您可以調用設備默認的YouTube應用來打開用戶的網址。請看下面的代碼

var Win = Titanium.UI.createWindow({ 
    title : 'Youtube Video View Demo', 
    backgroundColor : '#fff' 
}); 
var button = Titanium.UI.createButton({ 
    title: 'Hello', 
    top: 10, 
    width: 100, 
    height: 50 
}); 
button.addEventListener('click',function(e) 
{ 
    Titanium.Platform.openURL('https://www.youtube.com/watch?v=bSiDLCf5u3s'); 
}); 
Win.add(button); 
Win.open(); 

謝謝。

+0

謝謝謝里夫,但它在瀏覽器/ YT應用程序中打開視頻。我想在應用程序內部顯示視頻,而不是在外面... – kreatywny

2

那麼你可以試試這個

var Win = Titanium.UI.createWindow({ 
    title : 'Video View Demo', 
    backgroundColor : '#fff' 
}); 
var video_url = 'https://www.youtube.com/watch?v=bSiDLCf5u3s'; 
var movie = '<html><head></head><body style="margin:0"><embed id="yt" src="' + video_url + '" type="application/x-shockwave-flash" width="480" height="266"></embed></body></html>'; 


var webView = Ti.UI.createWebView({ 
    top:0, 
    left:0, 
    width:480, 
    height:266, 
    url:video_url, 
    html:movie 
}); 

Win.add(webView); 
Win.open(); 
+0

這適用於iOS平臺。 – Wikki

0

我發現,嵌入式視頻不會在Android上工作,而iOS上做得很好。 但是使用webviews url屬性切換窗體來加載視頻,以使用setHtml()函數起作用。做到這一點的方法是使用Youtube iframe api。

var videoUrl = 'https://www.youtube.com/embed/' + videoId + '? autoplay=1&autohide=1&cc_load_policy=0&color=white&controls=0&fs=0&iv_load_policy=3&modestbranding=1&rel=0&showinfo=0'; 
var playerWidth = $.youtubeWebView.width; 
var playerHeight = $.youtubeWebView.height; 
var html = '<iframe id="player" type="text/html" width="'+playerWidth+'" height="'+playerHeight+'" src="'+videoUrl+'" frameborder="0"></iframe>'; 
$.youtubeWebView.setHtml(html); 

擡起頭,I幀可以是一個痛苦,在負載情況下添加此獲得在頂部&左側擺脫了奇怪的白色填充

this.evalJS('document.getElementsByTagName( 「本體」)[0] .style.margin = 0;');

事情是這樣的:

$.youtubeWebView.addEventListener('load', function(){ 
    this.evalJS('document.getElementsByTagName("body")[0].style.margin=0;'); 
    var showYoutubeTimer = setTimeout(function() { 
     $.activityIndicator.hide(); 
    $.youtubeWebView.opacity = 1; 
    clearTimeout(showYoutubeTimer); 
    }, 300); 
});