neonwarge04 Posted November 12, 2015 Share Posted November 12, 2015 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 Quote Link to comment Share on other sites More sharing options...
b10b Posted November 12, 2015 Share Posted November 12, 2015 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). Quote Link to comment Share on other sites More sharing options...
neonwarge04 Posted November 13, 2015 Author Share Posted November 13, 2015 I have a question, is this first-touch strategy needs to be implemented through tags? Or I just used any kind of events that listens from touch/click events? Quote Link to comment Share on other sites More sharing options...
b10b Posted November 13, 2015 Share Posted November 13, 2015 Any sound started by a user-initiated-event should suffice. However I recall there is a bug in iOS9 that means "toucheend" is the best choice. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.