Dailymotion embedded player on iOS devices (HTML5)

2019-01-20 19:12发布

问题:

I have a Dailymotion embedded player using the Player api ( http://www.dailymotion.com/doc/api/player.html ) . It works well on a Desktop and a Android Tablet. But on a iOS device, the video just doesn't start. My code is as follows:

<!-- 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>

回答1:

Your code is perfectly valid. The thing is that most mobile devices, including iOS devices, prevent videos from being played automatically (see Apple documentation : Safari HTML5 Audio and Video Guide). On those devices, the first play must be triggered by a user interaction, such as touching the play button, otherwise it's ignored by the browser.

The apiready event is triggered by the Dailymotion SDK and is not a user event. That's why the play() method as no effect on the video.

[Edit]: You'd rather call the play() method from another event listener, such as a click or touchend event.. Also, as the Dailymotion Player is embedded within an <iframe>, the communication between the parent page and the <iframe> will always be considered as a programmatic event by the browser, no matter if the original event from the parent page comes from a user or not.

TLDR: On mobile device, you must wait for the user to touch the player's start screen.