简体   繁体   English

使用本机测量音频的响度

[英]Measure loudness of the audio with react-native

I'm creating an application (Android) to record the voice from the phone using react native. 我正在创建一个应用程序(Android),以使用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. 我尝试使用react-native-audio库,但问题是仅在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? 是否有其他库/方法可以实时测量录制声音的响度?

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. 我发现了这个库https://www.npmjs.com/package/react-native-sound-level ,它可以实时分贝显示录制音频的响度。

first request for permission, for android 23+ 首次请求许可,适用于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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM