What's the play event on a video html5 element?

99封情书 提交于 2019-11-30 11:34:28

问题


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!


回答1:


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
});



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/12680432/whats-the-play-event-on-a-video-html5-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!