Sound notification in iOS 5.x in Safari on js event (e.g. ajax response)

In the iOS browser (safari), sounds that are triggered by events are blocked (when clicked, everything is in order) Does anyone know a working solution - how can it be avoided?

Example: the message ajax chat and iphone \ ipad appeared to make a sound (the browser is currently open and the application is active)

thank you for your answers

+6
source share
2 answers

It is best to use a media player with javascript API. Sort of: -

http://www.schillmania.com/projects/soundmanager2/

This allows you to play files with javascript, and it uses either html5 or flash, so Safari will not be able to block the event that triggers it.

-2
source

The reason these events are blocked is to avoid any download of data that is not triggered by the user ( read Apple's explanation here ). This means that you need to figure out a way to get the user to start downloading audio data. Make the user interact with the page before the meat of your application is launched. Initialization can be caused by something as simple as a "touchstart" running on document.body , or by a button that the user clicks to launch the application (example: ask the user to click the button that says β€œstart chat”). In this event handler, load your audio file into a variable and make it available to the rest of your application. Then in your ajax success handler, reproduce the sound:

HTML

 <a id="initbutton">Initialize</a> 

Js

 var sound; $('#initbutton').one('click',function(ev){ sound = new Audio("http://soundjax.com/reddo/61767^ding.mp3"); sound.load(); // load the audio data sound.volume=1; // make sure the volume is all the way up, though this doesn't work on iOS }); $.ajax(...,function(data,status,jqxhr){ //presumably this will be triggered by some other code if(sound){ sound.play() } }); 

See an example here. Try initializing the audio first, then run the ajax loop and vice versa. It will be silent until the sound is loaded.

This has been tested in iOS 5.1 on the iPad 2 and iPhone 4S. I do not know if it will work on other devices or in the old version of iOS.

I do not know any other ways to make it work.

+5
source

Source: https://habr.com/ru/post/906494/


All Articles