Introduction
In the karaoke scene, it is often necessary to pull multiple streams and display the user who is speaking. We need to identify whether the user is speaking, the volume of the speech (sound wave), and display it on the UI.
The SDK provides the following two methods to achieve this function:
Sound level refers to the volume of a stream. The ZEGO Express SDK provides the ability to capture the sound level of a stream in real time and delivers the captured sound level data to the app client through related callbacks. A typical use case of this feature is that you can visualize the sound level data on your app UI to indicate the current active speaker and their volume. The following picture shows an example of such use cases.
Audio spectrum refers to a series of values, each of which represents an audio signal's energy level at a point in a frequency range. The ZEGO Express SDK provides the ability to capture the audio spectrum data of a stream in real time and delivers the captured audio spectrum data to the app client through related callbacks. A typical use case of this feature is that you can use it to create and display an audio spectrum visualizer on your app UI, especially for those online karaoke apps. The following picture shows an example of audio spectrum visualizers.
Prerequisites
Before you begin to use sound level and audio spectrum in your project, make sure you complete the following steps:
- Create a project in ZEGOCLOUD Admin Console, and get the AppID of your project.
- The ZEGO Express SDK has been integrated into the project. For details, see Integration and Implementation.
Enable the sound level callbacks
To enable sound level callbacks, call the startSoundLevelMonitor
method.
/// Enable sound level callbacks.
///
- (void)startSoundLevelMonitor;
/// Enable the sound level monitor, supports enabling advanced features.
///
/// @note After enabled the monitor, the SDK sends out notification through the callback [onCapturedSoundLevelUpdate] to deliver the sound level data of locally captured audio, and through the callback [onRemoteSoundLevelUpdate] to deliver sound level data of remote streams.
/// @note You can call the [startPreview] method before entering a room, and together with the callback [onCapturedSoundLevelUpdate] to check whether the audio device is working properly.
/// @note The notification interval of the [onCapturedSoundLevelUpdate] and [onRemoteSoundLevelUpdate] callbacks is the value of the parameter.
///
/// @param config The configuration to enable sound level monitor.
- (void)startSoundLevelMonitorWithConfig:(ZegoSoundLevelConfig *)config;
After the above step is completed:
- When you start previewing the local video (by calling the
startPreview
method or publishing a local stream by calling thestartPublishingStream
method, the SDK will trigger theonCapturedSoundLevelUpdate
callback at an interval of 100ms to deliver the sound level data of the locally captured audio.
/// The callback to deliver the sound level data of locally captured audio.
///
/// @param soundLevel: The sound level value of the locally captured audio, which is in the range [0.0, 100.0].
- (void)onCapturedSoundLevelUpdate:(NSNumber *)soundLevel;
/// The callback to deliver the sound level data of locally captured audio.
///
/// @note To trigger this callback, you must call the [startSoundLevelMonitor] method to enable the sound level monitor.
/// @note The callback notification interval is the value set when [startSoundLevelMonitor] is called.
///
/// @param soundLevelInfo The sound level value of the locally captured audio, which is in the range [0.0, 100.0].
- (void)onCapturedSoundLevelInfoUpdate:(ZegoSoundLevelInfo *)soundLevelInfo;
- When you start playing remote streams by calling the
startPlayingStream
method, the SDK will trigger theonRemoteSoundLevelUpdate
callback at an interval of 100ms to deliver the sound level data of the remote streams.
/// The callback to deliver the sound level data of remote streams.
///
/// @param soundLevels: The sound level data (key-value pairs) of the remote streams, of which the key is a stream ID, and the value is the sound level value of the corresponding stream. Each sound level value is in the range [0.0, 100.0].
- (void)onRemoteSoundLevelUpdate:(NSDictionary<NSString *, NSNumber *> *)soundLevels;
/// The callback to deliver the sound level data of remote streams when stream playing.
///
/// @note To trigger this callback, you must call the [startSoundLevelMonitor] method to enable the sound level monitor when playing streams.
/// @note The callback notification interval is the value set when [startSoundLevelMonitor] is called.
///
/// @param soundLevelInfos The sound level data (key-value pairs) of the remote streams, of which the key is a stream ID, and the value is the sound level value of the corresponding stream. Each sound level value is in the range [0.0, 100.0].
- (void)onRemoteSoundLevelInfoUpdate:(NSDictionary<NSString *, ZegoSoundLevelInfo *> *)soundLevelInfos;
Enable the audio spectrum callbacks
To enable audio spectrum callbacks, call the startAudioSpectrumMonitor
method.
/// Enable audio spectrum callbacks
///
- (void)startAudioSpectrumMonitor;
After the above step is completed:
- When you start previewing the local video by calling the
startPreview
method or publishing a local stream by calling thestartPublishingStream
method, the SDK will trigger theonCapturedAudioSpectrumUpdate
callback at an interval of 100ms to deliver the audio spectrum data of locally captured audio.
/// The callback to deliver the audio spectrum data of locally captured audio.
///
/// @param audioSpectrum: An array of audio spectrum values of the locally captured audio. Each audio spectrum value is in the range [0, 2^30].
- (void)onCapturedAudioSpectrumUpdate:(NSArray<NSNumber *> *)audioSpectrum;
- When you start playing remote streams by calling the
startPlayingStream
method, the SDK will trigger theonRemoteAudioSpectrumUpdate
callback at an interval of 100ms to deliver audio spectrum data of the remote streams.
/// The callback to deliver the audio spectrum data of remote streams
///
/// @param audioSpectrums: The audio spectrum data (key-value pairs) of remote streams, of which the key is a stream ID, and the value is an array of audio spectrum values of the corresponding stream. Each audio spectrum value is in the range [0, 2^30].
- (void)onRemoteAudioSpectrumUpdate:(NSDictionary<NSString *, NSArray<NSNumber *> *> *)audioSpectrums;
Obtain data from the callbacks
The SDK delivers the sound level data and audio spectrum data of the remote streams as key-value pairs in an NSDictionary
. In each key-value pair, the key is the stream ID of a remote stream published by another user in the same room, and the value is the sound level value or an array of audio spectrum values that stream.
You can first obtain and save the list of remote streams published by the other users in the current room through the onRoomStreamUpdate
callback, and then use each stream ID in the list as an index to get the sound level value or the audio spectrum data of that stream from the NSDictionary
.
The following examples show how to obtain the sound level and audio spectrum data from the callbacks.
// The callback to deliver the sound level data of locally captured audio.
- (void)onCapturedSoundLevelUpdate:(NSNumber *)soundLevel {
ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.soundLevel = soundLevel;
}
// The callback to deliver the sound level data (key-value pairs) of remote streams. In each key-value pair, the key is the stream ID of a remote stream, and the value is the sound level data of that stream.
- (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];
}
}
}
// The callback to deliver the audio spectrum data of locally captured audio.
- (void)onCapturedAudioSpectrumUpdate:(NSArray<NSNumber *> *)audioSpectrum {
ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.spectrumList = audioSpectrum;
}
// The callback to deliver the audio spectrum data (key-value pairs) of the remote streams. Audio spectrum callback for remote streams. In each key-value pair, the key is the stream ID of a remote stream, and the value is the audio spectrum data of that stream.
- (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];
}
}
}
Disable the sound level callbacks
To disable the sound level callbacks, call the stopSoundLevelMonitor
method.
/// Disable sound level callbacks
- (void)stopSoundLevelMonitor;
After the above step is completed, the SDK will stop triggering the callbacks onCapturedSoundLevelUpdate
and onRemoteSoundLevelUpdate
.
Disable the audio spectrum callbacks
To disable audio spectrum callbacks, call the stopAudioSpectrumMonitor
method.
/// Stop listening for audio spectrum callbacks
///
- (void)stopAudioSpectrumMonitor;
After the above step is completed, the SDK will stop triggering the callbacks onCapturedAudioSpectrumUpdate
and onRemoteAudioSpectrumUpdate
.
Top comments (0)