Measure loudness of the audio with react-native

Viewed 2712

I'm creating an application (Android) to record the voice from the phone using react native. One requirement is to measure the loudness of the voice in realtime and do an animation based on it. I tried to use react-native-audio library, but the problem is loudness monitoring is only supported in IOS. I checked the expo audio library, but couldn't figure out a way to do that. Is there any other library/way to measure the loudness of the sound recorded in real-time?

1 Answers

I found this library https://www.npmjs.com/package/react-native-sound-level, which gives the loudness of the recorded audio in decibel in real-time.

first request for permission, for android 23+

async requestAudioRecordPermission() {
    try {
        if(PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO))
        {
            const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
            {
                'title': 'Audio Record Permission',
                'message': 'App needs access to your microphone'
            }
        )
            if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                console.log("You can use the microphone")
            } else {
                console.log("Audio record permission denied")
            }
        }

    } catch (err) {
        console.log(err)
    }
}



componentWillMount() {
      this.requestAudioRecordPermission();
  }

Then import the library into the program:

import RNSoundLevel from 'react-native-sound-level'

And simply add the following:

componentDidMount(){
this._waveRect.changeSpeed();
  RNSoundLevel.start()
  RNSoundLevel.onNewFrame = (data) => {
        this.setState({sound_level: data.value})
  }
}

componentWillUnmount() {
  RNSoundLevel.stop()
}
Related