WebKit Audio distorts on iOS 6 (iPhone 5) first time after power cycling

前端 未结 3 831
再見小時候
再見小時候 2020-12-14 11:56

I\'ve been struggling with an elusive audio distortion bug using webkitAudioContext in HTML5 under iOS 6. It can happen in other circumstances, but the only way I can get 1

3条回答
  •  失恋的感觉
    2020-12-14 12:09

    I have been having similar problems, even on iOS 9.2.

    Even without a tag, playback is distorted when first playing audio on the page after cold boot. After a reload, it works fine.

    The initial AudioContext seems to default to 48 kHz, which is where distortion is happening (even with our audio at 48 kHz sample rate). When playback is working properly, the AudioContext has a sample rate of 44.1 kHz.

    I did find a workaround: it is possible to re-create the AudioContext after playing an initial sound. The newly-created AudioContext seems to have the correct sample rate. To do this:

    // 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();
    }
    

提交回复
热议问题