Jump to content

Help playing sound via SoundJS with Pixi on iOS devices


neonwarge04
 Share

Recommended Posts

Hi, 

 

It seems there are several issues going on when playing sounds in iOS devices. I need a way to play a background music.  I am using SoundJS as my sound engine. What SoundJS suggest from this docu is to trigger playing sounds through touch/click events. The documentation state the reason for this and I find it very reasonable. Given my situation, I need to play the background music the moment the game starts so I am scratching my head how should I go around this.

 

Here is my code running the background music while I still preserving the structure:
 

 
define(['src/Runner2', 'src/World', 'soundjs', "core/utils/Button", "core/utils/CircularFader", 'core/utils/Dimmer' , "src/GameUI"] , function(Runner2, World, SoundJS, Button, CircularFader, Dimmer, GameUI){  console.log("MainGameScene loaded.");  return function (renderer , screenSize)  {    ...    var bgm = SoundJS.play("bgm1" , {loop: -1});        ...    function initGameMode(runner)    {      ...      if(bgm !== null)        bgm.stop();      bgm = SoundJS.play("bgm1", {loop: -1});            ...    }    function updateGame()    {      ...      if(!mRunner.isDead())      {         ...      }      else      {        // I stop music on game over        if(bgm !== null)        {          bgm.stop();          bgm = null;        }      }    }    ...    return MainGameScene;  }});

The line where I immediately play the sound stops on browsers on iOS devices including iPhones and Ipads. Have you guys figured out how this will work? Or a generic solution that will work all across browser and OSes? I very much prefer to stick to JavaScript solution, but this is just my thought. Thank you very much

 

 

Link to comment
Share on other sites

Remember, only the first sound need be triggered by a touch event explicitly.  All following sounds can be called from code in the main loop, just as you'd expect / want.

 

The approach I take is (for iOS devices) to load the game assets with a progress bar, and then hold and wait for a touch event.  At that point display a message to the user to encourage them to touch (e.g. "tap to continue" or "choose X vs Y").  Then on that first touch event play a sound (I play a silent sound, of duration >1s) and move the game onwards.  From there on, sounds can be started on iOS just the same as for Android and desktop.

 

That same "first-touch-event" strategy can also be used for other similarly restrictive APIs, like fullScreen and lockOrientation (note, last time I checked, neither work on iOS).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...