iOS设备上的Dailymotion嵌入式播放器(HTML5)
问题描述:
我有一个Dailymotion嵌入式播放器,使用播放器API(http://www.dailymotion.com/doc/api/player.html)。它适用于桌面和Android平板电脑。但在iOS设备上,视频只是无法启动。我的代码如下:iOS设备上的Dailymotion嵌入式播放器(HTML5)
<!-- This <div> tag will be replaced the <iframe> video player -->
<div id="player"></div>
<script>
// This code loads the Dailymotion Javascript SDK asynchronously.
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//api.dmcdn.net/all.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(e, s);
}());
// This function init the player once the SDK is loaded
window.dmAsyncInit = function()
{
// PARAMS is a javascript object containing parameters to pass to the player if any (eg: {autoplay: 1})
var player = DM.player("player", {video: 'somevideoid', width: "100%", height: "100%", params: {autoplay: 0}});
// 4. We can attach some events on the player (using standard DOM events)
player.addEventListener("apiready", function(e)
{
e.target.play();
});
};
</script>
答
您的代码完全有效。问题是,大多数移动设备(包括iOS设备)都会阻止视频自动播放(请参阅Apple documentation : Safari HTML5 Audio and Video Guide)。在这些设备上,首次播放必须由用户交互触发,例如触摸播放按钮,否则浏览器将忽略它。
apiready
事件由Dailymotion SDK触发,不是用户事件。这就是为什么play()
方法对视频没有影响。
[编辑]:您宁可致电。 此外,由于Dailymotion播放器嵌入在内,因此浏览器始终将父页面与之间的通信视为编程事件,而不管父页面的原始事件是否来自用户或不。play()
方法从另一个事件侦听器,如click
或touchend
事件。
TLDR:在移动设备上,您必须等待用户触摸播放器的开始屏幕。