我正在创建一个应用程序(Android)来使用 native react 录制手机中的语音。一项要求是实时测量声音的响度并基于它制作动画。我尝试使用react-native-audio库,但问题是响度监控仅在IOS中支持。我检查了世博会音频库,但找不到方法。有没有其他库/方法来测量实时录制的声音的响度?
最佳答案
我找到了这个库https://www.npmjs.com/package/react-native-sound-level ,实时给出录制音频的响度(以分贝为单位)。
首次请求许可,适用于 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();
}
然后将库导入到程序中:
import RNSoundLevel from 'react-native-sound-level'
只需添加以下内容:
componentDidMount(){
this._waveRect.changeSpeed();
RNSoundLevel.start()
RNSoundLevel.onNewFrame = (data) => {
this.setState({sound_level: data.value})
}
}
componentWillUnmount() {
RNSoundLevel.stop()
}
关于android - 使用react-native测量音频的响度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54237516/