The SoundManager is a sound management class. It provides control methods for playing background music and sound effects.

  • The engine has two default sound schemes: WebAudio and H5Audio.
  • For playing sound effects, WebAudio is prioritized. If WebAudio is not available, H5Audio is used. H5Audio may have compatibility issues on some devices (such as inability to mix sounds or playback delays).
  • For playing background music, H5Audio is used (using WebAudio would significantly increase memory usage and cause a delay as it needs to wait for the sound to load completely before playing).
  • It is recommended to use mp3 format for background music and wav or mp3 format for sound effects (if packaging as an app, only wav format can be used for sound effects).

SoundManager 是一个声音管理类。提供了对背景音乐、音效的播放控制方法。

  • 引擎默认有两套声音方案:WebAudio和H5Audio。
  • 播放音效时,优先使用WebAudio播放声音,如果WebAudio不可用,则用H5Audio播放。H5Audio在部分机器上有兼容问题(比如不能混音,播放有延迟等)。
  • 播放背景音乐时,则使用H5Audio播放(使用WebAudio会增加特别大的内存,并且要等加载完毕后才能播放,有延迟)。
  • 建议背景音乐用mp3类型,音效用wav或者mp3类型(如果打包为app,音效只能用wav格式)。

构造函数

属性

autoStopMusic: boolean = true

Whether to automatically stop background music when losing focus.

失去焦点后是否自动停止背景音乐。

playbackRate: number = 1

Sound playback rate. default value is 1.

声音播放速率。默认值为1。

useAudioMusic: boolean = true

Determines whether background music is played using the Audio tag.

背景音乐是否使用Audio标签播放。

访问器

方法

  • 参数

    • url: string

      The URL of the sound file.

    • 可选loops: number

      The number of times to loop the music. 0 means infinite loop. Default is 1.

    • 可选complete: ((success: boolean) => void)

      The callback function when the music playback is complete. The result parameter is true if playback is completed, false/undefined if triggered by stop.

        • (success): void
        • 参数

          • success: boolean

          返回 void

    • 可选startTime: number

      The start time of the music playback. In seconds.

    返回 SoundChannel

    A SoundChannel object, through which you can control the sound and get sound information.

    Play background music. Only one background music can be played at a time. If this method is called while background music is already playing, the previous music will be stopped before playing the current one.

    播放背景音乐。背景音乐同时只能播放一个,如果在播放背景音乐时再次调用本方法,会先停止之前的背景音乐,再播放当前的背景音乐。

  • 参数

    • url: string
    • 可选loops: number
    • 可选complete: Handler
    • 可选startTime: number

    返回 SoundChannel

  • 参数

    • url: string

      The URL of the sound file.

    • 可选loops: number

      The number of times to loop the sound. 0 means infinite loop.

    • 可选complete: (() => void)

      The callback function when the sound playback is complete.

        • (): void
        • 返回 void

    • 可选startTime: number

      The start time of the sound playback. In seconds.

    返回 SoundChannel

    A SoundChannel object, through which you can control the sound and get sound information.

    Play a sound effect. Multiple sound effects can be played simultaneously.

    播放音效。音效可以同时播放多个。

  • 参数

    • url: string
    • 可选loops: number
    • 可选complete: Handler
    • 可选startTime: number

    返回 SoundChannel

  • 参数

    • url: string
    • 可选loops: number
    • 可选complete: Handler
    • 可选soundClass: (new () => any)
        • new (): any
        • 返回 any

    • 可选startTime: number

    返回 SoundChannel

  • 返回 void

    Stop playing all sounds (including background music and sound effects).

    停止播放所有声音(包括背景音乐和音效)。

  • 参数

    • url: string

      The URL of the sound file.

    返回 void

    Stop playing a specific sound. This method can stop the playback of any sound (including background music and sound effects) by providing the corresponding sound file address.

    停止声音播放。此方法能够停止任意声音的播放(包括背景音乐和音效),只需传入对应的声音播放地址。