admin管理员组

文章数量:1417070

Why doesn't play event get triggered when the video with autoplay first starts playing after loading the page?

document.getElementById("vid").addEventListener('play', function, false);
<video id="vid" src="video.mp4" type="video/mp4" autoplay></video>

Why doesn't play event get triggered when the video with autoplay first starts playing after loading the page?

document.getElementById("vid").addEventListener('play', function, false);
<video id="vid" src="video.mp4" type="video/mp4" autoplay></video>

It should fire the play event I think, but doesn't.

Is this by design? and if so, why?

Manually pressing play works normally.

Share Improve this question edited Jan 8, 2019 at 16:02 Alexandre Elshobokshy 10.9k6 gold badges34 silver badges60 bronze badges asked Jan 5, 2019 at 15:54 DD3RDD3R 1451 silver badge8 bronze badges 5
  • What are you trying to achieve? – Alexandre Elshobokshy Commented Jan 7, 2019 at 12:45
  • Do something when video starts playing. In Firefox. – DD3R Commented Jan 8, 2019 at 13:07
  • It works tho, so again what's the problem? jsfiddle/d1sgqnfL – Alexandre Elshobokshy Commented Jan 8, 2019 at 13:26
  • You get an alert when the page loads and the video starts playing after a reload? *in Firefox jsfiddle/usrnm/y3gm74wz/1 – DD3R Commented Jan 8, 2019 at 14:01
  • Yes, but please refer to this answer to understand what you need to do for it to work on jsfiddle. – Alexandre Elshobokshy Commented Jan 8, 2019 at 14:11
Add a ment  | 

4 Answers 4

Reset to default 4

For future users, this could be caused by a race condition between the time the video starts auto-playing and the event handler being added.

If the handler is added after the video has already started playing, the play event will not fire. This can happen when scripts are added at the bottom of a page and the video element above is set to autoplay.

See the snippet below:

// add the handler after 500ms which assumes the video is already playing
setTimeout(() => {
  document.querySelector('#vid').addEventListener('play', (e) => {
    console.info('playing');
  });;
}, 500);

// this will fire as expected
// document.querySelector('#vid').addEventListener('play', (e) => {
//  console.info('playing');
// });;
<video height="100px" id="vid" src="https://sample-videos./video123/mp4/720/big_buck_bunny_720p_1mb.mp4" type="video/mp4" autoplay controls></video>

Yes, it depends on the browsers, you can get more info here to understand how Chrome works for example: https://developers.google./web/updates/2017/09/autoplay-policy-changes

What you can try for Chrome is to mute the video with the attribute muted and let the user unmute it:

<video id="vid" src="video.mp4" type="video/mp4" autoplay muted></video>

<script>
document.getElementById("vid").addEventListener('play', function, false);
</script>

See it in action here: https://codepen.io/anon/pen/pqLxZV?editors=1011

It should work according to the spec (https://www.w3/TR/html53/semantics-embedded-content.html#media-elements-event-summary):

From tests on Safari (12.0.2) and Chrome (71.0.3578.98) on OSX (10.12.6) it does appear to work:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple Video Test</title>
</head>

<body>
  <h1>Video Test</h1>
  <video id="theVideo" autoplay loop muted playsinline>
    <source src="https://media.w3/2010/05/sintel/trailer.mp4">
  </video>
</body>

<script>
var vid = document.getElementById("theVideo");
vid.onplay = function() {
    alert("Starting to play video");
};
</script>

</html>

You can do it the following way by fixing the addEventListener parameters.

document.getElementById("vid").addEventListener('play', function(){alert("test")});
<video id="vid" src="https://sample-videos./video123/mp4/720/big_buck_bunny_720p_1mb.mp4" type="video/mp4" autoplay></video>

JSFiddle : https://jsfiddle/usrnm/y3gm74wz/1/

本文标签: javascriptWhy doesn39t ltvideogt autoplay trigger quotplayquot eventStack Overflow