WebKit Audio在电源循环后第一次在iOS 6(iPhone 5)上扭曲

在iOS 6下,我一直在使用 HTML5中的webkitAudioContext来解决这个难以捉摸的音频失真错误.在其他情况下,可能会发生这种情况,但是我可以通过电源循环访问我的页面,只有这样才能获得100%的重现.看来,如果您访问任何有音频功能的页面访问这一个之前,问题不会发生.

该失真只发生在webkitAudioContext.decodeAudioData()生成的音频上,然后通过webkitAudioContext.createBufferSource()播放. webkitAudioContext.createMediaElementSource()的音频播放不会变形.

我缺少一些初始化步骤吗?以下是我向Apple提交的代码和HTML,作为错误报告(但没有收到回复):

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
        var buffer = null;
        var context = null;
        var voice = null;

        function load_music(file) {
            context = new webkitAudioContext();
            voice = context.createBufferSource();
            var request = new XMLHttpRequest();
            request.onload = function() {
                context.decodeAudioData(request.response,function(result) {
                    buffer = result;
                    document.getElementById("start").value = "Start";
                });
            };
            var base = window.location.pathname;
            base = base.substring(0,base.lastIndexOf("/") + 1);
            request.open("GET",base + file,true);
            request.responseType = "arraybuffer";
            request.send(null);
        }

        function start_music() {
            if (!buffer) {
                alert("Not ready yet");
                return;
            }
            voice.buffer = buffer;
            voice.connect(context.destination);
            voice.noteOn(0);

            document.getElementById("compare").style.display = "block";
        }
        </script>       
    </head>

    <body onload="load_music('music.mp3')">
        <p>This is a simple demo page to reproduce a <strong>webkitAudio</strong>
        problem occurring in Safari on iOS 6.1.4. This is a stripped down demo
        of a phenomenon discovered in our HTML5 game under development,using different assets.</p>

        <p><u>Steps to reproduce:</u></p>

        <ol>
            <li>Power cycle <strong>iPhone 5 with iOS 6.1.4</strong>.</li>
            <li>Launch Safari immediately,and visit this page.</li>
            <li>Wait for &quot;Loading...&quot; below to change to
                &quot;Start&quot;.</li>
            <li>Tap &quot;Start&quot;.</li>
        </ol>

        <p><u>Issue:</u></p>

        <p>Audio will be excessively distorted and play at wrong pitch. If 
        another audio-enabled web site is visited before this one,or this 
        site is reloaded,the audio will fix. The distortion only happens on 
        the first visit after cold boot.  <strong>To reproduce the bug,it is 
        critical to power cycle before testing.</strong></p>

        <p>This bug has not been observed on any other iOS version (e.g. does
        not occur on iPad Mini or iPod 5 using iOS 6.1.3).</p>

        <input id="start" type="button" value="Loading..." onmousedown="start_music()" />

        <span id="compare" style="display:none;"><p><a href="music.mp3">Direct link</a> to audio file,for
        comparison.</p></span>
    </body>
</html>

注意:身体文本表明这仅发生在iOS 6.1.4上,但我的意思是说,问题只发生在这种情况下的电源循环.在6.1.3的iPad Mini下,我也遇到过这个问题,但是没有电力循环.

编辑:我尝试过的一些事情…推迟缓冲源的创建没有什么区别.使用不同的代码转换器来生成它播放的.mp3文件没有什么区别.播放一次性的沉默作为第一个声音没有区别,因为每个decodeAudioData声音的失真持续到页面重新加载.如果createMediaElementSource和createBufferSource源在同一页面中混合,则只有createBufferSource音频(使用decodeAudioData)才会失真.当我在失败案例和非故障案例中检查request.response.byteLength时,它们是一样的,建议XMLHttpRequest不返回不正确的数据,尽管我认为数据损坏会损坏MP3头并渲染文件无法播放.

故障条件与非故障条件之间存在一个可观察的差异.只读值context.sampleRate在失败状态下将为48000,在非故障状态下为44​​100. (但是失败状态听起来比非故障状态的音调更低).唯一发生在我身上的是一个黑客,其中如果在浏览器上检测到48000,应该是报告44100,而通过JavaScript刷新页面,但这是严重的userAgent筛选并不是非常未来的证明,这让我很紧张.

解决方法

我已经有类似的问题,即使在iOS 9.2.

即使没有< video>标签后,首次在冷启动后在页面上播放音频时播放失真.重新加载后,它工作正常.

最初的AudioContext似乎默认为48 kHz,这是发生失真的地方(即使是采用48 kHz采样率的音频).当播放正常工作时,AudioContext的采样率为44.1 kHz.

我找到了一个

解决方法

:可以在播放初始声音后重新创建AudioContext.新创建的AudioContext似乎具有正确的采样率.去做这个:

// inside the click/touch handler
var playInitSound = function playInitSound() {
    var source = context.createBufferSource();
    source.buffer = context.createBuffer(1,1,48000);
    source.connect(context.destination);
    if (source.start) {
        source.start(0);
    } else {
        source.noteOn(0);
    }
};

playInit();
if (context.sampleRate === 48000) {
    context = new AudioContext();
    playInit();
}

以上是来客网为你收集整理的WebKit Audio在电源循环后第一次在iOS 6(iPhone 5)上扭曲全部内容,希望文章能够帮你解决WebKit Audio在电源循环后第一次在iOS 6(iPhone 5)上扭曲所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。