Class AudioManager

音频管理

Example

// 模块功能通过 oops.audio 调用
oops.audio.playMusic("audios/nocturne");

Hierarchy

  • Component
    • AudioManager

Constructors

  • Parameters

    • Optional name: string

    Returns AudioManager

Properties

__editorExtras__: unknown
__prefab: null | CompPrefabInfo
_enabled: boolean

Deprecated

since v3.5.0, this is an engine private interface that will be removed in the future.

_id: string

For internal usage.

Deprecated

since v3.5.0, this is an engine private interface that will be removed in the future.

_name: string
_objFlags: number
_sceneGetter: null | (() => RenderScene)
_switch_effect: boolean = true
_switch_music: boolean = true
_volume_effect: number = 1
_volume_music: number = 1
effect: AudioEffect = ...
local_data: any = {}
music: AudioMusic = ...
node: Node

En

The node this component is attached to. A component is always attached to a node.

Zh

该组件被附加到的节点。组件总会附加到一个节点。

Example

import { log } from 'cc';
log(comp.node);
EventHandler: typeof EventHandler
system: null

Accessors

  • get __scriptAsset(): null
  • Deprecated

    since v3.5.0, this is an engine private interface that will be removed in the future.

    Returns null

  • get _isOnLoadCalled(): number
  • En

    Returns a value which used to indicate the onLoad get called or not.

    Zh

    返回一个值用来判断 onLoad 是否被调用过,不等于 0 时调用过,等于 0 时未调用。

    Read Only

    Example

    import { log } from 'cc';
    log(this._isOnLoadCalled > 0);

    Deprecated

    since v3.5.0, this is an engine private interface that will be removed in the future.

    Returns number

  • get enabled(): boolean
  • En

    Indicates whether this component is enabled or not.

    Zh

    表示该组件自身是否启用。

    Default

    true

    Example

    import { log } from 'cc';
    comp.enabled = true;
    log(comp.enabled);

    Returns boolean

  • set enabled(value: boolean): void
  • Parameters

    • value: boolean

    Returns void

  • get enabledInHierarchy(): boolean
  • En

    Indicates whether this component is enabled and its node is also active in the hierarchy.

    Zh

    表示该组件是否被启用并且所在的节点也处于激活状态。

    Read Only

    Example

    import { log } from 'cc';
    log(comp.enabledInHierarchy);

    Returns boolean

  • get hideFlags(): Flags
  • Returns Flags

  • set hideFlags(hideFlags: Flags): void
  • En

    After inheriting CCObject objects, control whether you need to hide, lock, serialize, and other functions.

    Zh

    在继承 CCObject 对象后,控制是否需要隐藏,锁定,序列化等功能。

    Parameters

    • hideFlags: Flags

    Returns void

  • get isValid(): boolean
  • En

    Indicates whether the object is not yet destroyed. (It will not be available after being destroyed)
    When an object's destroy is called, it is actually destroyed after the end of this frame. So isValid will return false from the next frame, while isValid in the current frame will still be true. If you want to determine whether the current frame has called destroy, use isValid(obj, true), but this is often caused by a particular logical requirements, which is not normally required.

    Zh

    表示该对象是否可用(被 destroy 后将不可用)。
    当一个对象的 destroy 调用以后,会在这一帧结束后才真正销毁。
    因此从下一帧开始 isValid 就会返回 false,而当前帧内 isValid 仍然会是 true。
    如果希望判断当前帧是否调用过 destroy,请使用 isValid(obj, true),不过这往往是特殊的业务需求引起的,通常情况下不需要这样。

    Default

    true

    Read Only

    Example

    import { Node, log } from 'cc';
    const node = new Node();
    log(node.isValid); // true
    node.destroy();
    log(node.isValid); // true, still valid in this frame
    // after a frame...
    log(node.isValid); // false, destroyed in the end of last frame

    Returns boolean

  • get name(): string
  • Returns string

  • set name(value: string): void
  • Parameters

    • value: string

    Returns void

  • get progressMusic(): number
  • 获取背景音乐播放进度

    Returns number

  • set progressMusic(value: number): void
  • 设置背景乐播放进度

    Parameters

    • value: number

      播放进度值

    Returns void

  • get switchEffect(): boolean
  • 获取音效开关值

    Returns boolean

  • set switchEffect(value: boolean): void
  • 设置音效开关值

    Parameters

    • value: boolean

      音效开关值

    Returns void

  • get switchMusic(): boolean
  • 获取背景音乐开关值

    Returns boolean

  • set switchMusic(value: boolean): void
  • 设置背景音乐开关值

    Parameters

    • value: boolean

      开关值

    Returns void

  • get uuid(): string
  • En

    The uuid for editor.

    Zh

    组件的 uuid,用于编辑器。

    Read Only

    Example

    import { log } from 'cc';
    log(comp.uuid);

    Returns string

  • get volumeEffect(): number
  • 获取音效音量

    Returns number

  • set volumeEffect(value: number): void
  • 设置获取音效音量

    Parameters

    • value: number

      音效音量值

    Returns void

  • get volumeMusic(): number
  • 获取背景音乐音量

    Returns number

  • set volumeMusic(value: number): void
  • 设置背景音乐音量

    Parameters

    • value: number

      音乐音量值

    Returns void

