简体   繁体   English

Javascript Safari / IOS 15 - 14 上的音频上下文不再工作

[英]Javascript audio context on Safari / IOS 15 - 14 dont work anymore

Recent, IOS/Safari upgrade seems to broke web audio api...最近IOS/Safari升级好像断了web音频api...

Look here simple code that work before on IOS and safari but since upgrade thats dont work anymore..by the way it work on firefox, edge, chrome.看这里简单的代码,以前在 IOS 和 safari 上工作,但自从升级后就不再工作了……顺便说一句,它在 firefox、edge、chrome 上工作。

https://www.mylooper.com/debug.html https://www.mylooper.com/debug.html

    var url  = 'https://www.mylooper.com/static/btf_10_loop1.aac';
    var context = new AudioContext();
    var source = context.createBufferSource();
    
    //connect it to the destination so you can hear it.
    source.connect(context.destination);
    /* --- load buffer ---  */
    var request = new XMLHttpRequest();
    //open the request
    request.open('GET', url, true); 
    //webaudio paramaters
    request.responseType = 'arraybuffer';
    request.onload = function() {
        context.decodeAudioData(request.response, function(response) {
            /* --- play the sound AFTER the buffer loaded --- */
            //set the buffer to the response we just received.
            source.buffer = response;
            source.start(0);
            source.loop = true;
        }, function () { console.error('The request failed.'); } );
    }
    //Now that the request has been defined, actually make the request. (send it)
    request.send();
</script>

Lot of library is impacted like howler js, tuna js... but i dont know if this "bug"/"feature" may be fix one day...很多库都像 howler js、tuna js 一样受到影响……但我不知道这个“错误”/“功能”是否有一天会被修复……

By the way, for now how do a perfect audio loop with JS now on Safari / IOS?顺便说一下,现在如何在 Safari / IOS 上使用 JS 进行完美的音频循环?

Thanks a lot, this thing driving me crazy...非常感谢,这件事让我发疯......

What do you mean by "broken"? “破”是什么意思?
If it means that audio does not start playing with this warning, you should call source.start(0) after user's gesture.如果这意味着音频没有开始播放这个警告,你应该在用户的手势之后调用source.start(0)

The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. 

For Example, call request.send() in some button's onclick function.例如,在某个按钮的 onclick function 中调用request.send()
Or, wrap all your code in some button's onclick function.或者,将所有代码包装在某个按钮的 onclick function 中。

const button = document.getElementbyId("foo");
button.oncklick = () => {
    var url  = 'https://www.mylooper.com/static/btf_10_loop1.aac';
    var context = new AudioContext();
    var source = context.createBufferSource();
    
    //connect it to the destination so you can hear it.
    source.connect(context.destination);
    /* --- load buffer ---  */
    var request = new XMLHttpRequest();
    //open the request
    request.open('GET', url, true); 
    //webaudio paramaters
    request.responseType = 'arraybuffer';
    request.onload = function() {
        context.decodeAudioData(request.response, function(response) {
            /* --- play the sound AFTER the buffer loaded --- */
            //set the buffer to the response we just received.
            source.buffer = response;
            source.start(0);
            source.loop = true;
        }, function () { console.error('The request failed.'); } );
    }
    //Now that the request has been defined, actually make the request. (send it)
    request.send();
}

This is feature described here.这是此处描述的功能。 https://developer.chrome.com/blog/autoplay/#webaudio https://developer.chrome.com/blog/autoplay/#webaudio
So it will not be fixed.所以不会修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM