Hello everyone!
Problem with playing sounds on iphone devices.
If i use this code (get Phaser example), music is playing everywhere except iphone devices:
var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
pixelArt: true,
scene: {
preload: preload,
create: create
}
};
var game = new Phaser.Game(config);
function preload ()
{
this.load.audio('theme', [
'assets/audio/oedipus_wizball_highscore.ogg',
'assets/audio/oedipus_wizball_highscore.mp3'
]);
this.load.image('wizball', 'assets/sprites/wizball.png');
}
function create ()
{
this.add.image(400, 300, 'wizball').setScale(4);
var music = this.sound.add('theme');
music.play();
}
I read similar topics, and i try use html5 audio ( in config write property - audio: { disableWebAudio: true}), but if i use this way - audio not worked on all mobile devices.
Is there a way to make all sounds play on all devices (PC and mobile, android and iOS)?
I found a answer, must use property:
this.sound.unlock();
all 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');
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)');
}
3 Likes
One thing I noted as I worked with iOS devices is that sometimes the audio started playing a little cut off from the beginning, so I needed to add a little bit of empty sound in the audio file before the sound effect I wanted.
I never knew why this happened.
1 Like