audio5js icon indicating copy to clipboard operation
audio5js copied to clipboard

Uncaught (in promise) DOMException: play() failed because ...

Open mozai opened this issue 7 years ago • 11 comments

Following up on #88 , how to catch this error?

Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. https://goo.gl/xX8pDD

this.play() returns undefined whether it works or not. Setting throw_errors = true doesn't seem to make a difference. Wrapping it with try { this.play() } catch(err) { } does not catch an error. Using this.on('error', function...., this); doesn't receive the event. I thought of testing this.playing but it (correctly) says the audio5js instance is not playing immediately after calling this.play()

Google documentation says to check the return value of audio.play() for a promise object, and attach a callback to that promise object, but I don't know how to dig a promise return value out of audio5js.play()

To be clear: I don't expect audio5js.play() to start working as before, but I would like to be able to detect its failure so I can notify other components or notify the end-user.

mozai avatar Jun 12 '18 04:06 mozai

Not sure exactly what you're expecting here - If the problem is due to user not interacting with the DOM, then the solution is to trigger play on DOM interaction

zohararad avatar Jun 14 '18 14:06 zohararad

How do I write code that detects audio5js.play() did not succeed, so the code can respond (ie. asking the user to click on a 'start' button, halting animation that is supposed to synch up with the audio, etc.).

I'm not looking for a workaround for the Google Chrome scenario -- that was already answerd in #88 whilch I cited so you'd know I already read it.

mozai avatar Jun 14 '18 15:06 mozai

but for what use-case? is this on a mobile or a desktop browser?

zohararad avatar Jun 14 '18 15:06 zohararad

Either. I call a method on this object -- how does the program sense if it succeeded or failed?

Usually it would be a return value, or catching an exception in a try { } catch { } block, or I'd use a callback hooked into an error event, but as I already said above these don't work. Is there something else I should be doing to sense if the method call succeeded?

mozai avatar Jun 14 '18 15:06 mozai

@mozai what exactly is the source of the issue here? Are you experiencing problems on mobile browsers or desktop?

zohararad avatar Jun 20 '18 08:06 zohararad

I'm having the same issue on Android Chrome. I fixed the error in desktop by provided some initial interaction between the user and the application.

My use case is a monitoring application and requires to set off an audio as an alarm. is there any way an event (not user input) can trigger audio.play() for android chrome?

perryjsteward avatar Oct 04 '18 15:10 perryjsteward

No - As far as I know, on mobile browsers you need a user interaction event to trigger play()

zohararad avatar Oct 08 '18 05:10 zohararad

I have this error when a user manually reloads a page with audio on it. The audio object is created with new Audio(), and was previously successfully playing, but after the user reloads it, while the object exists, it fails to play and I get this error in the console, but there's no way to tell that it hasn't played, in order to catch the error and attempt some recovery....it just breaks. This is on desktop with chrome devtools open and mobile emulation enabled.

davidmaxwaterman avatar Oct 25 '19 13:10 davidmaxwaterman

@davidmaxwaterman I'm no longer actively maintaining this repo. My suggestion is to move away from it to native HTML5 audio which is widely supported by all major browsers

zohararad avatar Oct 27 '19 03:10 zohararad

I should have been clear, but I am using the native html5 audio class. I came to this issue by searching for the error message, which seems to come from the underlying platform.

davidmaxwaterman avatar Oct 28 '19 06:10 davidmaxwaterman

OK. Thanks for clarifying that

zohararad avatar Oct 28 '19 17:10 zohararad