I have a WebRTC iOS application.
There I have AVAudioSession
and RTCAudioSource
.
I need to detect when a microphone starts receiving loud sounds (like when a person starts speaking) similar to what hark does in the browser with AudioContext.
How can I detect it or get something that resembles stream that can be measured like AVCaptureAudioChannel
or AVCaptureAudioDataOutput
?
-
4Assuming that you will be making webrtc calls, why don't you consider using peerconnection->getStats() method and use `audioInputLevel` stats parameter? This parameter gives input audio level and you can use the same to know the intensity of the sound. – manishg Aug 20 '17 at 01:51
-
2@manishg unfortunately I don't have this API. In which version was it introduced? This [doxygen](http://webrtc.b0.upaiyun.com/da/d93/class_r_t_c_peer_connection.html) knows nothing about it neither. – Teivaz Aug 20 '17 at 18:27
-
3It's been there from long time. You can check the header files – manishg Aug 20 '17 at 22:58
-
Regarding WebRTC stats, check this: https://developer.mozilla.org/en-US/docs/Web/API/RTCStatsReport – pyb Aug 29 '17 at 23:37
-
@manishg Yes, that peer connections RTLegacy stats gave the audio input level as well as the output level too. Thanks for sharing this. – Ambrose Jesuraj Mar 30 '21 at 05:10
1 Answers
After using AVAudioSession
to request permission to record audio, I would recommending using AVAudioRecorder. It is a fairly straightforward class and is as simple as:
- Create an instance of
AVAudioRecorder
- Call the method
prepareToRecord
on the instance - Enable the sound metering function using the method
meteringEnabled
After enabling the recording, you can access the recording volume measurement using the method averagePowerForChannel:
.
You may want to read Apple's documentation
~~~~~~~~~~~~~~~~~~~~~~~ N O T E
~~~~~~~~~~~~~~~~~~~~~~~
I am not familiar with the WebRTC framework/functionality, but the AVAudioRecorder class will provide you with the ability to measure the audio input during a recording.
~~~~~~~~~~~~~~~~~~~~~~ S A M P L E
~~~~~~~~~~~~~~~~~~~~~~
I've included a GitHub sample project that I've used in the past. It is setup to detect sensitivity of the audio using the AVAudioRecorder class that I've described.

- 2,800
- 16
- 20
-
4The problem is that for WebRTC audio track is requested through internal method: `RTCAudioTrack *audioTrack = [self.peerConnectionFactory audioTrackWithTrackId:trackId];` and I don't create any audio recorders. – Teivaz Aug 30 '17 at 21:23