0

I'm trying to embed a Dailymotion video (not my own) in a way that has access to Dailymotion's new 2021 Platform API and that will NOT autoplay when loaded.

<script src="https://geo.dailymotion.com/libs/player.js"></script>

<div id="video"></div>

<script>
dailymotion
    .createPlayer('video', {
        video: 'x7tgad0',
        params: {
            startTime: 0
        },
    })
    .then((player) => {
        //log player object to console
        console.log({player});
        //add listener to timechange event from the Dailymotion Platform API
        player.on(dailymotion.events.VIDEO_TIMECHANGE, (state) => console.log(state.videoTime));
      });
</script>

JSFiddle here: https://jsfiddle.net/Esn024/egrLb1tc/12/

The above code transforms <div id="video"></div> into:

<div id="video" class="dailymotion-player-root dailymotion-player-default" style="background: rgb(13, 13, 13); padding-bottom: 75%; position: relative;">
    <div class="dailymotion-player-wrapper" style="height: 100%; overflow: hidden; position: absolute; width: 100%; margin: 1e-05px;">
        <iframe allow="autoplay; fullscreen; picture-in-picture; web-share" class="dailymotion-player" frameborder="0" src="https://geo.dailymotion.com/player.html?video=x7tgad0&amp;startTime=0" title="Dailymotion video player" height="100%" width="100%" style="opacity: 1;"></iframe>
    </div>
</div>

It adds an embedded Dailymotion video that autoplays by default (note: as I discovered, it won't autoplay if the above code is saved into a .html file and opened by double-clicking it, probably because browsers behave differently with files opened from the file system, but it WILL autoplay if the same .html file is opened in a server).

I thought that maybe I could stop it from autoplaying if I removed the "autoplay" from the iframe's "allow" attribute, based on this StackOverflow question from 6 years ago about their old API, so I added the following code into the .then() block:

const iframeNode = player.getRootNode().childNodes[0].childNodes[0];
iframeNode.setAttribute("allow", "fullscreen; picture-in-picture; web-share");
iframeNode.replaceWith(iframeNode);
console.log({iframeNode});

The "allow" attribute was modified to remove "autoplay" and the video reloaded, but it still autoplayed.

I also tried modifying the autostart attribute in the player's settings from firstTimeViewable to off, but that didn't work either (at least not in the way I tried to do it below, and I couldn't figure out any other way to change the setting):

player._do_not_use_e5dbad3203d413f04bf4.settings.autostart = 'off';

I suppose this agrees with the official docs, which say that some of the player settings cannot be changed at runtime: "A Player configuration is composed of several settings that can't be modified or overridden at run time. These settings can only be updated via the Player configurator in the Dailymotion Studio or the Platform API."

Certain other settings have methods within the player object to change them after the fact, such as setAspectRatio, setVolume and some others - but there's no setAutoplay/setAutostart.

Dailymotion introduced their new player embed system in 2021, but the old API kept working until maybe a month ago. The migration guide seems to say that autoplay has 3 settings - "on", "off", "firstTimeViewable", which can be set from "Dailymotion Studio" (which I think is only available to those with paid accounts). Their online guides, as far as I can tell, seem to assume that anyone embedding videos has a paid Dailymotion account and will only embed videos from their own channel - e.g. see their Getting Started with Web SDK page which directs users to "create a custom Player configuration associated with your Dailymotion account and identified by a unique Player ID". But it seems that it's not actually mandatory since the docs also say "If the Player ID is incorrectly specified or does no longer exist the configuration will fall back to a default library configuration."

The temporary solution I have settled on right now is to pause the video the first time it starts playing (see below), but I'd like to find a way to not have the video start loading at all until clicked:

<script src="https://geo.dailymotion.com/libs/player.js"></script>

<div id="video"></div>

<script>
let pausePlayer = true;

dailymotion
    .createPlayer('video', {
        video: 'x7tgad0',
        params: {
            startTime: 0
        },
    })
    .then((player) => {
        //log player object to console
        console.log({player});
        //add listener to timechange event from the Dailymotion Platform API
        player.on(dailymotion.events.VIDEO_TIMECHANGE, (state) => {
            if (pausePlayer) {
                player.pause();
                pausePlayer = false;
            }

            console.log(state.videoTime);
        });
      });
2
  • 1
    It's not an ideal solution, but you could have the dailymotion div show a thumbnail or screenshot of the video and a custom play button and only after the user clicks on it does it load the auto-playing dailymotion video. Is the juice worth the squeeze trying to just get their video player to not autoplay upon loading?
    – elias-berg
    Commented May 21 at 14:09
  • Thank you, @elias-berg. I've added an answer below that seems like a good enough solution to me (although probably the HTML and CSS structure, which I mostly copied from the Dailymotion website, could be simplified a lot without losing too much).
    – Esn024
    Commented May 21 at 18:33

1 Answer 1

0

Inspired by elias-berg's suggestion above, here's a solution that loads a preview image + buttons that mimic what a not-autoplaying Dailymotion player should look like: https://jsfiddle.net/Esn024/egrLb1tc/37/

It can likely be done simpler, but I just copied much of the HTML and CSS from the Dailymotion screen before the video starts autoplaying (with a few changes), then added it to a loadDailymotionPreviewDiv() function, which runs when the page first loads.

When the user clicks the button in the middle, it triggers a loadVideo() function, which creates the Dailymotion player, which begins autoplaying the video. By default it starts playing muted, so I had to add a player.setMute(false); in there as well.

<!DOCTYPE html>
<head>
<script src="https://geo.dailymotion.com/libs/player.js"></script>
<style>
.player {
    display: flex;
    justify-content: center;
    width: 480px;
    height: 360px;
    background-color: black;
    position: relative;
}
.gradient {
    position: absolute;
    transition: opacity 200ms ease-in-out;
    pointer-events: none;
    background: linear-gradient(180deg,rgba(13,13,13,.6)0,rgba(13,13,13,.2) 20%,rgba(13,13,13,.2) 80%,rgba(13,13,13,.6) 100%);
    left: 0;
    bottom: 0;
    right: 0;
    top: 0;
}
.video_poster {
    height: 100%;
    width: 100%;
    position: absolute;
    background-color: #0d0d0d;
}
.video_poster_image {
    object-fit: cover;
    height: 100%;
    width: 100%;
}

.play_screen {
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
.layout {
    display: flex;
    flex-direction: column;
    height: 100%;
}
.layout_top, .layout_bottom {
    align-items: center;
    display: flex;
    flex-direction: row;
    height: 3rem;
    height: clamp(48px,3rem,51px);
    position: relative;
    z-index: 1;
    width: 100%;
}
.layout_top_left {
    height: 100%;
    margin: 0 1rem;
    min-width: 0;
}
.layout_title {
    display: block;
    margin-top: 12px;
}
.video_title_link {
    text-decoration: none;
}
.layout_title .title {
    color: #fff;
    font-size: 1rem;
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: auto;
    height: 1.5rem;
    line-height: 1.5rem;
}
.video_owner {
    color: #fff;
    font-size: .875rem;
    line-height: 1.25rem;
}
.video_owner_link {
    color: #fff;
    text-decoration: none;
}

.layout_middle {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    flex-grow: 1;
}
.play_screen .layout_bottom {
    justify-content: center;
}
.button_watch_onsite {
    align-items: center;
    background: 0 0;
    border: 0;
    border-radius: 1.5rem;
    color: #fff;
    cursor: pointer;
    display: flex;
    font-weight: 400;
    height: 2.5rem;
    padding: 0 0.75rem;
}
.button_watch_onsite-text {
    font-size: .875rem;
    font-style: italic;
    line-height: 1.5rem;
    margin-right: 0.375rem;
    text-transform: lowercase;
    text-decoration:none;
}
.button_watch_onsite .brand_wordmark {
    height: 0.75rem;
    margin-top: -0.0625rem;
}

.play_screen_content {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
}
.button_play {
    border-radius: 6.25rem;
    background: 0 0;
    color: #fff;
    display: block;
    cursor: pointer;
    font-variant-numeric: tabular-nums;
    padding: 0.125rem;
}
.button_play_content {
    background-color: rgba(13,13,13,.6);
    display: flex;
    align-items: center;
    border-radius: 6.25rem;
}
.button_play_left {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 64px;
    width: 64px;
}
.button_play {
    border-radius: 6.25rem;
    background: 0 0;
    color: #fff;
    display: block;
    cursor: pointer;
    font-variant-numeric: tabular-nums;
    padding: 0.125rem;
}
.button_play_right {
    display: block;
    line-height: 1;
    padding-right: 1.5rem;
    font-size: .875rem;
    text-transform: uppercase;
}
</style>
</head>

<body>

<div id="video"></div>

<script>
const videoId = 'x7tgad0';

const videoDiv = document.getElementById('video');
const loadVideoButton = document.getElementById('loadVideo');


//https://stackoverflow.com/a/61335543/14926023
function secondsToTime(e){
    const h = Math.floor(e / 3600).toString().padStart(2,'0'),
          m = Math.floor(e % 3600 / 60).toString().padStart(2,'0'),
          s = Math.floor(e % 60).toString().padStart(2,'0');
    
    return h + ':' + m + ':' + s;
    //return `${h}:${m}:${s}`;
}

function loadDailymotionPreviewDiv(videoId, videoDiv) {
    fetch(`https://api.dailymotion.com/video/${videoId}?fields=thumbnail_360_url,title,duration,owner.username`)
    .then(res => res.json())
        .then((json) => {
        console.log({json});
            const imgSrc = json.thumbnail_360_url;
            const title = json.title;
            const time = secondsToTime(json.duration);
            const username = json['owner.username'];
        
            const overlayHtml = `
<div class="player">            
<div class="video_poster"><img class="video_poster_image" src="${imgSrc}" alt=""></div>
  <div class="gradient" style="opacity: 1;"></div>
  <div class="play_screen">
  <div class="play_screen_backdrop" role="button" tabindex="0"></div>
  <div class="layout">
    <div class="layout_top">
      <div class="layout_top_left">
        <div class="layout_title">
          <a class="video_title_link" href="https://www.dailymotion.com/video/${videoId}" target="_blank">
            <div class="title">${title}</div>
          </a>
        </div>
        <div class="layout_signature">
          <div class="video_owner">
            <a class="video_owner_link" href="https://www.dailymotion.com/${username}" target="_blank">${username}</a>
          </div>
        </div>
      </div>
    </div>
    <div class="layout_middle"></div>
    <div class="layout_bottom">
      <button class="button_watch_onsite" onclick="window.open('https://www.dailymotion.com/video/${videoId}','_blank')" type="button">
        <div class="button_watch_onsite-text">Watch on</div>
        <svg class="brand_wordmark" aria-hidden="true" viewBox="0 0 131 20" xmlns="http://www.w3.org/2000/svg" aria-labelledby="logo-alt-text" role="img">
          <title id="logo-alt-text">Dailymotion</title>
          <path fill="currentColor" d="M26.815 19.657h-2.39l4.53-19.33h2.366l-4.504 19.33h-.002ZM37.933 19.657H29.95L34.513.327h2.39l-4.057 17.166h5.587l-.5 2.164ZM44.193 12.182l-1.725 7.475h-2.42l1.752-7.531-1.333-11.8h2.42l.695 9.42L48.78.326h2.697l-7.285 11.856ZM97.724 2.49H94.22l-3.785 17.167h-2.42L91.8 2.49h-3.837l.53-2.163h9.76l-.529 2.163ZM97.646 19.657h-2.143L99.788.327h2.363l-4.255 19.33h-.25ZM113.266 5.764l-2.113 9.485c-.287 1.343-.688 2.303-1.208 2.987-.791 1.07-2.088 1.591-3.842 1.591-2.893 0-4.551-1.262-4.583-3.426-.01-.63.067-1.206.3-2.246l2.113-9.484c.702-3.21 2.35-4.664 5.216-4.664 2.865 0 4.357 1.316 4.389 3.509.01.63-.066 1.288-.274 2.246h.002v.002Zm-4.226-3.727c-.834 0-1.47.273-1.905.82-.357.44-.54 1.014-.805 2.138l-2.113 9.538c-.131.575-.205 1.123-.2 1.535.017 1.152.777 1.78 2.083 1.78 1.391 0 2.131-.793 2.574-2.767l2.14-9.429c.208-.933.287-1.371.28-1.81-.018-1.18-.75-1.81-2.059-1.81h.002l.003.005ZM53.33.347h4.007l1.1 11.083c.199 2.136.31 3.861.454 6.195h.057a91.22 91.22 0 0 1 2.764-6.195L67.18.347h4.063l-2.17 19.303H66.42l1.384-11.526c.254-2.084.537-3.862.876-5.999l-.057-.029c-.96 2.083-1.807 3.915-2.849 6.028L60.19 19.65h-3.415L55.704 8.124a221.94 221.94 0 0 1-.48-5.999h-.055a129.08 129.08 0 0 1-1.524 5.999L50.542 19.65h-2.68L53.331.347ZM87.087 9.987C85.654 17.4 82.537 20 77.62 20c-3.878 0-6.013-1.66-6.013-5.92 0-1.274.139-2.52.45-4.067C73.547 2.602 76.61 0 81.554 0c3.848 0 6.013 1.66 6.013 5.92 0 1.275-.168 2.52-.478 4.067h-.002Zm-5.845-7.938c-3.202 0-5.364 2.076-6.544 8.02-.31 1.382-.478 2.739-.478 3.847 0 2.875 1.235 4.037 3.68 4.037 3.231 0 5.364-2.075 6.574-8.02.282-1.381.477-2.738.477-3.847 0-2.85-1.234-4.037-3.709-4.037ZM8.49.322c-.458 0-4.464.066-4.464.066L0 19.688h1.898v-.002c1.081-.01 1.482.012 2.539.012 4.936 0 8.061-2.215 9.499-9.604h.002c.31-1.542.48-2.782.48-4.052 0-4.249-2.063-5.72-5.928-5.72Zm3.036 9.765c-1.215 5.925-3.16 7.28-6.404 7.297-1.133.004-1.39-.005-2.25-.018L5.954 2.473v.005c.965-.022 1.193-.032 2.079-.022 2.484.022 3.974.96 3.974 3.8 0 1.104-.196 2.457-.478 3.833l-.002-.002ZM127.766.322l-3.825 17.315L121.1.322h-3.998l-4.348 19.342h2.514l3.823-17.612 2.818 17.612h3.957L130.189.322h-2.423ZM20.728.328l-7.953 19.33h2.557l1.447-3.613.918-2.244.324-.867h2.682l-.086.867-.196 2.246-.277 3.614h2.42l1.5-19.333h-3.338.002ZM18.87 10.661l3.08-8.579-1.014 8.579H18.87Z"></path>
        </svg>
      </button>
    </div>
  </div>
  <div class="play_screen_content">
    <button class="button_play" aria-label="Playback, ${time}">
      <span class="button_play_content">
        <span class="button_play_left" style="padding: 8px;">
          <svg viewBox="0 0 24 24" aria-hidden="true" xmlns="http://www.w3.org/2000/svg">
            <path d="M8.56047 5.09337C8.34001 4.9668 8.07015 4.96875 7.85254 5.10019C7.63398 5.23162 7.5 5.47113 7.5 5.73011L7.5 18.2698C7.5 18.5298 7.63398 18.7693 7.85254 18.9007C7.96372 18.9669 8.0882 19 8.21268 19C8.33241 19 8.45309 18.9688 8.56047 18.9075L18.1351 12.6377C18.3603 12.5082 18.5 12.2648 18.5 12C18.5 11.7361 18.3603 11.4917 18.1351 11.3632L8.56047 5.09337Z" fill="currentColor"></path>
          </svg>
        </span>
        <span class="button_play_right">${time}</span>
      </span>
    </button>
  </div>
</div>
</div>`;

            videoDiv.innerHTML = overlayHtml;
      
            const playContentButton = document.querySelector('#video .button_play');
            playContentButton.addEventListener('click', () => {
                loadVideo(videoId, videoDiv);
            });
      
        });
}

function loadVideo(videoId) {
  dailymotion
      .createPlayer('video', {
        video: videoId,
        params: {
          startTime: 0
        },
      })
      .then((player) => {
        console.log({player});
        player.setMute(false);
        player.on(dailymotion.events.VIDEO_TIMECHANGE, (state) => {console.log(state.videoTime);});
        });
}


loadDailymotionPreviewDiv(videoId, videoDiv);

</script>
</body>
</html>

Not the answer you're looking for? Browse other questions tagged or ask your own question.