Phaser: Distorted and buzzes audio on ios (only first load)

Created on 3 Mar 2016  路  2Comments  路  Source: photonstorm/phaser

Here it is the test case:
clear cache from settings->safari than open page.

This only happen on ios (ios 9.2.1 iphone 5/6/6s - not tested other version) + safari when no content has been cached.
If you reload the page the sound is correctly played.
No problem with chrome on the same device.

In order to reproduce the bug once the page has been loaded, you have to clear the cache again.

-- the sound in the example has been downloaded from http://phaser.io/examples/v2/audio/loop example

testPhaserBuzz.zip

馃悰 Browser Bug

Most helpful comment

@lucap86 I ran into a similar issue using Phaser on iOS. It's more of an issue with Web Audio rather than Phaser. See this comment in SoundManager.js

Mobile warning: There are some mobile devices (certain iPad 2 and iPad Mini revisions) that cannot play 48000 Hz audio.

In my project I noticed that Web Audio on iOS would sometimes initialize with a sample rate of 48000 Hz. The solution I can up with was to reinitialize audioContext before starting Phaser. (Note: Phaser will look for window['PhaserGlobal']['audioContext'] for audioContext before creating it's own).

function initAudio() {
    // Sometimes audioContext.sampleRate is 48000 and not compatible with
    // unspecified devices.  Reinitializing seems to do the trick.
    window['PhaserGlobal'] = { audioContext: new window['webkitAudioContext']() };
    window['PhaserGlobal']['audioContext'].close();
    window['PhaserGlobal'] = { audioContext: new window['webkitAudioContext']() };
    console.log('Reinitialized audio sample rate at ', window['PhaserGlobal']['audioContext'].sampleRate);
  }

If you add in a console.log of the audioContext.sampleRate, I have a feeling you'll see 48000 as the value when you hear the distortion. Unfortunately there is no way to tell Web Audio what sample rate to use, so reinitializing seems to be the only way around it. Hope this will work for you.

All 2 comments

@lucap86 I ran into a similar issue using Phaser on iOS. It's more of an issue with Web Audio rather than Phaser. See this comment in SoundManager.js

Mobile warning: There are some mobile devices (certain iPad 2 and iPad Mini revisions) that cannot play 48000 Hz audio.

In my project I noticed that Web Audio on iOS would sometimes initialize with a sample rate of 48000 Hz. The solution I can up with was to reinitialize audioContext before starting Phaser. (Note: Phaser will look for window['PhaserGlobal']['audioContext'] for audioContext before creating it's own).

function initAudio() {
    // Sometimes audioContext.sampleRate is 48000 and not compatible with
    // unspecified devices.  Reinitializing seems to do the trick.
    window['PhaserGlobal'] = { audioContext: new window['webkitAudioContext']() };
    window['PhaserGlobal']['audioContext'].close();
    window['PhaserGlobal'] = { audioContext: new window['webkitAudioContext']() };
    console.log('Reinitialized audio sample rate at ', window['PhaserGlobal']['audioContext'].sampleRate);
  }

If you add in a console.log of the audioContext.sampleRate, I have a feeling you'll see 48000 as the value when you hear the distortion. Unfortunately there is no way to tell Web Audio what sample rate to use, so reinitializing seems to be the only way around it. Hope this will work for you.

That's work! tank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JarLowrey picture JarLowrey  路  4Comments

Secretmapper picture Secretmapper  路  3Comments

JoeBerkley picture JoeBerkley  路  3Comments

BigZaphod picture BigZaphod  路  4Comments

samme picture samme  路  3Comments