Ash Ryan Arnwine
Ash Ryan Arnwine

Reputation: 1481

Avoid stopping background audio for howler.js sprite playback on iOS

I'm making a little browser game as a study project, with howler.js 2.0 handling the sound effects.

When I play the game on iOS and I'm listening to music (say, in the music player app), the game sound effects will stop the background audio. Actually, I believe all background audio stops upon load of the game.

Is there any way to prevent this and keep the background audio going?

I'd prefer to avoid audio ducking as well, but could live with that if it's the only option.

The game is here: http://ashryanbeats.com/apps/guessing-game/index.html

Howler is invoked in this way:

var sound = new Howl({
  src: ['sounds/game-sounds.ogg', 'sounds/game-sounds.mp3'],
  sprite: {
    warmSound: [0, 750],
    coldSound: [875, 950],
    winSound: [1930, 2500],
    sameSound: [4518, 850],
    loseSound: [6093, 1700]
  }
});

howler.js 2.0 ref: https://github.com/goldfire/howler.js/tree/2.0

Upvotes: 3

Views: 2000

Answers (1)

Sajith Thomas
Sajith Thomas

Reputation: 1

In ios, if you put your app on background then your tab goes inactive. So add the eventListner to check whether the app is going in background if it does play the audio again.

document.addEventListener('visibilitychange',() => {
    sound.pause();
    sound.play();
});

Upvotes: 0

Related Questions