Methods

  • Private

    En

    __preload is called before every onLoad.
    It is used to initialize the builtin components internally,
    to avoid checking whether onLoad is called before every public method calls.
    This method should be removed if script priority is supported.

    Zh

    __preload 在每次onLoad之前调用。
    它用于在内部初始化内置组件,
    以避免在每次公有方法调用之前检查是否调用了onLoad。
    如果支持脚本优先级,则应删除此方法。

    Returns void

  • Deprecated

    since v3.5.0, this is an engine private interface that will be removed in the future.

    Returns void

  • En

    Clear all references in the instance.

    NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject.

    Zh

    清理实例的所有引用 注意:此方法不会清理实例上的 getter 与 setter 方法。

    Deprecated

    since v3.5.0, this is an engine private interface that will be removed in the future.

    Example

    // You can override the _destruct method if you need, for example:
    _destruct: function () {
    for (var key in this) {
    if (this.hasOwnProperty(key)) {
    switch (typeof this[key]) {
    case 'string':
    this[key] = '';
    break;
    case 'object':
    case 'function':
    this[key] = null;
    break;
    }
    }
    }

    Returns void

  • Private

    En

    If the component's bounding box is different from the node's, you can implement this method to supply a custom axis aligned bounding box (AABB), so the editor's scene view can perform hit test properly.

    Zh

    如果组件的包围盒与节点不同,您可以实现该方法以提供自定义的轴向对齐的包围盒(AABB), 以便编辑器的场景视图可以正确地执行点选测试。

    Parameters

    • out_rect: Rect

      The rect to store the result bounding rect

    Returns void

  • Deprecated

    since v3.5.0, this is an engine private interface that will be removed in the future.

    Returns RenderScene

  • Deprecated

    since v3.5.0, this is an engine private interface that will be removed in the future.

    Parameters

    • Optional cloned: Component

    Returns undefined | Component

  • Deprecated

    since v3.5.0, this is an engine private interface that will be removed in the future.

    Returns void

  • En

    Adds a component class to the node. You can also add component to node by passing in the name of the script.

    Zh

    向节点添加一个指定类型的组件类,你还可以通过传入脚本的名称来添加组件。

    Example

    import { Sprite } from 'cc';
    const sprite = node.addComponent(Sprite);

    Type Parameters

    • T extends Component<T>

    Parameters

    • classConstructor: __types_globals__Constructor<T>

      The class of component to be retrieved or to be created

    Returns null | T

  • En

    Adds a component class to the node. You can also add component to node by passing in the name of the script.

    Zh

    向节点添加一个指定类型的组件类,你还可以通过传入脚本的名称来添加组件。

    Example

    const test = node.addComponent("Test");
    

    Parameters

    • className: string

      A string for the class name of the component

    Returns null | Component

  • Returns boolean

  • En

    Returns the component of supplied type if the node has one attached, null if it doesn't.
    You can also get component in the node by passing in the name of the script.

    Zh

    获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
    传入参数也可以是脚本的名称。

    Example

    import { Sprite } from 'cc';
    // get sprite component.
    var sprite = node.getComponent(Sprite);

    Type Parameters

    • T extends Component<T>

    Parameters

    • classConstructor: __types_globals__Constructor<T>

      The class of component to be retrieved or to be created

    Returns null | T

  • En

    Returns the component of supplied type if the node has one attached, null if it doesn't.
    You can also get component in the node by passing in the name of the script.

    Zh

    获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
    传入参数也可以是脚本的名称。

    Example

    // get custom test calss.
    var test = node.getComponent("Test");

    Parameters

    • className: string

      A string for the class name of the component

    Returns null | Component

  • En

    Returns the component of supplied type in any of its children using depth first search.

    Zh

    递归查找所有子节点中第一个匹配指定类型的组件。

    Example

    import { Sprite } from 'cc';
    const sprite = node.getComponentInChildren(Sprite);

    Type Parameters

    • T extends Component<T>

    Parameters

    • classConstructor: __types_globals__Constructor<T>

      The class of component to be retrieved

    Returns null | T

  • En

    Returns the component of supplied type in any of its children using depth first search.

    Zh

    递归查找所有子节点中第一个匹配指定类型的组件。

    Example

    var Test = node.getComponentInChildren("Test");
    

    Parameters

    • className: string

      A string for the class name of the component

    Returns null | Component

  • En

    Returns all components of supplied type in the node.

    Zh

    返回节点上指定类型的所有组件。

    Example

    import { Sprite } from 'cc';
    const sprites = node.getComponents(Sprite);

    Type Parameters

    • T extends Component<T>

    Parameters

    • classConstructor: __types_globals__Constructor<T>

      The class of components to be retrieved

    Returns T[]

  • En

    Returns all components of supplied type in the node.

    Zh

    返回节点上指定类型的所有组件。

    Example

    const tests = node.getComponents("Test");
    

    Parameters

    • className: string

      A string for the class name of the components

    Returns Component[]

  • En

    Returns all components of supplied type in self or any of its children.

    Zh

    递归查找自身或所有子节点中指定类型的组件。

    Example

    import { Sprite } from 'cc';
    const sprites = node.getComponentsInChildren(Sprite);

    Type Parameters

    • T extends Component<T>

    Parameters

    • classConstructor: __types_globals__Constructor<T>

      The class of components to be retrieved

    Returns T[]

  • En

    Returns all components of supplied type in self or any of its children.

    Zh

    递归查找自身或所有子节点中指定类型的组件。

    Example

    const tests = node.getComponentsInChildren("Test");
    

    Parameters

    • className: string

      A string for the class name of the components

    Returns Component[]

  • En

    LateUpdate is called every frame, if the Component is enabled.
    This is a lifecycle method. It may not be implemented in the super class.
    You can only call its super class method inside it. It should not be called manually elsewhere.

    Zh

    如果该组件启用,则每帧调用 LateUpdate。
    该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。

    Parameters

    • dt: number

      the delta time in seconds it took to complete the last frame

    Returns void

  • 本地加载音乐音效的音量、开关配置数据并设置到游戏中

    Returns void

  • En

    Called when this component will be destroyed.
    This is a lifecycle method. It may not be implemented in the super class.
    You can only call its super class method inside it. It should not be called manually elsewhere.

    Zh

    当该组件被销毁时调用
    该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。

    Returns void

  • En

    Called when this component becomes disabled or its node becomes inactive.
    This is a lifecycle method. It may not be implemented in the super class. You can only call its super class method inside it. It should not be called manually elsewhere.

    Zh

    当该组件被禁用或节点变为无效时调用。
    该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。

    Returns void

  • En

    Called when this component becomes enabled and its node is active.
    This is a lifecycle method. It may not be implemented in the super class. You can only call its super class method inside it. It should not be called manually elsewhere.

    Zh

    当该组件被启用,并且它的节点也激活时。
    该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。

    Returns void

  • Returns void

  • En

    When attaching to an active node or its node first activated.
    onLoad is always called before any start functions, this allows you to order initialization of scripts.
    This is a lifecycle method. It may not be implemented in the super class.
    You can only call its super class method inside it. It should not be called manually elsewhere.

    Zh

    当附加到一个激活的节点上或者其节点第一次激活时候调用。onLoad 总是会在任何 start 方法调用前执行,这能用于安排脚本的初始化顺序。
    该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。

    Returns void

  • Returns void

  • En

    onRestore is called after the user clicks the Reset item in the Inspector's context menu or performs an undo operation on this component.

    If the component contains the "internal state", short for "temporary member variables which not included
    in its CCClass properties", then you may need to implement this function.

    The editor will call the getset accessors of your component to record/restore the component's state
    for undo/redo operation. However, in extreme cases, it may not works well. Then you should implement
    this function to manually synchronize your component's "internal states" with its public properties.
    Once you implement this function, all the getset accessors of your component will not be called when
    the user performs an undo/redo operation. Which means that only the properties with default value
    will be recorded or restored by editor.

    Similarly, the editor may failed to reset your component correctly in extreme cases. Then if you need
    to support the reset menu, you should manually synchronize your component's "internal states" with its
    properties in this function. Once you implement this function, all the getset accessors of your component
    will not be called during reset operation. Which means that only the properties with default value
    will be reset by editor.

    This function is only called in editor mode.

    Zh

    onRestore 是用户在检查器菜单点击 Reset 时,对此组件执行撤消操作后调用的。

    如果组件包含了“内部状态”(不在 CCClass 属性中定义的临时成员变量),那么你可能需要实现该方法。

    编辑器执行撤销/重做操作时,将调用组件的 get set 来录制和还原组件的状态。 然而,在极端的情况下,它可能无法良好运作。
    那么你就应该实现这个方法,手动根据组件的属性同步“内部状态”。 一旦你实现这个方法,当用户撤销或重做时,组件的所有 get set 都不会再被调用。 这意味着仅仅指定了默认值的属性将被编辑器记录和还原。

    同样的,编辑可能无法在极端情况下正确地重置您的组件。
    于是如果你需要支持组件重置菜单,你需要在该方法中手工同步组件属性到“内部状态”。
    一旦你实现这个方法,组件的所有 get set 都不会在重置操作时被调用。 这意味着仅仅指定了默认值的属性将被编辑器重置。
    此方法仅在编辑器下会被调用。

    Returns void

  • 暂停当前音乐与音效的播放

    Returns void

  • 播放音效

    Parameters

    • url: string | AudioClip

      资源地址

    Returns void

  • 播放背景音乐

    Parameters

    • url: string

      资源地址

    • Optional callback: Function

      音乐播放完成事件

    Returns void

  • 循环播放背景音乐

    Parameters

    • url: string

    Returns void

  • 释放音效资源

    Parameters

    • url: string | AudioClip

    Returns void

  • En

    Called to initialize the component or node’s properties when adding the component the first time or when the Reset command is used. This function is only called in editor.

    Zh

    用来初始化组件或节点的一些属性,当该组件被第一次添加到节点上或用户点击了它的 Reset 菜单时调用。这个回调只会在编辑器下调用。

    Parameters

    • Optional didResetToDefault: boolean

    Returns void

  • 恢复当前暂停的音乐与音效播放

    Returns void

  • 保存音乐音效的音量、开关配置数据到本地

    Returns void

  • En

    Use Scheduler system to schedule a custom task.
    If the task is already scheduled, then the interval parameter will be updated without scheduling it again.

    Zh

    使用定时器系统调度一个自定义的回调任务。
    如果回调任务已调度,那么将不会重复调度它,只会更新时间间隔参数。

    Example

    import { log } from 'cc';
    this.schedule((dt) => void log(`time: ${dt}`), 1);

    Parameters

    • callback: any

      The callback function of the task

    • Optional interval: number

      The time interval between each invocation

    • Optional repeat: number

      The repeat count of this task, the task will be invoked (repeat + 1) times, use macro.REPEAT_FOREVER to repeat a task forever

    • Optional delay: number

      The delay time for the first invocation, Unit: s

    Returns void

  • En

    Use Scheduler system to schedule a task that runs only once, with a delay of 0 or larger.

    Zh

    使用定时器系统调度一个只运行一次的回调任务,可以指定 0 让回调函数在下一帧立即执行或者在一定的延时之后执行。

    Method

    scheduleOnce

    See

    schedule

    Example

    import { log } from 'cc';
    this.scheduleOnce((dt) => void log(`time: ${dt}`), 2);

    Parameters

    • callback: any

      The callback function of the task

    • Optional delay: number

      The delay time for the first invocation, Unit: s

    Returns void

  • 设置背景音乐播放完成回调

    Parameters

    • callback: null | Function = null

      背景音乐播放完成回调

    Returns void

  • Returns void

  • Returns void

  • En

    Called before all scripts' update if the Component is enabled the first time.
    Usually used to initialize some logic which need to be called after all components' onload methods called.
    This is a lifecycle method. It may not be implemented in the super class.
    You can only call its super class method inside it. It should not be called manually elsewhere.

    Zh

    如果该组件第一次启用,则在所有组件的 update 之前调用。通常用于需要在所有组件的 onLoad 初始化完毕后执行的逻辑。
    该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。

    Returns void

  • 停止当前音乐与音效的播放

    Returns void

  • 停止背景音乐播放

    Returns void

  • En

    Un-schedules a custom task.

    Zh

    取消调度一个自定义的回调任务。

    Example

    this.unschedule(_callback);
    

    Parameters

    • callback_fn: any

      The callback function of the task

    Returns void

  • En

    unschedule all scheduled tasks.

    Zh

    取消调度所有已调度的回调函数。

    Example

    this.unscheduleAllCallbacks();
    

    Returns void

  • En

    Update is called every frame, if the Component is enabled.
    This is a lifecycle method. It may not be implemented in the super class.
    You can only call its super class method inside it. It should not be called manually elsewhere.

    Zh

    如果该组件启用,则每帧调用 update。
    该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。

    Parameters

    • dt: number

      the delta time in seconds it took to complete the last frame

    Returns void

  • Returns void

Generated using TypeDoc