【问题标题】:Embedding a youtube video programmatically via iFrame API does not play on mobile通过 iFrame API 以编程方式嵌入 youtube 视频无法在移动设备上播放
【发布时间】: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


【解决方案1】:

如果用户交互不是问题,您可以在您的 html 文件(在 iframe 中)中设置一个等于 1 的自动播放属性。它适用于android。

    <iframe src="http://www.youtube.com/embed/#{video_id}?enablejsapi=1&autoplay=1"></iframe>

看起来 android 浏览器不响应 youtube api,尤其是 iframe 和 YTplayer api。它只是没有以编程方式启动。

【讨论】:

  • 可能这个答案需要更新,因为它没有用,至少现在是https 并且可能还会进一步改进,谢谢
猜你喜欢
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 2015-11-03
  • 1970-01-01
  • 2014-11-30
  • 2022-10-17
相关资源
最近更新 更多