Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过js事件在safari浏览器的iOS 5.x中发出声音通知(例如ajax响应)_Javascript_Jquery_Iphone_Html_Ios - Fatal编程技术网

Javascript 通过js事件在safari浏览器的iOS 5.x中发出声音通知(例如ajax响应)

Javascript 通过js事件在safari浏览器的iOS 5.x中发出声音通知(例如ajax响应),javascript,jquery,iphone,html,ios,Javascript,Jquery,Iphone,Html,Ios,在iOS浏览器(safari)中,事件触发的声音被阻止(在​​单击-一切正常) 是否有人知道一个有效的解决方案——如何避免 示例:ajax聊天信息到达,iphone\ipad发出声音(此时浏览器处于打开状态,应用程序处于活动状态) 感谢您的回答您最好使用具有javascript API的媒体播放器。比如:- 这允许您播放来自javascript的文件,它使用html5或flash,因此Safari无法阻止触发它的事件。阻止这些事件的原因是为了避免加载非用户启动的数据()。这意味着您需要找到一种

在iOS浏览器(safari)中,事件触发的声音被阻止(在​​单击-一切正常) 是否有人知道一个有效的解决方案——如何避免

示例:ajax聊天信息到达,iphone\ipad发出声音(此时浏览器处于打开状态,应用程序处于活动状态)


感谢您的回答

您最好使用具有javascript API的媒体播放器。比如:-


这允许您播放来自javascript的文件,它使用html5或flash,因此Safari无法阻止触发它的事件。

阻止这些事件的原因是为了避免加载非用户启动的数据()。这意味着您需要找到一种方法让用户触发音频数据的加载。让用户在应用程序运行之前与页面交互。初始化可以通过在
document.body
上启动的
“touchstart”
或用户单击以启动应用程序的按钮(例如:让用户单击显示“开始聊天”的按钮)等简单操作触发。在该事件的处理程序中,将音频文件加载到一个变量,并使其可供应用程序的其余部分使用。然后在ajax成功处理程序中播放以下声音:

HTML

首先尝试初始化音频,然后启动ajax循环,反之亦然。在加载音频之前,它将保持静音

这已经在iPad2和iPhone4S上的iOS5.1中进行了测试。我不知道它是否能在其他设备上运行,或者在旧版本的iOS上运行


我不知道还有什么其他方法可以让它工作。

我的解决方案与公认的答案类似,但我会发布它,以防其他人发现它更可读/更可行:

<script>

    //save the sound to play
    var sound=new Audio('sound.mp3');

    //start listening for a mousedown or touchstart event (the later needed for ios)
    $(window).bind('mousedown touchstart',function() {
        //turn down the volume
        sound.volume=0;
        //iOS needs muting set to true 
        sound.muted=true;
        //play the sound on mute
        sound.play();

        console.log("played sound on mute");

        //only need to do this once.  we can unbind these two events when done.
        //rework this if you have mousedown or touchstart bindings for other purposes
        $(window).unbind('mousedown');
        $(window).unbind('touchstart');
    });

    //play the sound after 10 seconds.
    //this is just to test to see if auto-playing works.
    //you'd actually be putting the inside parts of this into some other function
    setTimeout(function(){ 
        //make sure sound is back on
        sound.volume=1;
        sound.muted=false;
        //play the sound
        sound.play();
    },10000);

</script>

//保存声音以便播放
var sound=新音频('sound.mp3');
//开始监听mousedown或touchstart事件(ios需要后者)
$(窗口).bind('mousedown touchtstart',function(){
//把音量调低
声音。音量=0;
//iOS需要将静音设置为true
sound.muted=true;
//在静音模式下播放声音
声音。播放();
console.log(“静音播放声音”);
//只需执行一次。完成后,我们可以解除这两个事件的绑定。
//如果您有用于其他目的的mousedown或touchstart绑定,请重新执行此操作
$(窗口).unbind('mousedown');
$(窗口)。解除绑定('touchstart');
});
//10秒后播放声音。
//这只是为了测试自动播放是否有效。
//实际上,你会把它的内部部分放到其他函数中
setTimeout(函数(){
//确保声音恢复正常
声音。音量=1;
sound.muted=false;
//播放声音
声音。播放();
},10000);

我可以确认这在运行iOS 4.2.1的第二代iPod touch上是否有效。@Dondragmer:很高兴知道。谢谢你的测试。
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()
    }
});
<script>

    //save the sound to play
    var sound=new Audio('sound.mp3');

    //start listening for a mousedown or touchstart event (the later needed for ios)
    $(window).bind('mousedown touchstart',function() {
        //turn down the volume
        sound.volume=0;
        //iOS needs muting set to true 
        sound.muted=true;
        //play the sound on mute
        sound.play();

        console.log("played sound on mute");

        //only need to do this once.  we can unbind these two events when done.
        //rework this if you have mousedown or touchstart bindings for other purposes
        $(window).unbind('mousedown');
        $(window).unbind('touchstart');
    });

    //play the sound after 10 seconds.
    //this is just to test to see if auto-playing works.
    //you'd actually be putting the inside parts of this into some other function
    setTimeout(function(){ 
        //make sure sound is back on
        sound.volume=1;
        sound.muted=false;
        //play the sound
        sound.play();
    },10000);

</script>