我正在嘗試爲移動設備創建一個網站,其中點擊圖像後會播放YouTube視頻。Youtube Api playVideo方法在某些移動設備上不起作用
我已經在幾款Android手機/版本上測試過,有些沒有按預期運行。
我的意思是停在緩衝區,永遠不會播放視頻。我注意到的另一件事是播放器在用戶觸發視頻播放而不是以編程方式播放之後工作。如果我直接顯示YouTube播放器,更詳細的用戶點擊播放視頻,然後點擊按鈕/圖像播放另一個視頻。
我已經張貼在這裏,我與JsFiddle
$(document).ready(function() {
// Caching jQuery objects
var $body = $('body'),
$log = $('#log'),
$yt = $('#ytplayer'),
$ytwrap = $('#ytwrapper'),
$choices = $('#choices');
// This code loads the YouTube API
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
$body.append(tag);
// This will become the player object when the API is ready
var player;
// See what kind of device we're using
var userAgent = navigator.userAgent.toLowerCase();
var isAndroid = userAgent.indexOf('android') > -1;
var isIpad = userAgent.indexOf('ipad') > -1;
var isIphone = userAgent.indexOf('iphone') > -1;
window.onYouTubeIframeAPIReady = function onYouTubeIframeAPIReady() {
player = new YT.Player('ytplayer', {
videoId: videos[0],
playerVars: {
allowfullscreen: 'allowfullscreen',
playsinline: 0
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
window.player = player;
//hide player
slidePlayer(false);
};
function onPlayerStateChange(event) {
// When a video starts playing,
// enable the fake fullscreen mode on Android & iPad
if (event.data == YT.PlayerState.PLAYING) {
if (isIpad) {
fakeFullScreen(true);
} else if (isAndroid) {
fakeFullScreen(true);
}
}
// On pause: hide the player, show thumbs
if (event.data == YT.PlayerState.PAUSED) {
if (isAndroid) {
// Exit fullscreen
fakeFullScreen(false);
// Scroll back to choices
window.scrollTo(0, playerTop);
} else if (isIpad) {
fakeFullScreen(false);
window.scrollTo(0, playerTop);
} else if (isIphone) {
slide(false);
}
}
}
$('#vstImageAd .imageWrap img').click(function (e) {
e.preventDefault();
var $this = $(this);
if (player) {
$this.css("display", "none");
slidePlayer(true);
player.playVideo();
}
});
// When a thumb image is pushed, start the video
$('#choices .playthumb img').click(function (e) {
var $this = $(this),
nr = parseInt($this.data('nr'));
if (!videos[nr]) nr = 1;
player.loadVideoById(videos[nr]);
// Hide the thumbs
slide(true);
});
});