19

I have a video html5 tag embedded on page. I need to trigger an action when the user clicks the "Play Button". But I can't find how to bind that to my action. Is there an event for what I need? I'm using jQuery...

Thanks!

hitautodestruct
  • 20,081
  • 13
  • 69
  • 93
Andres
  • 11,439
  • 12
  • 48
  • 87

3 Answers3

27

Does this W3C demo page help? http://www.w3.org/2010/05/video/mediaevents.html It appears the play event is simply 'play'.

For example:

$('video').bind('play', function (e) {
    // do something
});

or

$('video').on('play', function (e) {
    // do something
});
Stephen Booher
  • 6,522
  • 4
  • 34
  • 50
8

I used code from the following page and stripped it:

<script language="javascript">
    document.addEventListener("DOMContentLoaded", init, false);

    function init() {
        var _video = document.getElementById("video");
        _video.addEventListener("playing", play_clicked, false);
    }

    function play_clicked() {
        alert("play was clicked");
    }
</script>

<video id='video'
  controls preload='none' 
  poster="http://media.w3.org/2010/05/sintel/poster.png">
  <source id='mp4'
    src="http://media.w3.org/2010/05/sintel/trailer.mp4"
    type='video/mp4'>
  <p>Your user agent does not support the HTML5 Video element.</p>
</video>

Hoped I could help.

Uri May
  • 365
  • 2
  • 7
7

HTML5 Video elements have an onplay event that you can use that doesn't require the extra step of adding a listener. This solution also doesn't require JQuery.

var myVid = document.getElementById('video');    
if (myVid !== null) { // Possibility of no video loaded in DOM
    myVid.onplay = function () {
        //do something
    };
}

MDN has full details.

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
Jabare Mitchell
  • 141
  • 1
  • 7