Skip to main content

Class: SoundManager

Fetches, loads, and provides sounds to the game.

For m2c2kit library use only

Constructors

new SoundManager()

new SoundManager(game, baseUrls): SoundManager

Parameters

game: Game

baseUrls: GameBaseUrls

Returns

SoundManager

Source

SoundManager.ts:18

Properties

_audioContext?

private optional _audioContext: AudioContext

Source

SoundManager.ts:16


baseUrls

private baseUrls: GameBaseUrls

Source

SoundManager.ts:15


game

private game: Game

Source

SoundManager.ts:14


sounds

private sounds: Record<string, M2Sound> = {}

Source

SoundManager.ts:13

Accessors

audioContext

get audioContext(): AudioContext

Returns

AudioContext

Source

SoundManager.ts:23

Methods

decodeFetchedSounds()

Internal

decodeFetchedSounds(): Promise<void[]>

Decodes all fetched sounds from bytes to an AudioBuffer.

For m2c2kit library use only

Returns

Promise<void[]>

A promise that completes when all fetched sounds have been decoded

Remarks

This method will be called after the AudioContext has been created and if there are fetched sounds waiting to be decoded.

Source

SoundManager.ts:168


decodeSound()

private decodeSound(sound): Promise<void>

Decodes a sound from bytes to an AudioBuffer.

Parameters

sound: M2Sound

sound to decode

Returns

Promise<void>

Source

SoundManager.ts:181


dispose()

Internal

dispose(): void

Frees up resources allocated by the SoundManager.

For m2c2kit library use only

Returns

void

Remarks

This will be done automatically by the m2c2kit library; the end-user must not call this.

Source

SoundManager.ts:233


fetchDeferredSound()

Internal

fetchDeferredSound(m2Sound): Promise<void>

Fetches a m2c2kit sound (M2Sound) that was previously initialized with lazy loading.

For m2c2kit library use only

Parameters

m2Sound: M2Sound

M2Sound to fetch

Returns

Promise<void>

A promise that completes when sounds have been fetched

Source

SoundManager.ts:137


fetchSounds()

private fetchSounds(): Promise<void>

Returns

Promise<void>

Source

SoundManager.ts:103


getSound()

Internal

getSound(soundName): M2Sound

Returns a m2c2kit sound (M2Sound) that has been entered into the SoundManager.

For m2c2kit library use only

Parameters

soundName: string

sound's name as defined in the game's sound assets

Returns

M2Sound

a m2c2kit sound

Remarks

Typically, a user won't need to call this because sound initialization and processing is handled by the framework.

Source

SoundManager.ts:217


getSoundNames()

getSoundNames(): string[]

Gets names of sounds entered in the SoundManager.

Returns

string[]

array of sound names

Remarks

These are sounds that the SoundManager is aware of. The sounds may not be ready to play (may not have been fetched or decoded yet).

Source

SoundManager.ts:243


hasSoundsToDecode()

Internal

hasSoundsToDecode(): boolean

Checks if the SoundManager has sounds needing decoding.

For m2c2kit library use only

Returns

boolean

true if there are sounds that have been fetched and are waiting to be decoded (status is M2SoundStatus.Fetched)

Source

SoundManager.ts:150


initializeSounds()

Internal

initializeSounds(soundAssets): Promise<void>

Loads sound assets during the game initialization.

For m2c2kit library use only

Parameters

soundAssets: undefined | SoundAsset[]

array of SoundAsset objects

Returns

Promise<void>

Remarks

Typically, a user won't call this because the m2c2kit framework will call this automatically. At initialization, sounds can only be fetched, not decoded because the AudioContext can not yet be created (it requires a user interaction).

Source

SoundManager.ts:47


loadSounds()

loadSounds(soundAssets): Promise<void>

Loads an array of sound assets and makes them ready for the game.

Parameters

soundAssets: SoundAsset[]

an array of SoundAsset

Returns

Promise<void>

A promise that completes when all sounds have loaded

Remarks

Loading a sound consists of 1) fetching the sound file and 2) decoding the sound data. The sound is then ready to be played. Step 1 can be done at any time, but step 2 requires an AudioContext, which can only be created after a user interaction. If a play Action is attempted before the sound is ready (either it has not been fetched or decoded), the play Action will log a warning to the console and the loading process will continue in the background, and the sound will play when ready. This loadSounds() method does not have to be awaited.

Source

SoundManager.ts:69