skip to Main Content

I want to catch the event, when the users clicks on any Dailymotion video.

When there is only one video on the page, it is clear.

// Get access to the embedded player instance using one of the get player methods.
const callback = (state) => {
}
dailymotion
.getPlayer()
.then((player) => {
// Add an event listener to the event 'PLAYER_START'
player.on(dailymotion.events.PLAYER_START, callback)
});

But there are several videos.

Also there are some videos which are loaded by ajax on button click.

var player = dailymotion.createPlayer(iframeID, {
            video: videoSrc,
            params: {
        },
          })
          .then((player) => console.log(player));

How can I add the listener for them?

2

Answers


  1. Chosen as BEST ANSWER

    Resolved based on the answer of @Milos Stojanovic.

       // Get access to all embedded player instances on the page
    Promise.all(dailymotion.getAllPlayers()).then((players) => {
        // Add an event listener to each player for the 'PLAYER_START' event
        players.forEach((player) => {
            player.on(dailymotion.events.PLAYER_START, callback)
        });
    })
    

    For videos which are loaded by ajax on button click.

    var player = dailymotion.createPlayer(iframeID, {
                    video: videoSrc,
                    params: {
                },
                  })
                  .then((player) => {
        player.on(dailymotion.events.PLAYER_START, callback)
        });
    

  2. Maybe something like this:

    // Define the callback function
    const callback = (state) => {
        alert("Received PLAYER_START event");
    };
    
    // Get access to all embedded player instances on the page
    Promise.all(dailymotion.getAllPlayers()).then((players) => {
        // Add an event listener to each player for the 'PLAYER_START' event
        players.forEach((player) => {
            player.on(dailymotion.events.PLAYER_START, callback);
        });
    })
    .catch((error) => console.error(error));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search