问题描述
我正在尝试使用包 react-native-audio-recorder-player 在react native应用中录制音频。音频录制成功开始,但是即使调用 stopRecorder()也可以继续录制。
jobscheduler
即使在按Stop之后,控制台仍会获得 Recording。 。 。,以下是我的控制台。
解决方法
为此,需要在组件外部添加const audioRecorderPlayer = new AudioRecorderPlayer();
。因此,组件将如下所示。
import React from 'react';
import { View,TouchableOpacity,Text} from 'react-native';
import AudioRecorderPlayer from 'react-native-audio-recorder-player';
const audioRecorderPlayer = new AudioRecorderPlayer();
export const Recorder = () => {
const onStartRecord = async () => {
await audioRecorderPlayer.startRecorder();
audioRecorderPlayer.addRecordBackListener(e => {
console.log('Recording . . . ',e.current_position);
return;
});
};
const onStopRecord = async () => {
const audio = await audioRecorderPlayer.stopRecorder();
audioRecorderPlayer.removeRecordBackListener();
};
return (
<View style={{flex: 1,justifyContent: 'center',alignItems: 'space-between'}}>
<TouchableOpacity onPress={onStartRecord}>
<Text>Start</Text>
</TouchableOpacity>
<TouchableOpacity onPress={onStopRecord}>
<Text>Stop</Text>
</TouchableOpacity>
</View>
);
};
更改之后,我的stopRecorder就像魅惑一样工作。