Class AudioEffect

游戏音效

Hierarchy

  • AudioSource
    • AudioEffect

Constructors

  • Parameters

    • Optional name: string

    Returns AudioEffect

Properties

__editorExtras__: unknown
__prefab: null | CompPrefabInfo
_clip: null | AudioClip
_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.

_loop: boolean
_name: string
_objFlags: number
_playOnAwake: boolean
_player: null | _pal_audio__AudioPlayer
_progress: number = 0
_sceneGetter: null | (() => RenderScene)
_volume: number
effects: Map<string, AudioClip> = ...
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);
AudioState: typeof _pal_audio_type__AudioState
EventHandler: typeof EventHandler
EventType: typeof _cocos_audio_audio_source__AudioSourceEventType
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 clip(): null | AudioClip
  • Returns null | AudioClip

  • set clip(val: null | AudioClip): void
  • En

    The default AudioClip to be played for this audio source.

    Zh

    设定要播放的音频。

    Parameters

    • val: null | AudioClip

    Returns void

  • get currentTime(): number
  • En

    Get the current playback time, in seconds.

    Zh

    以秒为单位获取当前播放时间。

    Returns number

  • set currentTime(num: number): void
  • En

    Set current playback time, in seconds.

    Zh

    以秒为单位设置当前播放时间。

    Parameters

    • num: number

      playback time to jump to.

    Returns void

  • get duration(): number
  • En

    Get the audio duration, in seconds.

    Zh

    获取以秒为单位的音频总时长。

    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 loop(): boolean
  • Returns boolean

  • set loop(val: boolean): void
  • En

    Is looping enabled for this audio source?

    Zh

    是否循环播放音频?

    Parameters

    • val: boolean

    Returns void

  • get name(): string
  • Returns string

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

    • value: string

    Returns void

  • get playOnAwake(): boolean
  • Returns boolean

  • set playOnAwake(val: boolean): void
  • En

    Is the autoplay enabled?
    Note that for most platform autoplay will only start
    after a user gesture is received, according to the latest autoplay policy:
    https://www.chromium.org/audio-video/autoplay

    Zh

    是否启用自动播放。
    请注意,根据最新的自动播放策略,现在对大多数平台,自动播放只会在第一次收到用户输入后生效。
    参考:https://www.chromium.org/audio-video/autoplay

    Parameters

    • val: boolean

    Returns void

  • get playing(): boolean
  • En

    Is the audio currently playing?

    Zh

    当前音频是否正在播放?

    Returns boolean

  • get progress(): number
  • 获取音乐播放进度

    Returns number

  • set progress(value: number): void
  • 设置音乐当前播放进度

    Parameters

    • value: number

      进度百分比0到1之间

    Returns void

  • get state(): _pal_audio_type__AudioState
  • En

    Get current audio state.

    Zh

    获取当前音频状态。

    Returns _pal_audio_type__AudioState

  • get uuid(): string
  • En

    The uuid for editor.

    Zh

    组件的 uuid,用于编辑器。

    Read Only

    Example

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

    Returns string

  • get volume(): number
  • Returns number

  • set volume(val: number): void
  • En

    The volume of this audio source (0.0 to 1.0).
    Note: Volume control may be ineffective on some platforms.

    Zh

    音频的音量(大小范围为 0.0 到 1.0)。
    请注意,在某些平台上,音量控制可能不起效。

    Parameters

    • val: number

    Returns void

  • get maxAudioChannel(): number
  • Returns number

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

  • 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

    Get PCM data from specified channel. Currently it is only available in Native platform and Web Audio (including Web and ByteDance platforms).

    Zh

    通过指定的通道获取音频的 PCM data。 目前仅在原生平台和 Web Audio(包括 Web 和 字节平台)中可用。

    Returns

    A Promise to get the PCM data after audio is loaded.

    Example

    audioSource.getPCMData(0).then(dataView => {
    if (!dataView) return;
    for (let i = 0; i < dataView.length; ++i) {
    console.log('data: ' + dataView.getData(i));
    }
    });

    Parameters

    • channelIndex: number

      The channel index. 0 is left channel, 1 is right channel.

    Returns Promise<undefined | AudioPCMDataView>

  • En

    Get the sample rate of audio. Currently it is only available in Native platform and Web Audio (including Web and ByteDance platforms).

    Zh

    获取音频的采样率。 目前仅在原生平台和 Web Audio(包括 Web 和 字节平台)中可用。

    Returns

    A Promise to get the sample rate after audio is loaded.

    Returns Promise<number>

  • 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

  • 加载音效并播放

    Parameters

    • url: string | AudioClip

      音效资源地址

    • Optional callback: Function

      资源加载完成并开始播放回调

    Returns void

  • Returns void

  • Returns void

  • Returns void

  • Returns void

  • 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

  • En

    Pause the clip.

    Zh

    暂停播放。

    Returns void

  • En

    Play the clip.
    Restart if already playing.
    Resume if paused.

    NOTE: On Web platforms, the Auto Play Policy bans auto playing audios at the first time, because the user gesture is required. there are 2 ways to play audios at the first time:

    • play audios in the callback of TOUCH_END or MOUSE_UP event
    • play audios straightly, the engine will auto play audios at the next user gesture.

    Zh

    开始播放。
    如果音频处于正在播放状态,将会重新开始播放音频。
    如果音频处于暂停状态,则会继续播放音频。

    注意:在 Web 平台,Auto Play Policy 禁止首次自动播放音频,因为需要发生用户交互之后才能播放音频。 有两种方式实现音频首次自动播放:

    • 在 TOUCH_END 或者 MOUSE_UP 的事件回调里播放音频。
    • 直接播放音频,引擎会在下一次发生用户交互时自动播放。

    Returns void

  • En

    Plays an AudioClip, and scales volume by volumeScale. The result volume is audioSource.volume * volumeScale.

    Zh

    以指定音量倍数播放一个音频一次。最终播放的音量为 audioSource.volume * volumeScale

    Parameters

    • clip: AudioClip

      The audio clip to be played.

    • Optional volumeScale: number

      volume scaling factor wrt. current value.

    Returns void

  • 释放指定地址音效资源

    Parameters

    • url: string | AudioClip

      音效资源地址

    Returns void

  • 释放所有已使用过的音效资源

    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

  • 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

  • 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

  • En

    Stop the clip.

    Zh

    停止播放。

    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