MCPcopy Create free account
hub / github.com/codecombat/codecombat / playSound

Function playSound

app/core/store/modules/audio.js:243–315  ·  view source on GitHub ↗

* Plays a new sound on a track. * * @param opts {string|object} SoundId or Howl configuration to play * @param opts.track {string} Track to play sound on * @param opts.preloadedId {string} For playing an already loaded sound * @param opts.volume {number} Volume of sound [0,

({ getters, commit, state, dispatch }, opts)

Source from the content-addressed store, hash-verified

241 * @throws {Error} when invalid ID specified
242 */
243 playSound ({ getters, commit, state, dispatch }, opts) {
244 if (typeof opts !== 'object' || opts.preloadedId) {
245 const id = typeof opts !== 'object' ? opts : opts.preloadedId
246
247 const sound = getters.getSoundById(id)
248 if (!sound) {
249 throw new Error(`Sound ID does not exist: ${id}`)
250 }
251
252 // Creates a new overlapping oneshot sound instance.
253 const playingInstanceId = sound.play()
254
255 // Setting volume cancels any current fade effects
256 if (typeof opts?.volume === 'number') {
257 // Note: It is important that this happens after the play is called.
258 // This allows us to only change the volume of the instance playing.
259 // There may still be another instance playing that has a different volume.
260 sound.volume(opts.volume, playingInstanceId)
261 }
262
263 return id
264 }
265
266 if (!getters.hasTrack(opts.track)) {
267 throw new Error('Invalid track specified')
268 }
269
270 const { track, unique, ...howlOpts } = opts
271
272 if (unique && state.unique.keys.has(unique)) {
273 return
274 }
275
276 if (track && track === 'background' && !me.get('music', true)) {
277 // Take this opportunity to mute the background music track if we me.get('music') has been disabled
278 dispatch('muteTrack', track)
279 }
280
281 // Default to playing sounds immediately upon load but allow
282 // consumers to specify autoplay behavior to support scenarios
283 // such as pre loading sound effects.
284 if (typeof howlOpts.autoplay === 'undefined') {
285 howlOpts.autoplay = true
286 }
287
288 const sound = new Howl({
289 ...howlOpts,
290
291 mute: howlOpts.muted || state.muted.all || state.muted[opts.track]
292 })
293
294 // Prevent falsy 0 by incrementing
295 let soundId = Math.random() + 1
296 while (getters.hasId(soundId)) {
297 soundId = Math.random() + 1
298 }
299
300 if (!howlOpts.loop) {

Callers 1

awardRandomGemsMethod · 0.50

Calls 2

dispatchFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected