Skip to content

音效 ​

索引 ​

接口描述
DisableOriginMusic客户端停止原生背景音乐
PlayCustomMusic客户端播放场景音效,包括原版音效及自定义音效
PlayCustomUIMusic客户端播放UI音效,包括原版音效及自定义音效
PlayGlobalCustomMusic客户端播放背景音乐
SetCustomMusicLoop客户端设定指定音乐是否循环播放,包括场景音效与背景音乐
SetCustomMusicLoopById客户端设定指定音乐是否循环播放
StopCustomMusic客户端停止音效,包括场景音效与背景音乐,将依据fadeOutTime触发OnMusicStopClientEvent事件
StopCustomMusicById客户端停止场景音效

DisableOriginMusic ​

客户端背景音乐 原生音乐 播放停止开发补充

停止原生背景音乐。

客户端

method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient

  • 描述

停止原生背景音乐

  • 参数
参数名数据类型说明
disableboolTrue表示停止,False表示恢复
  • 返回值
数据类型说明
bool设置是否成功
  • 示例
python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
comp.DisableOriginMusic(True)

PlayCustomMusic ​

客户端场景音效 位置播放 循环播放 实体绑定开发补充

在客户端按位置或绑定实体播放场景音效;保存返回的播放实例 ID 以便后续停止或控制循环。

  • 资源需配置 sound_definitions.json;不是服务端向任意玩家直接发声的接口,远端触发需先通知目标客户端。
  • 绑定实体时 pos 为相对坐标;距离、流式音频和失败 ID 的限制见原文。
  • 未绑定实体且距离本地玩家超过 16 格会跳过;流式音频多次调用会从头播放,不能同时开多个实例。
  • 名字带 CustomMusic 但也支持原版场景音效;接口仅在客户端播放,服务端定向请求需通知对应客户端。

关联接口:NotifyToClient (server)

客户端

method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient

  • 描述

播放场景音效,包括原版音效及自定义音效

  • 参数
参数名数据类型说明
namestr音效名称
postuple(float,float,float)播放位置
volumefloat音量倍率,范围0-1,与json中的volume乘算后为游戏中实际播放的音量大小
pitchfloat播放速度,范围0-256,1表示原速,可以从json文件里进行修改
loopbool是否循环播放
entityIdstr绑定的实体id,默认为None,若有绑定的实体,则pos参数为相对于实体的坐标
  • 返回值
数据类型说明
str音乐播放id,可用于控制音乐的停止和循环
  • 备注

  • 当播放音乐错误时,将返回相应的错误id

错误错误描述
-1无法在服务端播放音效
-2播放位置错误
-3已有同名背景音乐正在播放
-4播放失败
-5绑定实体不存在
-6位置距离本地玩家距离大于16格,跳过音效播放(绑定实体的音效不受此限制)
  • 当背景音乐在播放的时候,请不要播放同名的场景音效

  • 针对比较大的音乐,请将load_on_low_memory和stream设为true。但使用stream的音频无法同时播放两个音效实例,多次调用的效果是从头播放音频(不使用stream的音频,多次调用会同时播放多个)

  • 该接口触发PlaySoundClientEvent

  • 与示例对应的sound_definitions.json配置:

{
  "sound001": {
    "min_distance": 0,
    "max_distance": 20.0,
    "sounds": [
      {
        "name": "sounds/testaudio/largeBlast1",
        "load_on_low_memory": true,
        "stream": true,
        "pitch": 1,
        "volume": 1
      }
    ]
  }
}
  • 示例
python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
musicId = comp.PlayCustomMusic("music.global0", (1,1,1), 1, 1, False, None)

PlayCustomUIMusic ​

客户端界面音效 声音播放 音效资源开发补充

播放原版或自定义界面音效。

客户端

method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient

  • 描述

播放UI音效,包括原版音效及自定义音效

  • 参数
参数名数据类型说明
namestr音效名称
volumefloat音量倍率,范围0-1,与json中的volume乘算后为游戏中实际播放的音量大小
pitchfloat播放速度,范围0-256,1表示原速,可以从json文件里进行修改
loopbool是否循环播放
  • 返回值
数据类型说明
str音乐播放id,可用于控制音乐的停止和循环
  • 备注

  • 当播放音乐错误时,将返回相应的错误id

错误错误描述
-1无法在服务端播放音效
-2播放失败
-3已有同名背景音乐正在播放
  • 当背景音乐在播放的时候,请不要播放同名的场景音效

  • 该接口触发PlaySoundClientEvent

  • 示例

python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
musicId = comp.PlayCustomUIMusic("mob.cat.beg", 1, 1, False)

PlayGlobalCustomMusic ​

客户端背景音乐 声音播放 音频资源开发补充

播放背景音乐。

客户端

method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient

  • 描述

播放背景音乐

  • 参数
参数名数据类型说明
namestr音乐名称
volumefloat音量大小,范围0-1,与json中的volume乘算后为游戏中实际播放的音量大小
loopbool是否循环播放
  • 返回值
数据类型说明
bool播放是否成功
  • 备注

  • 背景音乐同时仅能播放一个,当播放背景音乐时,会停止掉当前播放的音乐。这个规则也包括原版的背景音乐,所以为了避免原版背景音乐覆盖掉自定义背景音乐,建议先使用DisableOriginMusic屏蔽原版背景音乐。

  • 背景音乐的优先级大于场景音效,当背景音乐在播放时,会让同名场景音效播放失败

  • 背景音乐的播放速度需要从配置json里进行修改

  • 针对比较大的背景音乐,请将load_on_low_memory和stream设为true

  • 背景音乐需要在json里定义category为music

  • 该接口触发PlayMusicClientEvent

  • 与示例对应的sound_definitions.json配置:

{
  "Gsound001": {
    "category": "music",
    "sounds": [
      {
        "name": "sounds/testaudio/Music001",
        "load_on_low_memory": true,
        "stream": true,
        "pitch": 1,
        "volume": 1
      }
    ]
  }
}
  • 示例
python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
comp.PlayGlobalCustomMusic("Gsound001", 1, False)

SetCustomMusicLoop ​

客户端声音播放 循环开关 播放控制开发补充

按音乐设置循环播放状态。

客户端

method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient

  • 描述

设定指定音乐是否循环播放,包括场景音效与背景音乐

  • 参数
参数名数据类型说明
namestr音乐名称
loopboolTrue则循环播放,False则停止循环,停止会持续到播放到本次结束
  • 返回值
数据类型说明
bool停止是否成功
  • 示例
python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
comp.SetCustomMusicLoop("soundName", True)

SetCustomMusicLoopById ​

客户端声音播放 循环开关 实例控制开发补充

按播放实例标识控制循环播放。

客户端

method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient

  • 描述

设定指定音乐是否循环播放

  • 参数
参数名数据类型说明
musicIdstr音乐id,播放指定音乐获取的音乐id
loopboolTrue则循环播放,False则停止循环,停止会持续到播放到本次结束
  • 返回值
数据类型说明
bool停止是否成功
  • 示例
python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
musicId = comp.PlayCustomMusic("sound001", (1,1,1), 1, 1, False, False)
comp.SetCustomMusicLoopById(musicId, True)

StopCustomMusic ​

客户端声音停止 淡出控制 停止事件开发补充

停止指定音效或背景音乐,并按淡出时间触发停止事件。

客户端

method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient

  • 描述

停止音效,包括场景音效与背景音乐,将依据fadeOutTime触发OnMusicStopClientEvent事件

  • 参数
参数名数据类型说明
namestr音乐名称
fadeOutTimefloat停止的淡出时间,单位为秒,如果剩余时间小于淡出时间,将以剩余时间为准
  • 返回值
数据类型说明
bool停止是否成功
  • 示例
python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
comp.StopCustomMusic("music.global0", 0)

StopCustomMusicById ​

客户端声音停止 实例控制 场景音效开发补充

按播放实例标识停止场景音效。

客户端

method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient

  • 描述

停止场景音效

  • 参数
参数名数据类型说明
musicIdstr音乐id,播放指定音乐获取的音乐id
fadeOutTimefloat停止的淡出时间,单位为秒
  • 返回值
数据类型说明
bool停止是否成功
  • 备注

  • 使用PlayCustomMusic播放音效或音乐后,7帧内再使用StopCustomMusicById接口停止播放该musicId音乐/音效,会有概率停止失败,且后续再使用StopCustomMusicById接口停止播放该musicId音乐/音效无效。因此在上述情况下,建议开发者使用计时器延迟15帧以上执行该接口

  • 示例

python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
musicId = comp.PlayCustomMusic("sound001", (1,1,1), 1, 1, False, False)
comp.StopCustomMusicById(musicId, 0)