【发布时间】:2013-10-08 13:17:46
【问题描述】:
我需要能够通过 iframe api 以编程方式将视频加载到元素中,并让它在移动设备上播放(iOS 6、iOS 7、最新的 android)。
这里是javascript:
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player
var ping = new VideoModel({
videoId: 'qMD2Jifubto',
start: 20,
end: 28
});
function VideoModel(attributes) {
this.attributes = {
videoId: attributes.videoId,
start: attributes.start,
end: attributes.end
}
this.get = function(attr) {
if (attr) {
return this.attributes[attr]
} else {
return this.attributes
}
}
}
function onYouTubeIframeAPIReady() {
player = new YT.Player('thumbnail-inner', {
height: '100%',
width: '100%',
enablejsapi: 1,
events: {
'onReady': onPlayerReady
},
rel: 0,
playerVars: {
controls: 0,
modestBranding: 1,
showinfo: 0
}
});
}
function onPlayerReady(event) {
event.target.loadVideoById({'videoId': ping.get('videoId'), 'startSeconds': ping.get('start'), 'endSeconds': ping.get('end'), 'suggestedQuality': 'large'});
}
这在台式机上完美运行,但对于移动设备(在 ios 和 android 上测试),YouTube 播放器会加载,但视频加载时显示“视频不可用”,或者只是黑屏。
您能帮忙在移动设备上完成这项工作吗?
【问题讨论】:
-
这可能会有所帮助,而不是使用 api 使用普通 iframe 嵌入 stackoverflow.com/questions/12106511/…
-
我知道嵌入代码确实有效。但是,如果我使用嵌入作为一种解决方法,我应该如何更新视频 ID?还有为什么嵌入代码有效但 iframe api 无效?
-
@CBroe ,我不介意视频需要点击播放(或通过其他用户交互),但现在也不起作用。
-
那里说
loadVideoById可能也不能正常工作。
标签: javascript ios video youtube