In iOS browser (safari) sounds that are triggered by events are blocked (on click - everything is OK) Does anyone know a working solution - how it can be avoided?
在iOS浏览器(safari)中,由事件触发的声音会被屏蔽(点击时,一切正常),有人知道一个可行的解决方案吗?
Example: ajax chat message arrived and iphone \ ipad make a sound (the browser is open at the moment and active app)
示例:ajax聊天消息到达,iphone \ ipad发出声音(此时浏览器打开,活动应用程序)
thank you for your answers
谢谢你的回答
2 个解决方案
#1
-2
Your best bet would be to use a media player that has a javascript API. Something like:-
最好的选择是使用一个具有javascript API的媒体播放器。类似的:
http://www.schillmania.com/projects/soundmanager2/
http://www.schillmania.com/projects/soundmanager2/
This allows you to play files from javascript and it uses either html5 or flash so Safari wouldn't be able to block an event triggering it.
这允许您从javascript播放文件,它使用html5或flash,因此Safari不能阻止触发事件。
#2
5
The reason these events are blocked is to avoid any loading of data that is not user-initiated (read Apple's explanation here). That means you need to figure out a way to make the user trigger loading of the audio data. Make the user interact with the page before the meat of your app runs. The initialization could be triggered by something as simple as a "touchstart"
fired on document.body
, or a button that the user clicks to launch the app (example: have the user click a button that says "start chatting"). In that event's handler, load your audio file to a variable and make it available to the rest of your application. Then in your ajax success handler, play the sound:
这些事件被阻塞的原因是为了避免加载非用户发起的数据(请参阅苹果的解释)。这意味着您需要找到一种方法来让用户触发音频数据的加载。让用户在程序运行之前与页面进行交互。初始化可以由一些简单的操作触发,比如在文档上启动“touchstart”。或者用户点击的按钮来启动应用程序(例如:让用户点击一个按钮,上面写着“开始聊天”)。在该事件的处理程序中,将音频文件加载到一个变量中,并使其对应用程序的其余部分可用。然后在ajax成功处理程序中,播放声音:
HTML
HTML
<a id="initbutton">Initialize</a>
JS
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 the example here. Try initializing audio first, then start the ajax loop and vice versa. It will be silent until the audio is loaded.
在这里看到的例子。首先尝试初始化音频,然后启动ajax循环,反之亦然。在音频加载之前,它将保持沉默。
This has been tested in iOS 5.1 on iPad 2 and iPhone 4S. I don't know if it will work on other devices or in older version of iOS.
iOS 5.1已经在iPad 2和iPhone 4S上进行了测试。我不知道它是否适用于其他设备或iOS的旧版本。
I don't know of any other ways to make it work.
我不知道还有什么别的方法可以让它工作。
#1
-2
Your best bet would be to use a media player that has a javascript API. Something like:-
最好的选择是使用一个具有javascript API的媒体播放器。类似的:
http://www.schillmania.com/projects/soundmanager2/
http://www.schillmania.com/projects/soundmanager2/
This allows you to play files from javascript and it uses either html5 or flash so Safari wouldn't be able to block an event triggering it.
这允许您从javascript播放文件,它使用html5或flash,因此Safari不能阻止触发事件。
#2
5
The reason these events are blocked is to avoid any loading of data that is not user-initiated (read Apple's explanation here). That means you need to figure out a way to make the user trigger loading of the audio data. Make the user interact with the page before the meat of your app runs. The initialization could be triggered by something as simple as a "touchstart"
fired on document.body
, or a button that the user clicks to launch the app (example: have the user click a button that says "start chatting"). In that event's handler, load your audio file to a variable and make it available to the rest of your application. Then in your ajax success handler, play the sound:
这些事件被阻塞的原因是为了避免加载非用户发起的数据(请参阅苹果的解释)。这意味着您需要找到一种方法来让用户触发音频数据的加载。让用户在程序运行之前与页面进行交互。初始化可以由一些简单的操作触发,比如在文档上启动“touchstart”。或者用户点击的按钮来启动应用程序(例如:让用户点击一个按钮,上面写着“开始聊天”)。在该事件的处理程序中,将音频文件加载到一个变量中,并使其对应用程序的其余部分可用。然后在ajax成功处理程序中,播放声音:
HTML
HTML
<a id="initbutton">Initialize</a>
JS
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 the example here. Try initializing audio first, then start the ajax loop and vice versa. It will be silent until the audio is loaded.
在这里看到的例子。首先尝试初始化音频,然后启动ajax循环,反之亦然。在音频加载之前,它将保持沉默。
This has been tested in iOS 5.1 on iPad 2 and iPhone 4S. I don't know if it will work on other devices or in older version of iOS.
iOS 5.1已经在iPad 2和iPhone 4S上进行了测试。我不知道它是否适用于其他设备或iOS的旧版本。
I don't know of any other ways to make it work.
我不知道还有什么别的方法可以让它工作。