音效
索引
| 接口 | 描述 | |
|---|---|---|
| DisableOriginMusic | 客户端 | 停止原生背景音乐 |
| PlayCustomMusic | 客户端 | 播放场景音效,包括原版音效及自定义音效 |
| PlayCustomUIMusic | 客户端 | 播放UI音效,包括原版音效及自定义音效 |
| PlayGlobalCustomMusic | 客户端 | 播放背景音乐 |
| SetCustomMusicLoop | 客户端 | 设定指定音乐是否循环播放,包括场景音效与背景音乐 |
| SetCustomMusicLoopById | 客户端 | 设定指定音乐是否循环播放 |
| StopCustomMusic | 客户端 | 停止音效,包括场景音效与背景音乐,将依据fadeOutTime触发OnMusicStopClientEvent事件 |
| StopCustomMusicById | 客户端 | 停止场景音效 |
DisableOriginMusic
客户端开发补充
停止原生背景音乐。
客户端
method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient
- 描述
停止原生背景音乐
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| disable | bool | True表示停止,False表示恢复 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | 设置是否成功 |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
comp.DisableOriginMusic(True)PlayCustomMusic
客户端开发补充
在客户端按位置或绑定实体播放场景音效;保存返回的播放实例 ID 以便后续停止或控制循环。
- 资源需配置 sound_definitions.json;不是服务端向任意玩家直接发声的接口,远端触发需先通知目标客户端。
- 绑定实体时 pos 为相对坐标;距离、流式音频和失败 ID 的限制见原文。
- 未绑定实体且距离本地玩家超过 16 格会跳过;流式音频多次调用会从头播放,不能同时开多个实例。
- 名字带 CustomMusic 但也支持原版场景音效;接口仅在客户端播放,服务端定向请求需通知对应客户端。
客户端
method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient
- 描述
播放场景音效,包括原版音效及自定义音效
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| name | str | 音效名称 |
| pos | tuple(float,float,float) | 播放位置 |
| volume | float | 音量倍率,范围0-1,与json中的volume乘算后为游戏中实际播放的音量大小 |
| pitch | float | 播放速度,范围0-256,1表示原速,可以从json文件里进行修改 |
| loop | bool | 是否循环播放 |
| entityId | str | 绑定的实体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
}
]
}
}- 示例
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音效,包括原版音效及自定义音效
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| name | str | 音效名称 |
| volume | float | 音量倍率,范围0-1,与json中的volume乘算后为游戏中实际播放的音量大小 |
| pitch | float | 播放速度,范围0-256,1表示原速,可以从json文件里进行修改 |
| loop | bool | 是否循环播放 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| str | 音乐播放id,可用于控制音乐的停止和循环 |
备注
当播放音乐错误时,将返回相应的错误id
| 错误 | 错误描述 |
|---|---|
| -1 | 无法在服务端播放音效 |
| -2 | 播放失败 |
| -3 | 已有同名背景音乐正在播放 |
当背景音乐在播放的时候,请不要播放同名的场景音效
该接口触发PlaySoundClientEvent
示例
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
- 描述
播放背景音乐
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| name | str | 音乐名称 |
| volume | float | 音量大小,范围0-1,与json中的volume乘算后为游戏中实际播放的音量大小 |
| loop | bool | 是否循环播放 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| 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
}
]
}
}- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
comp.PlayGlobalCustomMusic("Gsound001", 1, False)SetCustomMusicLoop
客户端开发补充
按音乐设置循环播放状态。
客户端
method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient
- 描述
设定指定音乐是否循环播放,包括场景音效与背景音乐
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| name | str | 音乐名称 |
| loop | bool | True则循环播放,False则停止循环,停止会持续到播放到本次结束 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | 停止是否成功 |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
comp.SetCustomMusicLoop("soundName", True)SetCustomMusicLoopById
客户端开发补充
按播放实例标识控制循环播放。
客户端
method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient
- 描述
设定指定音乐是否循环播放
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| musicId | str | 音乐id,播放指定音乐获取的音乐id |
| loop | bool | True则循环播放,False则停止循环,停止会持续到播放到本次结束 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | 停止是否成功 |
- 示例
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事件
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| name | str | 音乐名称 |
| fadeOutTime | float | 停止的淡出时间,单位为秒,如果剩余时间小于淡出时间,将以剩余时间为准 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | 停止是否成功 |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateCustomAudio(levelId)
comp.StopCustomMusic("music.global0", 0)StopCustomMusicById
客户端开发补充
按播放实例标识停止场景音效。
客户端
method in mod.client.component.audioCustomCompClient.AudioCustomComponentClient
- 描述
停止场景音效
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| musicId | str | 音乐id,播放指定音乐获取的音乐id |
| fadeOutTime | float | 停止的淡出时间,单位为秒 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | 停止是否成功 |
备注
使用PlayCustomMusic播放音效或音乐后,7帧内再使用StopCustomMusicById接口停止播放该musicId音乐/音效,会有概率停止失败,且后续再使用StopCustomMusicById接口停止播放该musicId音乐/音效无效。因此在上述情况下,建议开发者使用计时器延迟15帧以上执行该接口
示例
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)