0

How to play video from dailymotion using javascript ?

It's not work, how can i do for work ?

https://jsfiddle.net/4tdac0rL/1/

<script type="text/javascript">
window.onload=function(){
    var player = DM.player(document.getElementById('player-dailymotion'), {
        video: 'x5d2gjc'
    });
}
</script>

<script>
function play_fn(){
    player.play();
}
</script>
mongkon mamiw
  • 179
  • 2
  • 5
  • 14
  • 1
    You can't – it's an iframe and you can't control iframes that are [cross-origin](http://stackoverflow.com/a/6170976/484780) – Kevin Jantzer Apr 04 '17 at 15:30

2 Answers2

1

This works for me when I try your JSFiddle:

<div id="player-dailymotion"></div>
<br/>
<div onclick="play_fn()">Play</div>

<script type="text/javascript">
var player;
window.onload=function(){
    player = DM.player(document.getElementById('player-dailymotion'), {
        video: 'x5d2gjc'
    });
}
function play_fn(){
    player.play();
}
</script>
ApocalypsjeNL
  • 45
  • 1
  • 7
1

This is possible to do via the documentation daily motion provides here: https://developer.dailymotion.com/player

Example with a video play and pause button:

<div id="player-dailymotion"></div>
<br/>
<button type="button" onclick="play_fn()">Play</button>
<button type="button" onclick="play_pause()">Pause</button>


<script>
//assign variable player to dailymotion player
var player = DM.player(document.getElementById('player-dailymotion'), {
        video: 'x5d2gjc'
    });


function play_fn(){
    //call play on dailymotion player
    player.play();
}

function play_pause(){
 //call pause on dailymotion player
 player.pause();
}
</script>

JS fiddle working example: https://jsfiddle.net/l33tstealth/eoxrr3t5/1/

l33tstealth
  • 821
  • 8
  • 15