Search code examples
javascriptvideoautoplaydailymotion-api

Dailymotion video autoplay and muted


I am currently working on a project where i need to display a Dailymotion video that is automatically launched in muted mode. According to the documentation - http://www.dailymotion.com/doc/api/sdk-javascript.html - the DM.player is able to manipulate the volume of a video (method: setMuted(muted)). However, after many changes in my code, i cannot figure out how this works. Have you ever done this before ? Could you provide some help please ?

Thanks

Here is my code:

<html>

<head>
  <script src="http://api.dmcdn.net/all.js"></script>
</head>

<body>
  <div id="myPlayer"></div>

  <script>
    // 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("myPlayer", {video: "xz0ytt", width: "480", height: "270"});

      // 4. We can attach some events on the player (using standard DOM events)
      player.addEventListener("apiready", function(e) {
        // alert(e.target.muted);
        // e.target.muted = true;
        // alert(e.target.muted);
        // e.target.play();
        // player.setMuted(1);
        player.setMuted("1");
        e.target.play();
      });
    };
  </script>
</body>

</html>

Solution

  • The method you are trying to use can only work after the video is playing. Hence, you have to listen to the event "play" to mute the video.

    try the following:

    player.addEventListener("apiready", function(e) {
        e.target.play();
    });
    player.addEventListener('play', function(e){
        e.target.setMuted(1);
    });
    

    the advertisement (if any) at the beginning can't be muted though,