Skip to content

Commit 3149cd6

Browse files
authored
Merge pull request phaserjs#5095 from samme/feature/new-soundmanager-methods
Add new Sound Manager methods
2 parents 3122b51 + e8381c1 commit 3149cd6

1 file changed

Lines changed: 73 additions & 4 deletions

File tree

src/sound/BaseSoundManager.js

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var EventEmitter = require('eventemitter3');
1111
var Events = require('./events');
1212
var GameEvents = require('../core/events');
1313
var NOOP = require('../utils/NOOP');
14+
var GetAll = require('../utils/array/GetAll');
15+
var GetFirst = require('../utils/array/GetFirst');
1416

1517
/**
1618
* @classdesc
@@ -225,6 +227,36 @@ var BaseSoundManager = new Class({
225227
return sound;
226228
},
227229

230+
/**
231+
* Gets the first sound in the manager matching the given key, if any.
232+
*
233+
* @method Phaser.Sound.BaseSoundManager#get
234+
* @since 3.23.0
235+
*
236+
* @param {string} key - Sound asset key.
237+
*
238+
* @return {?Phaser.Sound.BaseSound} - The sound, or null.
239+
*/
240+
get: function (key)
241+
{
242+
return GetFirst(this.sounds, 'key', key);
243+
},
244+
245+
/**
246+
* Gets any sounds in the manager matching the given key.
247+
*
248+
* @method Phaser.Sound.BaseSoundManager#getAll
249+
* @since 3.23.0
250+
*
251+
* @param {string} key - Sound asset key.
252+
*
253+
* @return {Phaser.Sound.BaseSound[]} - The sounds, or an empty array.
254+
*/
255+
getAll: function (key)
256+
{
257+
return GetAll(this.sounds, 'key', key);
258+
},
259+
228260
/**
229261
* Adds a new sound to the sound manager and plays it.
230262
* The sound will be automatically removed (destroyed) once playback ends.
@@ -315,6 +347,23 @@ var BaseSoundManager = new Class({
315347
return false;
316348
},
317349

350+
351+
/**
352+
* Removes all sounds from the manager, destroying the sounds.
353+
*
354+
* @method Phaser.Sound.BaseSoundManager#removeAll
355+
* @since 3.23.0
356+
*/
357+
removeAll: function ()
358+
{
359+
this.sounds.forEach(function (sound)
360+
{
361+
sound.destroy();
362+
});
363+
364+
this.sounds.length = 0;
365+
},
366+
318367
/**
319368
* Removes all sounds from the sound manager that have an asset key matching the given value.
320369
* The removed sounds are destroyed before removal.
@@ -398,6 +447,29 @@ var BaseSoundManager = new Class({
398447
this.emit(Events.STOP_ALL, this);
399448
},
400449

450+
451+
/**
452+
* Stops any sounds matching the given key.
453+
*
454+
* @method Phaser.Sound.BaseSoundManager#stopByKey
455+
* @since 3.23.0
456+
*
457+
* @param {string} key - Sound asset key.
458+
*
459+
* @return {number} - How many sounds were stopped.
460+
*/
461+
stopByKey: function (key)
462+
{
463+
var stopped = 0;
464+
465+
this.getAll(key).forEach(function (sound)
466+
{
467+
if (sound.stop()) { stopped++; }
468+
});
469+
470+
return stopped;
471+
},
472+
401473
/**
402474
* Method used internally for unlocking audio playback on devices that
403475
* require user interaction before any sound can be played on a web page.
@@ -479,10 +551,7 @@ var BaseSoundManager = new Class({
479551
{
480552
this.removeAllListeners();
481553

482-
this.forEachActiveSound(function (sound)
483-
{
484-
sound.destroy();
485-
});
554+
this.removeAll();
486555

487556
this.sounds.length = 0;
488557
this.sounds = null;

0 commit comments

Comments
 (0)