Skip to content

Chrome 55 now requires audio to be unlocked by user gestures, fails playing audio on Android in iframe #2913

@AleBles

Description

@AleBles

We've noticed this on a large number of portals where games are generally included with iFrames. We've been having this issue in Phaser 2.6.2, but I think this'll happen in any version available.

Basicly the Chromium team decided that, as in Safari iOS, WebAudio should only be actived trough a user gesture. But the problem is that they do it slightly different compared to Safari. Now this works fine on most platforms. but somehow manages to not work on Chrome - Android.

A comment in the WebAudio repo tells us that when we want to unlock the audio (Phaser.SoundManager#unlock) we need to check the state of the AudioContext and manually resume() the context when it's state is 'suspened'

A quick&dirty workaround for me is adding the following code in my boot state:

if (this.game.device.android && this.game.device.chrome && this.game.device.chromeVersion >= 55) {
    this.game.sound.setTouchLock();
    this.game.input.touch.addTouchLockCallback(function () {
        if (this.noAudio || !this.touchLocked || this._unlockSource !== null) {
            return true;
        }
        if (this.usingWebAudio) {
            // Create empty buffer and play it
            // The SoundManager.update loop captures the state of it and then resets touchLocked to false

            var buffer = this.context.createBuffer(1, 1, 22050);
            this._unlockSource = this.context.createBufferSource();
            this._unlockSource.buffer = buffer;
            this._unlockSource.connect(this.context.destination);

            if (this._unlockSource.start === undefined) {
                this._unlockSource.noteOn(0);
            }
            else {
                this._unlockSource.start(0);
            }

            //Hello Chrome 55!
            if (this._unlockSource.context.state === 'suspended') {
                this._unlockSource.context.resume();
            }
        }

        //  We can remove the event because we've done what we needed (started the unlock sound playing)
        return true;

    }, this.game.sound, true);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions