Issues with sound on android devices

the game:

https://chylinski82.github.io/androidCoop/

and all the codes:

https://github.com/chylinski82/androidCoop

Hi, in my game i have a separate SoundScene.js:

class SoundScene extends Phaser.Scene {
    constructor(){
        super({ key: 'SoundScene' })
    }

    preload(){
        this.load.audio('theme2', './audio/theme2.mp3');
    }

    create() {
        this.input.once('pointerdown', _ => {
            // UNLOCK audio, doesn't work immediately on iphone
            this.sound.unlock();
            gameState.music = this.sound.add('theme2', {loop: true});
            
            // if unlock worked, just play the sound
            if(!this.sound.locked){
                gameState.music.play();
            } 
            else {  // IF Not wait on unlock event 
                this.sound.once(Phaser.Sound.Events.UNLOCKED, () => {
                    gameState.music.play();
                })
            }
        });
    }

}

then in main game.js in config i have:

audio: {
        disableWebAudio: true
    }

because apparently there were issues on some iOS devices.

My sound file is .mp3, and it’s size is 1.1MB, apparently works fine on Android devices that soemone else tested, but not on my Galaxy S9 (it does work fine on my kids’ Galaxy TabA), then if i change the audio file to a larger (4MB) .mp3 or to a .ogg file it doesn’t work anymore.

Highly appreciate help.

Hello, Krzysztof!
Thank you - in your question i found answer for my question:)
I can’t undestand how use sounds on all devices, and all that is needed - add
this.sound.unlock();
and sounds worked after touch on the screen.
Thank you very much!

Regarding your question - i try your code in main js file, not in separate scene. I’m try use .mp3 file with size 13.5 MB, and sound playing on all my devices( iphone 11, huawei (old phone), xiaomi (old phone).
Work code:

var config = {
	type: Phaser.AUTO,
	width: 800,
	height: 600,
	audio: {
		disableWebAudio: true
	},
	scene: {
		preload: preload,
		create: create
	}
};

var text;
var game = new Phaser.Game(config);

function preload() {
	text = this.add.text(10, 10, 'Loading audio ...', { font: '16px Courier', fill: '#00ff00' });

	this.load.audio('dafunk', [
		'assets/audio/Dafunk - Hardcore Power (We Believe In Goa - Remix).ogg',
		'assets/audio/Dafunk - Hardcore Power (We Believe In Goa - Remix).mp3',
		'assets/audio/Dafunk - Hardcore Power (We Believe In Goa - Remix).m4a'
	]);
}

function create() {
	this.sound.pauseOnBlur = false;

	this.input.on('pointerdown', function () {
		this.sound.unlock();
		var music = this.sound.add('dafunk');
		console.log(music);
		if (!this.sound.locked) {
			music.play();
		}
		else {  // IF Not wait on unlock event 
			this.sound.once(Phaser.Sound.Events.UNLOCKED, () => {
				music.play();
			})
		}
	}, this)

	text.setText('Playing Dafunk - Hardcore Power (We Believe In Goa - Remix)');
}

Maybe some problem in Galaxy TabA?

1 Like

hi Daniil, thanks for your reply, it was actually someone on stack overflow who came up with sound.unlock and they told me that my game worked on all devices, now you’re saying the same, so I’m just gonna assume that something’s wrong with my devices.

1 Like