11var Class = require ( '../../utils/Class' ) ;
22var BaseSoundManager = require ( '../BaseSoundManager' ) ;
33var HTML5AudioSound = require ( './HTML5AudioSound' ) ;
4+
45/*!
56 * @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
67 */
78var HTML5AudioSoundManager = new Class ( {
89 Extends : BaseSoundManager ,
10+
911 /**
1012 * HTML5 Audio implementation of the sound manager.
1113 *
1214 * @class Phaser.Sound.HTML5AudioSoundManager
1315 * @constructor
1416 * @param {Phaser.Game } game - Reference to the current game instance.
1517 */
16- initialize : function HTML5AudioSoundManager ( game ) {
18+ initialize : function HTML5AudioSoundManager ( game )
19+ {
1720 /**
1821 * Flag indicating whether if there are no idle instances of HTML5 Audio tag,
1922 * for any particular sound, if one of the used tags should be hijacked and used
@@ -24,6 +27,7 @@ var HTML5AudioSoundManager = new Class({
2427 * @default true
2528 */
2629 this . override = true ;
30+
2731 /**
2832 * Value representing time difference, in seconds, between calling
2933 * play method on an audio tag and when it actually starts playing.
@@ -36,6 +40,7 @@ var HTML5AudioSoundManager = new Class({
3640 * @default 0.1
3741 */
3842 this . audioPlayDelay = 0.1 ;
43+
3944 /**
4045 * A value by which we should offset the loop end marker of the
4146 * looping sound to compensate for lag, caused by changing audio
@@ -48,6 +53,7 @@ var HTML5AudioSoundManager = new Class({
4853 * @default 0.05
4954 */
5055 this . loopEndOffset = 0.05 ;
56+
5157 /**
5258 * An array for keeping track of all the sounds
5359 * that were paused when game lost focus.
@@ -58,6 +64,7 @@ var HTML5AudioSoundManager = new Class({
5864 */
5965 this . onBlurPausedSounds = [ ] ;
6066 this . locked = 'ontouchstart' in window ;
67+
6168 /**
6269 * A queue of all actions performed on sound objects while audio was locked.
6370 * Once the audio gets unlocked, after an explicit user interaction,
@@ -67,6 +74,7 @@ var HTML5AudioSoundManager = new Class({
6774 * @property {{ sound: Phaser.Sound.HTML5AudioSound, name: string, value?: * }[] } lockedActionsQueue
6875 */
6976 this . lockedActionsQueue = this . locked ? [ ] : null ;
77+
7078 /**
7179 * Property that actually holds the value of global mute
7280 * for HTML5 Audio sound manager implementation.
@@ -76,6 +84,7 @@ var HTML5AudioSoundManager = new Class({
7684 * @default false
7785 */
7886 this . _mute = false ;
87+
7988 /**
8089 * Property that actually holds the value of global volume
8190 * for HTML5 Audio sound manager implementation.
@@ -87,6 +96,7 @@ var HTML5AudioSoundManager = new Class({
8796 this . _volume = 1 ;
8897 BaseSoundManager . call ( this , game ) ;
8998 } ,
99+
90100 /**
91101 * Adds a new sound into the sound manager.
92102 *
@@ -95,57 +105,72 @@ var HTML5AudioSoundManager = new Class({
95105 * @param {ISoundConfig } [config] - An optional config object containing default sound settings.
96106 * @returns {Phaser.Sound.HTML5AudioSound } The new sound instance.
97107 */
98- add : function ( key , config ) {
108+ add : function ( key , config )
109+ {
99110 var sound = new HTML5AudioSound ( this , key , config ) ;
100111 this . sounds . push ( sound ) ;
101112 return sound ;
102113 } ,
114+
103115 /**
104116 * Unlocks HTML5 Audio loading and playback on mobile
105117 * devices on the initial explicit user interaction.
106118 *
107119 * @private
108120 * @method Phaser.Sound.HTML5AudioSoundManager#unlock
109121 */
110- unlock : function ( ) {
122+ unlock : function ( )
123+ {
111124 var _this = this ;
112125 var moved = false ;
113- var detectMove = function ( ) {
126+ var detectMove = function ( )
127+ {
114128 moved = true ;
115129 } ;
116- var unlock = function ( ) {
117- if ( moved ) {
130+ var unlock = function ( )
131+ {
132+ if ( moved )
133+ {
118134 moved = false ;
119135 return ;
120136 }
121137 document . body . removeEventListener ( 'touchmove' , detectMove ) ;
122138 document . body . removeEventListener ( 'touchend' , unlock ) ;
123139 var allTags = [ ] ;
124- _this . game . cache . audio . entries . each ( function ( key , tags ) {
125- for ( var i = 0 ; i < tags . length ; i ++ ) {
140+ _this . game . cache . audio . entries . each ( function ( key , tags )
141+ {
142+ for ( var i = 0 ; i < tags . length ; i ++ )
143+ {
126144 allTags . push ( tags [ i ] ) ;
127145 }
128146 return true ;
129147 } ) ;
130148 var lastTag = allTags [ allTags . length - 1 ] ;
131- lastTag . oncanplaythrough = function ( ) {
149+ lastTag . oncanplaythrough = function ( )
150+ {
132151 lastTag . oncanplaythrough = null ;
133152 _this . unlocked = true ;
134153 } ;
135- allTags . forEach ( function ( tag ) {
154+ allTags . forEach ( function ( tag )
155+ {
136156 tag . load ( ) ;
137157 } ) ;
138158 } ;
139- this . once ( 'unlocked' , function ( ) {
140- _this . forEachActiveSound ( function ( sound ) {
159+ this . once ( 'unlocked' , function ( )
160+ {
161+ _this . forEachActiveSound ( function ( sound )
162+ {
141163 sound . duration = sound . tags [ 0 ] . duration ;
142164 sound . totalDuration = sound . tags [ 0 ] . duration ;
143165 } ) ;
144- _this . lockedActionsQueue . forEach ( function ( lockedAction ) {
145- if ( lockedAction . sound [ lockedAction . prop ] . apply ) {
166+ _this . lockedActionsQueue . forEach ( function ( lockedAction )
167+ {
168+ if ( lockedAction . sound [ lockedAction . prop ] . apply )
169+ {
146170 lockedAction . sound [ lockedAction . prop ] . apply ( lockedAction . sound , lockedAction . value || [ ] ) ;
147171 }
148- else {
172+ else
173+ {
149174 lockedAction . sound [ lockedAction . prop ] = lockedAction . value ;
150175 }
151176 } ) ;
@@ -155,45 +180,55 @@ var HTML5AudioSoundManager = new Class({
155180 document . body . addEventListener ( 'touchmove' , detectMove , false ) ;
156181 document . body . addEventListener ( 'touchend' , unlock , false ) ;
157182 } ,
183+
158184 /**
159185 * Method used internally for pausing sound manager if
160186 * Phaser.Sound.HTML5AudioSoundManager#pauseOnBlur is set to true.
161187 *
162188 * @protected
163189 * @method Phaser.Sound.HTML5AudioSoundManager#onBlur
164190 */
165- onBlur : function ( ) {
166- this . forEachActiveSound ( function ( sound ) {
167- if ( sound . isPlaying ) {
191+ onBlur : function ( )
192+ {
193+ this . forEachActiveSound ( function ( sound )
194+ {
195+ if ( sound . isPlaying )
196+ {
168197 this . onBlurPausedSounds . push ( sound ) ;
169198 sound . onBlur ( ) ;
170199 }
171200 } ) ;
172201 } ,
202+
173203 /**
174204 * Method used internally for resuming sound manager if
175205 * Phaser.Sound.HTML5AudioSoundManager#pauseOnBlur is set to true.
176206 *
177207 * @protected
178208 * @method Phaser.Sound.HTML5AudioSoundManager#onFocus
179209 */
180- onFocus : function ( ) {
181- this . onBlurPausedSounds . forEach ( function ( sound ) {
210+ onFocus : function ( )
211+ {
212+ this . onBlurPausedSounds . forEach ( function ( sound )
213+ {
182214 sound . onFocus ( ) ;
183215 } ) ;
184216 this . onBlurPausedSounds . length = 0 ;
185217 } ,
218+
186219 /**
187220 * Calls Phaser.Sound.BaseSoundManager#destroy method
188221 * and cleans up all HTML5 Audio related stuff.
189222 *
190223 * @method Phaser.Sound.HTML5AudioSoundManager#destroy
191224 */
192- destroy : function ( ) {
225+ destroy : function ( )
226+ {
193227 BaseSoundManager . prototype . destroy . call ( this ) ;
194228 this . onBlurPausedSounds . length = 0 ;
195229 this . onBlurPausedSounds = null ;
196230 } ,
231+
197232 /**
198233 * Method used internally by Phaser.Sound.HTML5AudioSound class methods and property setters
199234 * to check if sound manager is locked and then either perform action immediately or queue it
@@ -206,8 +241,10 @@ var HTML5AudioSoundManager = new Class({
206241 * @param {* } [value] - An optional parameter that either holds an array of arguments to be passed to the method call or value to be set to the property.
207242 * @returns {boolean } Whether the sound manager is locked.
208243 */
209- isLocked : function ( sound , prop , value ) {
210- if ( this . locked ) {
244+ isLocked : function ( sound , prop , value )
245+ {
246+ if ( this . locked )
247+ {
211248 this . lockedActionsQueue . push ( {
212249 sound : sound ,
213250 prop : prop ,
@@ -218,21 +255,26 @@ var HTML5AudioSoundManager = new Class({
218255 return false ;
219256 }
220257} ) ;
258+
221259/**
222260 * Global mute setting.
223261 *
224262 * @name Phaser.Sound.HTML5AudioSoundManager#mute
225263 * @property {boolean } mute
226264 */
227265Object . defineProperty ( HTML5AudioSoundManager . prototype , 'mute' , {
228- get : function ( ) {
266+ get : function ( )
267+ {
229268 return this . _mute ;
230269 } ,
231- set : function ( value ) {
270+ set : function ( value )
271+ {
232272 this . _mute = value ;
233- this . forEachActiveSound ( function ( sound ) {
273+ this . forEachActiveSound ( function ( sound )
274+ {
234275 sound . setMute ( ) ;
235276 } ) ;
277+
236278 /**
237279 * @event Phaser.Sound.HTML5AudioSoundManager#mute
238280 * @param {Phaser.Sound.HTML5AudioSoundManager } soundManager - Reference to the sound manager that emitted event.
@@ -241,21 +283,26 @@ Object.defineProperty(HTML5AudioSoundManager.prototype, 'mute', {
241283 this . emit ( 'mute' , this , value ) ;
242284 }
243285} ) ;
286+
244287/**
245288 * Global volume setting.
246289 *
247290 * @name Phaser.Sound.HTML5AudioSoundManager#volume
248291 * @property {number } volume
249292 */
250293Object . defineProperty ( HTML5AudioSoundManager . prototype , 'volume' , {
251- get : function ( ) {
294+ get : function ( )
295+ {
252296 return this . _volume ;
253297 } ,
254- set : function ( value ) {
298+ set : function ( value )
299+ {
255300 this . _volume = value ;
256- this . forEachActiveSound ( function ( sound ) {
301+ this . forEachActiveSound ( function ( sound )
302+ {
257303 sound . setVolume ( ) ;
258304 } ) ;
305+
259306 /**
260307 * @event Phaser.Sound.HTML5AudioSoundManager#volume
261308 * @param {Phaser.Sound.HTML5AudioSoundManager } soundManager - Reference to the sound manager that emitted event.
0 commit comments