question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HTMLVideoElement.play may be undefined

See original GitHub issue

According to MDN, “older browsers may not return a value from play().” I also got an alert about that in production shortly after deploying a change that used play().catch() without checking if the Promise was undefined.

There’s another mention to that in MDN’s “Autoplay guide for media and Web Audio APIs”:

[…] You might use code like this to accomplish the job:

let startPlayPromise = videoElem.play();

if (startPlayPromise !== undefined) {
  startPlayPromise.then(() => {
    // Start whatever you need to do only after playback
    // has begun.
  }).catch(error => {
    if (error.name === "NotAllowedError") {
      showPlayButton(videoElem);
    } else {
      // Handle a load or playback error
    }
  });
}

The first thing we do with the result of play() is make sure it’s not undefined. We check for this because in earlier versions of the HTML specification, play() didn’t return a value. Returning a promise to allow you to determine success or failure of the operation was added more recently. Checking for undefined prevents this code from failing with an error on older versions of web browsers.

I have never contributed to this repo, but I am happy to write a PR if the change makes sense!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
saschanazcommented, Sep 18, 2022

I think it’s safe to close this. 👍

@github-actions close

1reaction
alcarcommented, Jul 6, 2021

@saschanaz Thanks for the clarification and for updating the MDN article! I wasn’t sure how old is too old for still providing static type support, so if this scenario doesn’t pass the criteria we can close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

html5 video tag: video.play() "undefined is not a function"
I've logged extensively. When I try it in my own browsers, the log always say that "theVideoTag" is an "[object HTMLVideoElement]". Great, ...
Read more >
HTMLMediaElement.play() - Web APIs | MDN
The HTMLMediaElement play() method attempts to begin playback of the media. It returns a Promise which is resolved when playback has been ...
Read more >
HTMLMediaElement.play() returns a promise
Automatically playing audio and video on the web is a powerful capability, and one that's subject to different restrictions on different ...
Read more >
HTMLVideoElement.requestVideoFrameCallback()
This means, a 25fps video playing in a browser that paints at 60Hz would fire callbacks at 25Hz; a 120fps video in that...
Read more >
HTMLMediaElement JavaScript API - JavaScripture
May return NaN or Infinity if the duration is not known yet. ... Use pause() and play() methods to pause and play the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found