概念 | 描述 | 应用场景 | 场景图 |
---|---|---|---|
音量变化 |
指某条流的音量大小,下文简称为“声浪”。 |
在推拉流过程中,判断麦上的用户谁在说话,并做 UI 展示。 |
![]() |
音频频谱 |
指数字音频信号在各频点的能量值。 |
在主播 K 歌场景中,已经推流或拉流的前提下,让主播或观众看到音调与音量变化的动画。 |
![]() |
请参考 下载示例源码 获取源码。
相关源码请查看 “/ZegoExpressExample/Examples/AdvancedAudioProcessing/SoundLevel” 目录下的文件。
在实现声浪与音频频谱功能之前,请确保:
接口原型
本地采集的声浪回调接口 onCapturedSoundLevelUpdate、onCapturedSoundLevelInfoUpdate:
// 本地采集音频声浪回调
//
// @param soundLevel 本地采集的声浪值,取值范围为 0.0 ~ 100.0
- (void)onCapturedSoundLevelUpdate:(NSNumber *)soundLevel;
// 本地采集音频声浪回调,支持人声检测
//
// @note 想要触发这个回调,必须调用 [startSoundLevelMonitor] 函数启动声浪监控器。
// @note 回调通知周期为调用 [startSoundLevelMonitor] 时设置的参数值。
//
// @param soundLevelInfo 本地采集的声浪值,取值范围为 0.0 ~ 100.0
- (void)onCapturedSoundLevelInfoUpdate:(ZegoSoundLevelInfo *)soundLevelInfo;
远端音频声浪回调接口 onRemoteSoundLevelUpdate、onRemoteSoundLevelInfoUpdate:
// 远端拉流音频声浪回调
//
// @param soundLevels 远端的声浪键值对,key 为流 ID,value 为对应的流的声浪值,value 取值范围为 0.0 ~ 100.0
- (void)onRemoteSoundLevelUpdate:(NSDictionary<NSString *, NSNumber *> *)soundLevels;
// 远端拉流音频声浪回调,支持人声检测
//
// @note 想要触发这个回调,必须调用 [startSoundLevelMonitor] 函数启动声浪监控器,且处于正在拉流的状态。
// @note 回调通知周期为调用 [startSoundLevelMonitor] 时设置的参数值。
//
// @param soundLevelInfos 远端的声浪键值对,key 为流 ID,value 为对应的流的声浪值,value 取值范围为 0.0 ~ 100.0
- (void)onRemoteSoundLevelInfoUpdate:(NSDictionary<NSString *, ZegoSoundLevelInfo *> *)soundLevelInfos;
本地采集的音频频谱回调接口 onCapturedAudioSpectrumUpdate:
// 本地采集音频频谱回调
//
// @param audioSpectrum 本地采集的音频频谱值数组,频谱值范围为 [0-2^30]
- (void)onCapturedAudioSpectrumUpdate:(NSArray<NSNumber *> *)audioSpectrum;
-
远端拉流音频频谱回调接口 onRemoteAudioSpectrumUpdate:
// 远端拉流音频频谱回调
//
// @param audioSpectrums 远端音频频谱键值对,key 是流 ID,value 为对应的流的音频频谱值数组,频谱值范围为 [0-2^30]
- (void)onRemoteAudioSpectrumUpdate:(NSDictionary<NSString *, NSArray<NSNumber *> *> *)audioSpectrums;
调用示例
远端拉流声浪和远端音频频谱的回调给的是 NSDictionary
,key
是当前房间内正在推流的其他用户的流 ID,value
是对应这条流的声浪/音频频谱数据。
可先通过 onRoomStreamUpdate 回调方法获取到当前房间内存在的流列表,并保存起来,然后通过保存的流列表来索引 NSDictionary 取得每条流对应的声浪/音频频谱数据。
以下示例将演示如何从回调方法中获取到声浪/音频频谱的数据并传递给 UI。具体上下文请参考示例源码中 “/ZegoExpressExample/Examples/AdvancedAudioProcessing/SoundLevel” 目录下的文件。
// 本地采集音频声浪回调
- (void)onCapturedSoundLevelUpdate:(NSNumber *)soundLevel {
ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.soundLevel = soundLevel;
}
// 本地采集音频声浪回调,支持人声检测
- (void)onCapturedSoundLevelInfoUpdate:(ZegoSoundLevelInfo *)soundLevelInfo {
ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.soundLevelInfo = soundLevelInfo;
}
// soundLevels 远端的声浪键值对,key 为流 ID,value 为对应的流的声浪值
- (void)onRemoteSoundLevelUpdate:(NSDictionary<NSString *,NSNumber *> *)soundLevels {
NSInteger rowCount = [self.tableView numberOfRowsInSection:1];
for (NSInteger row = 0; row < rowCount; row++) {
ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:1]];
if ([soundLevels objectForKey:cell.streamID]) {
cell.soundLevel = soundLevels[cell.streamID];
}
}
}
// soundLevelInfos 远端的声浪键值对,key 为流 ID,value 为对应的流的声浪值
- (void)onRemoteSoundLevelInfoUpdate:(NSDictionary<NSString *, ZegoSoundLevelInfo *> *)soundLevelInfos {
NSInteger rowCount = [self.tableView numberOfRowsInSection:1];
for (NSInteger row = 0; row < rowCount; row++) {
ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:1]];
if ([soundLevelInfos objectForKey:cell.streamID]) {
cell. soundLevelInfos = soundLevelInfos[cell.streamID];
}
}
}
// 本地采集音频频谱回调
- (void)onCapturedAudioSpectrumUpdate:(NSArray<NSNumber *> *)audioSpectrum {
ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.spectrumList = audioSpectrum;
}
// audioSpectrums 远端音频频谱键值对,key 是流 ID,value 为对应的流的音频频谱值数组
- (void)onRemoteAudioSpectrumUpdate:(NSDictionary<NSString *,NSArray<NSNumber *> *> *)audioSpectrums {
NSInteger rowCount = [self.tableView numberOfRowsInSection:1];
for (NSInteger row = 0; row < rowCount; row++) {
ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:1]];
if ([audioSpectrums objectForKey:cell.streamID]) {
cell.spectrumList = audioSpectrums[cell.streamID];
}
}
}
可分别针对声浪或音频频谱,启动调用监听对应回调的接口。
调用 startSoundLevelMonitor 接口启动声浪的监听:
// 启动声浪监控
//
- (void)startSoundLevelMonitor;
// 启动声浪监控,支持开启进阶功能
//
// @note 启动监控后可通过 [onCapturedSoundLevelUpdate] 回调接收本地采集音频声浪回调,以及 [onRemoteSoundLevelUpdate] 回调接收远端拉流音频声浪回调。
// @note 开发者可在进入房间之前,调用 [startPreview] 此函数,并与 [onCapturedSoundLevelUpdate] 结合来判断音频设备是否正常工作。
// @note [onCapturedSoundLevelUpdate] 与 [onRemoteSoundLevelUpdate] 回调通知周期为参数设置的值。
//
// @param config 启动声浪监控的配置
- (void)startSoundLevelMonitorWithConfig:(ZegoSoundLevelConfig *)config;
在调用上述接口之后,onCapturedSoundLevelUpdate 回调方法会立刻触发,未推流且未预览时回调值为 0;onRemoteSoundLevelUpdate 需要在调用 startPlayingStream 开始拉流接口之后,才会有回调。
调用 startAudioSpectrumMonitor 接口启动音频频谱的监听:
// 启动音频频谱监控
//
- (void)startAudioSpectrumMonitor;
在调用上述接口之后,onCapturedAudioSpectrumUpdate 回调方法会立刻触发,未推流且未预览时回调值为 0;onRemoteAudioSpectrumUpdate 需要在调用 startPlayingStream 开始拉流接口之后,才会有回调。
可分别针对声浪或音频频谱,停止调用监听对应回调的开关。
调用 stopSoundLevelMonitor 接口停止声浪的监听:
// 停止声浪监控
//
- (void)stopSoundLevelMonitor;
在调用上述接口之后,onCapturedSoundLevelUpdate 与 onRemoteSoundLevelUpdate 不再回调。
调用 stopAudioSpectrumMonitor 接口停止音频频谱的监听:
// 停止音频频谱监控
//
- (void)stopAudioSpectrumMonitor;
在调用上述接口之后,onCapturedAudioSpectrumUpdate 与 onRemoteAudioSpectrumUpdate 将不再回调。
开启了声浪和音频频谱的监控开关之后,为什么没有收到相关回调?
本地采集的回调会立刻触发,未推流时的回调值为 0;远端拉流的回调在拉流 startPlayingStream 成功之后才会触发。
联系我们
文档反馈