JavaScript API playSound() issue

Q&A about the latest versions
Post Reply
ffurger
Posts: 80
Joined: Mon Mar 24, 2014 11:45 am

JavaScript API playSound() issue

Post by ffurger » Thu Jan 23, 2020 3:53 pm

Most browsers these days prevent audio from playing automatically.
This means that if a page tries to start playing without user intervention the browser will throw an error.
In theory this is not a problem, because the developer can catch the error and take an appropriate measure.

Unfortunately, the pano.playSound() doesn't include a way to catch the error, which in this case is this:

Code: Select all

Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.
In Firefox and Chrome at least, this has the unfortunate consequence that the player start playing as soon as the user clicks on any button, completely unrelated to the actual play button.

Is there a way to get around this issue ?

User avatar
Hopki
Gnome
Posts: 9811
Joined: Thu Jan 10, 2008 3:16 pm
Location: Layer de la Haye, Essex UK
Contact:

Re: JavaScript API playSound() issue

Post by Hopki » Thu Jan 23, 2020 4:58 pm

Hi,
If a sound is set to play, then on the first touch or click it will play.
If you want the sound to start playing with a button click then set the sounds Loopto -1.
This will stop the sound from playing until an action is used to play it.
Regards,
Hopki

ffurger
Posts: 80
Joined: Mon Mar 24, 2014 11:45 am

Re: JavaScript API playSound() issue

Post by ffurger » Fri Jan 24, 2020 9:18 am

well, what I mean is that for some panos I'd like the sound to start playing immediately upon page loading,
without user intervention.

Some users don't like that and prevent audio from start playing automatically.
That's why when this is the case the JavaScript engine throws an error.
Normally, in JavaScript I could catch the error and make sure that it does't cause any trouble,
but in this case I haven't been able to figure out how to do that.

-Franco

User avatar
Hopki
Gnome
Posts: 9811
Joined: Thu Jan 10, 2008 3:16 pm
Location: Layer de la Haye, Essex UK
Contact:

Re: JavaScript API playSound() issue

Post by Hopki » Mon Jan 27, 2020 3:19 pm

Hi Franco,
sound to start playing immediately upon page loading, without user intervention.
The browsers will not allow this, it needs user interaction.
This said Chrome has learning ability, I seem to remember if you go to the same website 7 or 8 times, and each time clicking the play button and listen to the audio for over 7 seconds each time, Chrome will then allow autoplay as it now knows you want to hear the sound.

Safari and Firefox will not allow autoplay and will require user interaction. You can not use JavaScript to get around this, even if you could the browser produces would find a way around this at some point and brake it.
Regards,
Hopki

ffurger
Posts: 80
Joined: Mon Mar 24, 2014 11:45 am

Re: JavaScript API playSound() issue

Post by ffurger » Thu Jan 30, 2020 5:15 pm

Hopki,

Sorry for this late reply, but I have been doing a lot of testing around this issue.
Here is what I found out. But first, a few words about my plan:

- Yes, most browsers by default don't allow autoplay, and that's fine. I have no intention to somehow bypass
this setting.
- I have some panos where it would make sense to autoplay a background sound. These panos are set with a loop value of 0;
- For these panos I use the JS API to start playing immediately.
- Now, if the browser requires user intervention, i.e. a click by the user, the JS engine will - correctly - throw an error,
so need to catch it before it causes the entire script to fail.
- There is a way to deal with this situation, it goes like this:

Code: Select all

window.addEventListener('unhandledrejection', function(event) {
  console.log(event.promise); // [object Promise] - the promise that generated the error
  console.log(event.reason); // Error: Whoops! - the unhandled error object
                            // and now do something ...
});
- This code works fine. My script recovers from the error and continue doing its job.

Now it gets interesting:
If a user clicks anywhere on the page, not just on the sound player play button,
the background sound starts playing. As far as I am concerned this is not supposed to happen
and it's a serious issue, as it screws up the entire sound logic.

Best,
Franco

User avatar
Hopki
Gnome
Posts: 9811
Joined: Thu Jan 10, 2008 3:16 pm
Location: Layer de la Haye, Essex UK
Contact:

Re: JavaScript API playSound() issue

Post by Hopki » Thu Feb 06, 2020 6:52 pm

Hi Franco,
If a sound has a loop of 0 or above, then on "first touch" it will start playing sound.
With regards to the errors, if you set the sound to loops -1 then no sound will try and play.
But I am not sure what information you require?
Regards,
Hopki

ffurger
Posts: 80
Joined: Mon Mar 24, 2014 11:45 am

Re: JavaScript API playSound() issue

Post by ffurger » Wed Feb 19, 2020 3:14 pm

Hi Hopki,

sorry for this late reply. My understanding is that for most browsers,
the user can decide to allow autoplay when a page loads.
By default, autoplay is turned off in the browser settings, but it can be changed.

In the case of my application, I wanted to autoplay (NOT merely play on touch)
some background sound. That's easy enough to do.

If the browser doesn't allow that to happen, javascript will
throw an error. So JS will tell me (in the console) that something went wrong with
executing playSound().

Now, if I don't do anything in response to this error the sound script in my application
will be disrupted. Normally that's easy enough to take care of. But in this case I don't know
how to deal with this situation because I have no idea what's going on in the playSound() API.

I guess what I am saying is that it would be helpful if one of your developers could let me
know how to deal with this situation.

Best,
Franco

christoph
Gnome
Posts: 95
Joined: Mon Aug 20, 2012 3:01 pm

Re: JavaScript API playSound() issue

Post by christoph » Fri Feb 21, 2020 8:49 am

Hi Franco!
The player function playSound() basically just calls the play() function of the HTML AUDIO element (https://www.w3schools.com/jsref/met_audio_play.asp)

Regards,
Christoph

ffurger
Posts: 80
Joined: Mon Mar 24, 2014 11:45 am

Re: JavaScript API playSound() issue

Post by ffurger » Fri Feb 21, 2020 11:44 am

Thank you Christoph,

I kind of suspected that. Now, could you tell me how to handle this Promise Rejection coming from the soundPlayer:

Code: Select all

Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.
Thanks,
Franco

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests