/
NoAudioSoundManager.js
360 lines (323 loc) · 10 KB
/
NoAudioSoundManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* @author Richard Davey <rich@photonstorm.com>
* @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var BaseSoundManager = require('../BaseSoundManager');
var Class = require('../../utils/Class');
var EventEmitter = require('eventemitter3');
var NoAudioSound = require('./NoAudioSound');
var NOOP = require('../../utils/NOOP');
/**
* @classdesc
* No-audio implementation of the Sound Manager. It is used if audio has been
* disabled in the game config or the device doesn't support any audio.
*
* It represents a graceful degradation of Sound Manager logic that provides
* minimal functionality and prevents Phaser projects that use audio from
* breaking on devices that don't support any audio playback technologies.
*
* @class NoAudioSoundManager
* @extends Phaser.Sound.BaseSoundManager
* @memberof Phaser.Sound
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - Reference to the current game instance.
*/
var NoAudioSoundManager = new Class({
Extends: EventEmitter,
initialize:
function NoAudioSoundManager (game)
{
EventEmitter.call(this);
this.game = game;
this.sounds = [];
this.mute = false;
this.volume = 1;
this.rate = 1;
this.detune = 0;
this.pauseOnBlur = true;
this.locked = false;
},
/**
* Adds a new sound into the sound manager.
*
* @method Phaser.Sound.NoAudioSoundManager#add
* @since 3.60.0
*
* @param {string} key - Asset key for the sound.
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
*
* @return {Phaser.Sound.NoAudioSound} The new sound instance.
*/
add: function (key, config)
{
var sound = new NoAudioSound(this, key, config);
this.sounds.push(sound);
return sound;
},
/**
* Adds a new audio sprite sound into the sound manager.
* Audio Sprites are a combination of audio files and a JSON configuration.
* The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite
*
* @method Phaser.Sound.NoAudioSoundManager#addAudioSprite
* @since 3.60.0
*
* @param {string} key - Asset key for the sound.
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
*
* @return {Phaser.Sound.NoAudioSound} The new audio sprite sound instance.
*/
addAudioSprite: function (key, config)
{
var sound = this.add(key, config);
sound.spritemap = {};
return sound;
},
/**
* Gets the first sound in the manager matching the given key, if any.
*
* @method Phaser.Sound.NoAudioSoundManager#get
* @since 3.23.0
*
* @generic {Phaser.Sound.BaseSound} T
* @genericUse {T} - [$return]
*
* @param {string} key - Sound asset key.
*
* @return {?Phaser.Sound.BaseSound} - The sound, or null.
*/
get: function (key)
{
return BaseSoundManager.prototype.get.call(this, key);
},
/**
* Gets any sounds in the manager matching the given key.
*
* @method Phaser.Sound.NoAudioSoundManager#getAll
* @since 3.23.0
*
* @generic {Phaser.Sound.BaseSound} T
* @genericUse {T[]} - [$return]
*
* @param {string} key - Sound asset key.
*
* @return {Phaser.Sound.BaseSound[]} - The sounds, or an empty array.
*/
getAll: function (key)
{
return BaseSoundManager.prototype.getAll.call(this, key);
},
/**
* This method does nothing but return 'false' for the No Audio Sound Manager, to maintain
* compatibility with the other Sound Managers.
*
* @method Phaser.Sound.NoAudioSoundManager#play
* @since 3.0.0
*
* @param {string} key - Asset key for the sound.
* @param {(Phaser.Types.Sound.SoundConfig|Phaser.Types.Sound.SoundMarker)} [extra] - An optional additional object containing settings to be applied to the sound. It could be either config or marker object.
*
* @return {boolean} Always 'false' for the No Audio Sound Manager.
*/
// eslint-disable-next-line no-unused-vars
play: function (key, extra)
{
return false;
},
/**
* This method does nothing but return 'false' for the No Audio Sound Manager, to maintain
* compatibility with the other Sound Managers.
*
* @method Phaser.Sound.NoAudioSoundManager#playAudioSprite
* @since 3.0.0
*
* @param {string} key - Asset key for the sound.
* @param {string} spriteName - The name of the sound sprite to play.
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
*
* @return {boolean} Always 'false' for the No Audio Sound Manager.
*/
// eslint-disable-next-line no-unused-vars
playAudioSprite: function (key, spriteName, config)
{
return false;
},
/**
* Removes a sound from the sound manager.
* The removed sound is destroyed before removal.
*
* @method Phaser.Sound.NoAudioSoundManager#remove
* @since 3.0.0
*
* @param {Phaser.Sound.BaseSound} sound - The sound object to remove.
*
* @return {boolean} True if the sound was removed successfully, otherwise false.
*/
remove: function (sound)
{
return BaseSoundManager.prototype.remove.call(this, sound);
},
/**
* Removes all sounds from the manager, destroying the sounds.
*
* @method Phaser.Sound.NoAudioSoundManager#removeAll
* @since 3.23.0
*/
removeAll: function ()
{
return BaseSoundManager.prototype.removeAll.call(this);
},
/**
* Removes all sounds from the sound manager that have an asset key matching the given value.
* The removed sounds are destroyed before removal.
*
* @method Phaser.Sound.NoAudioSoundManager#removeByKey
* @since 3.0.0
*
* @param {string} key - The key to match when removing sound objects.
*
* @return {number} The number of matching sound objects that were removed.
*/
removeByKey: function (key)
{
return BaseSoundManager.prototype.removeByKey.call(this, key);
},
/**
* Stops any sounds matching the given key.
*
* @method Phaser.Sound.NoAudioSoundManager#stopByKey
* @since 3.23.0
*
* @param {string} key - Sound asset key.
*
* @return {number} - How many sounds were stopped.
*/
stopByKey: function (key)
{
return BaseSoundManager.prototype.stopByKey.call(this, key);
},
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#onBlur
* @since 3.0.0
*/
onBlur: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#onFocus
* @since 3.0.0
*/
onFocus: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#onGameBlur
* @since 3.0.0
*/
onGameBlur: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#onGameFocus
* @since 3.0.0
*/
onGameFocus: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#pauseAll
* @since 3.0.0
*/
pauseAll: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#resumeAll
* @since 3.0.0
*/
resumeAll: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#stopAll
* @since 3.0.0
*/
stopAll: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#update
* @since 3.0.0
*/
update: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#setRate
* @since 3.0.0
*
* @return {this} This Sound Manager.
*/
setRate: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#setDetune
* @since 3.0.0
*
* @return {this} This Sound Manager.
*/
setDetune: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#setMute
* @since 3.0.0
*/
setMute: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#setVolume
* @since 3.0.0
*/
setVolume: NOOP,
/**
* Empty function for the No Audio Sound Manager.
*
* @method Phaser.Sound.NoAudioSoundManager#unlock
* @since 3.0.0
*/
unlock: NOOP,
/**
* Method used internally for iterating only over active sounds and skipping sounds that are marked for removal.
*
* @method Phaser.Sound.NoAudioSoundManager#forEachActiveSound
* @private
* @since 3.0.0
*
* @param {Phaser.Types.Sound.EachActiveSoundCallback} callback - Callback function. (manager: Phaser.Sound.BaseSoundManager, sound: Phaser.Sound.BaseSound, index: number, sounds: Phaser.Manager.BaseSound[]) => void
* @param {*} [scope] - Callback context.
*/
forEachActiveSound: function (callbackfn, scope)
{
BaseSoundManager.prototype.forEachActiveSound.call(this, callbackfn, scope);
},
/**
* Destroys all the sounds in the game and all associated events.
*
* @method Phaser.Sound.NoAudioSoundManager#destroy
* @since 3.0.0
*/
destroy: function ()
{
BaseSoundManager.prototype.destroy.call(this);
}
});
module.exports = NoAudioSoundManager;