TrackMixingManager 混音模块
关于混音功能的使用方法请阅读文档指南中的 音乐混音 一节。
Track 模式下的混音模块可以通过 createAudioMixingManagerFromTrack 来创建。这个函数需要手动传入一个 Track 对象作为混音的音源(一般是来自麦克风的人声)。混音模块的工作内容就是将各种类型的音乐(背景音乐,音效)和传入的音源(人声)混合后,输出一路新的 Track。
混音模块输出的 Track 就是 Track 模式下的媒体对象,使用方式和和其他正常采集下来的 Track 并无多大差异,可以直接被用于发布。
混音模块不会改动输入的原始音源(人声)对象,所以模块输出的 Track 是一个独立于输入的新 Track 对象。在 Track 模式下,输入和输出的 2 个 Track 可以根据业务需要同时被使用(比如发布)。
source
Track
类型:创建混音模块时提供输入音源对象,一般是采集下来的人声 Track
outputTrack
Track
类型:混音模块的输出
musicManager
AudioMusicManager
类型:用来控制混音背景音乐的子模块,下面会详细介绍
effectManager
AudioEffectManager
类型:用来控制混音背景音效的子模块,下面会详细介绍
setBitrate(bitrate)
number
bitrate: 设置混音模块输出 Track 的码率,单位 kbps
,只在将输出发布前设置有效
setTag(tag)
string
tag: 设置混音模块输出 Track 的 tag,只在将输出发布前设置有效
getSourcePlaybackState()
boolean
返回: 获取当前是否打开了输入音源返听,默认关闭
setSourcePlaybackState(state)
boolean
state: 设置是否打开输入音源返听,默认关闭
getMusicPlaybackState()
boolean
返回: 获取当前是否打开了背景音乐返听,默认打开
setMusicPlaybackState(state)
boolean
state: 设置是否打开背景音乐返听,默认打开
getEffectPlaybackState()
boolean
返回: 获取当前是否打开了音效返听,默认打开
setEffectPlaybackState(state)
boolean
state: 设置是否打开音效返听,默认打开
release()
释放整个混音模块,清除 输出/音乐/音效等资源。但是不会释放初始传入的原始音源对象
AudioMusicManager 背景音乐子模块
通过访问混音模块的 musicManager
对象来操作这个背景音乐子模块。
musicOption
AudioMusicOption
类型: 背景音乐混音的当前配置,AudioMusicOption
的细节见下:
AudioMusicOption {
loop: boolean; // 是否循环播放背景音乐,默认为 fakse
playbackVolume: number; // 本地播放时的音量,默认为 1,表示原始音量
remoteVolume: number; // 混音后在远端播放时的音量,默认为 1,表示原始音量
}
musicTrack
AudioSourceTrack | undefined
类型: 背景音乐的媒体对象,只有当调用 startMusicMixing 开始背景音乐混音后才能访问到这个对象
setMusicOption(option)
Partical<AudioMusicOption>
option: 设置背景音乐混音的配置,完整的配置可以参考 musicOption,允许在混音的中途设置来动态更新
// 打开背景音乐循环播放
trackMixingManager.musicManager.setMusicOption({ loop: true });
startMusicMixing(source)
string
| File | AudioBuffer
source: Promise<void>
返回: 开始背景音乐混音,source
用来标记 SDK 如何获取背景音乐,支持 在线URL、本地文件 或者 音频 Buffer
当该方法调用结束后,就能访问到 musicTrack 对象了,如果你想获取背景音乐的各种音频回调数据,可以通过 musicTrack 下的方法来获取。
pauseMusicMixing()
暂停背景音乐
resumeMusicMixing()
恢复背景音乐
stopMusicMixing()
停止背景音乐
getMusicDuration()
number
返回:获取当前背景音乐的时长,单位 秒
如果传入的背景音乐是在线背景音乐,时长的获取可能根据网络情况的不同会有延迟。可以配合下文中的 music-state-change 事件,当背景音乐第一次处于
PLAY
状态时,就能安全地获取到时长了。
getMusicCurrentTime()
number
返回: 获取当前背景音乐的播放时间,单位 秒。可以使用 setInterval
等定时器来定期获取更新这个值,以实现播放进度条等功能。
setMusicCurrentTime(time)
number
time: 设置当前背景音乐的播放时间,单位 秒,可以用来实现播放跳转等功能。
事件列表
music-state-change
当背景音乐的处理状态发生变化时,会触发这个事件,全部状态见下
AudioSourceState {
IDLE = "idle", // 表示还没有开始处理播放背景音乐,初始状态
LOADING = "loading", // 在处理在线背景音乐的时候会触发,表示还在加载
PLAY = "play", // 表示背景音乐在正常处理播放
PAUSE = "pause", // 表示背景音乐被暂停
END = "end", // 表示背景音乐播放结束
}
示例:
trackMixingManager.musicManager.on("music-state-change", (currState, lastState) => {
console.log(`music state change, ${lastState} -> ${currState}`);
});
AudioEffectManager 音效子模块
通过访问混音模块的 effectManager
对象来操作这个音效子模块。音效和背景不同,它支持同时处理多个不同的音源,需要在添加音源的时候指定 key
字段,然后通过提供 key
字段来播放音源。另一方面,音效模块没有提供像背景音乐那样的流程控制 API(暂停,获取时长和进度等),只是单纯地播放。
effectList
string[]
类型: 所有已经添加的音效的 key
addEffectSource(source, key)
添加指定的音效
string
| File | AudioBuffer
source: 用来标记 SDK 如何获取音效,支持 在线URL、本地文件 或者 音频 Buffer
string
key: 指定这个音效的 key
,用于之后的播放、获取等操作
Promise<void>
返回: playEffect(key, volume)
指定一个 key
播放音效
string
key: 添加音效时指定的 key
number
可选
volume: 播放时的音量,1 代表原始音量
getEffectTrack(key)
指定一个 key
获取这个对于音效的音源媒体对象
string
key: 添加音效时指定的 key
AudioSourceTrack | undefined
返回: 因为音效模块本身没有封装暂停、时长、状态回调、音频数据回调等方法,如果有特殊的需求可以直接通过这个方法获取到相应的音效媒体对象,直接调用 AudioSourceTrack 下的方法来实现
removeEffectSource(key)
string
可选
key: 指定一个 key
移除并释放这个音效,如果没有指定 key
,默认清空所有音效。