33 * @copyright 2018 Photon Storm Ltd.
44 * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License }
55 */
6- var Class = require ( '../../utils/Class' ) ;
6+
77var BaseSoundManager = require ( '../BaseSoundManager' ) ;
8+ var Class = require ( '../../utils/Class' ) ;
89var HTML5AudioSound = require ( './HTML5AudioSound' ) ;
910
1011/**
@@ -20,8 +21,12 @@ var HTML5AudioSound = require('./HTML5AudioSound');
2021 * @param {Phaser.Game } game - Reference to the current game instance.
2122 */
2223var HTML5AudioSoundManager = new Class ( {
24+
2325 Extends : BaseSoundManager ,
24- initialize : function HTML5AudioSoundManager ( game )
26+
27+ initialize :
28+
29+ function HTML5AudioSoundManager ( game )
2530 {
2631 /**
2732 * Flag indicating whether if there are no idle instances of HTML5 Audio tag,
@@ -77,6 +82,7 @@ var HTML5AudioSoundManager = new Class({
7782 * @since 3.0.0
7883 */
7984 this . onBlurPausedSounds = [ ] ;
85+
8086 this . locked = 'ontouchstart' in window ;
8187
8288 /**
@@ -115,6 +121,7 @@ var HTML5AudioSoundManager = new Class({
115121 * @since 3.0.0
116122 */
117123 this . _volume = 1 ;
124+
118125 BaseSoundManager . call ( this , game ) ;
119126 } ,
120127
@@ -132,7 +139,9 @@ var HTML5AudioSoundManager = new Class({
132139 add : function ( key , config )
133140 {
134141 var sound = new HTML5AudioSound ( this , key , config ) ;
142+
135143 this . sounds . push ( sound ) ;
144+
136145 return sound ;
137146 } ,
138147
@@ -147,21 +156,32 @@ var HTML5AudioSoundManager = new Class({
147156 unlock : function ( )
148157 {
149158 var _this = this ;
159+
150160 var moved = false ;
161+
151162 var detectMove = function ( )
152163 {
153164 moved = true ;
154165 } ;
166+
155167 var unlock = function ( )
156168 {
169+ if ( ! _this . game . cache . audio . entries . size )
170+ {
171+ return ;
172+ }
173+
157174 if ( moved )
158175 {
159176 moved = false ;
160177 return ;
161178 }
179+
162180 document . body . removeEventListener ( 'touchmove' , detectMove ) ;
163181 document . body . removeEventListener ( 'touchend' , unlock ) ;
182+
164183 var allTags = [ ] ;
184+
165185 _this . game . cache . audio . entries . each ( function ( key , tags )
166186 {
167187 for ( var i = 0 ; i < tags . length ; i ++ )
@@ -170,25 +190,30 @@ var HTML5AudioSoundManager = new Class({
170190 }
171191 return true ;
172192 } ) ;
193+
173194 var lastTag = allTags [ allTags . length - 1 ] ;
195+
174196 lastTag . oncanplaythrough = function ( )
175197 {
176198 lastTag . oncanplaythrough = null ;
177199 _this . unlocked = true ;
178200 } ;
201+
179202 allTags . forEach ( function ( tag )
180203 {
181204 tag . load ( ) ;
182205 } ) ;
183206 } ;
207+
184208 this . once ( 'unlocked' , function ( )
185209 {
186- _this . forEachActiveSound ( function ( sound )
210+ this . forEachActiveSound ( function ( sound )
187211 {
188212 sound . duration = sound . tags [ 0 ] . duration ;
189213 sound . totalDuration = sound . tags [ 0 ] . duration ;
190214 } ) ;
191- _this . lockedActionsQueue . forEach ( function ( lockedAction )
215+
216+ this . lockedActionsQueue . forEach ( function ( lockedAction )
192217 {
193218 if ( lockedAction . sound [ lockedAction . prop ] . apply )
194219 {
@@ -199,9 +224,11 @@ var HTML5AudioSoundManager = new Class({
199224 lockedAction . sound [ lockedAction . prop ] = lockedAction . value ;
200225 }
201226 } ) ;
202- _this . lockedActionsQueue . length = 0 ;
203- _this . lockedActionsQueue = null ;
204- } ) ;
227+
228+ this . lockedActionsQueue . length = 0 ;
229+ this . lockedActionsQueue = null ;
230+ } , this ) ;
231+
205232 document . body . addEventListener ( 'touchmove' , detectMove , false ) ;
206233 document . body . addEventListener ( 'touchend' , unlock , false ) ;
207234 } ,
@@ -240,6 +267,7 @@ var HTML5AudioSoundManager = new Class({
240267 {
241268 sound . onFocus ( ) ;
242269 } ) ;
270+
243271 this . onBlurPausedSounds . length = 0 ;
244272 } ,
245273
@@ -253,6 +281,7 @@ var HTML5AudioSoundManager = new Class({
253281 destroy : function ( )
254282 {
255283 BaseSoundManager . prototype . destroy . call ( this ) ;
284+
256285 this . onBlurPausedSounds . length = 0 ;
257286 this . onBlurPausedSounds = null ;
258287 } ,
@@ -281,51 +310,79 @@ var HTML5AudioSoundManager = new Class({
281310 prop : prop ,
282311 value : value
283312 } ) ;
313+
284314 return true ;
285315 }
316+
286317 return false ;
287- }
288- } ) ;
289- Object . defineProperty ( HTML5AudioSoundManager . prototype , 'mute' , {
290- get : function ( )
291- {
292- return this . _mute ;
293318 } ,
294- set : function ( value )
295- {
296- this . _mute = value ;
297- this . forEachActiveSound ( function ( sound )
319+
320+ /**
321+ * @event Phaser.Sound.HTML5AudioSoundManager#MuteEvent
322+ * @param {Phaser.Sound.HTML5AudioSoundManager } soundManager - Reference to the sound manager that emitted event.
323+ * @param {boolean } value - An updated value of Phaser.Sound.HTML5AudioSoundManager#mute property.
324+ */
325+
326+ /**
327+ * @name Phaser.Sound.HTML5AudioSoundManager#mute
328+ * @type {boolean }
329+ * @fires Phaser.Sound.HTML5AudioSoundManager#MuteEvent
330+ * @since 3.0.0
331+ */
332+ mute : {
333+
334+ get : function ( )
298335 {
299- sound . setMute ( ) ;
300- } ) ;
336+ return this . _mute ;
337+ } ,
338+
339+ set : function ( value )
340+ {
341+ this . _mute = value ;
342+
343+ this . forEachActiveSound ( function ( sound )
344+ {
345+ sound . setMute ( ) ;
346+ } ) ;
347+
348+ this . emit ( 'mute' , this , value ) ;
349+ }
301350
302- /**
303- * @event Phaser.Sound.HTML5AudioSoundManager#mute
304- * @param {Phaser.Sound.HTML5AudioSoundManager } soundManager - Reference to the sound manager that emitted event.
305- * @param {boolean } value - An updated value of Phaser.Sound.HTML5AudioSoundManager#mute property.
306- */
307- this . emit ( 'mute' , this , value ) ;
308- }
309- } ) ;
310- Object . defineProperty ( HTML5AudioSoundManager . prototype , 'volume' , {
311- get : function ( )
312- {
313- return this . _volume ;
314351 } ,
315- set : function ( value )
316- {
317- this . _volume = value ;
318- this . forEachActiveSound ( function ( sound )
352+
353+ /**
354+ * @event Phaser.Sound.HTML5AudioSoundManager#VolumeEvent
355+ * @param {Phaser.Sound.HTML5AudioSoundManager } soundManager - Reference to the sound manager that emitted event.
356+ * @param {number } value - An updated value of Phaser.Sound.HTML5AudioSoundManager#volume property.
357+ */
358+
359+ /**
360+ * @name Phaser.Sound.HTML5AudioSoundManager#volume
361+ * @type {number }
362+ * @fires Phaser.Sound.HTML5AudioSoundManager#VolumeEvent
363+ * @since 3.0.0
364+ */
365+ volume : {
366+
367+ get : function ( )
319368 {
320- sound . setVolume ( ) ;
321- } ) ;
369+ return this . _volume ;
370+ } ,
371+
372+ set : function ( value )
373+ {
374+ this . _volume = value ;
375+
376+ this . forEachActiveSound ( function ( sound )
377+ {
378+ sound . setVolume ( ) ;
379+ } ) ;
380+
381+ this . emit ( 'volume' , this , value ) ;
382+ }
322383
323- /**
324- * @event Phaser.Sound.HTML5AudioSoundManager#volume
325- * @param {Phaser.Sound.HTML5AudioSoundManager } soundManager - Reference to the sound manager that emitted event.
326- * @param {number } value - An updated value of Phaser.Sound.HTML5AudioSoundManager#volume property.
327- */
328- this . emit ( 'volume' , this , value ) ;
329384 }
385+
330386} ) ;
387+
331388module . exports = HTML5AudioSoundManager ;
0 commit comments