react-native-audio-recorder-player,audioRecorderPlayer.stopRecorder()crahs app

vsmadaxz  于 2023-08-07  发布在  React
关注(0)|答案(1)|浏览(111)

react-native-audio-recorder-player,用于在我的项目中录制音频的库。当我开始录制一切正常,但关闭应用程序时,我调用函数停止录制音频“audioRecorderPlayer.stopRecorder()"。它在IOS上完美工作(模拟器和真实的设备),和Android模拟器,但是,它不工作在真正的Android设备.
导致此错误的原因是什么,如何解决?

"react-native": "0.63.3",
"react-native-audio-recorder-player": "^2.6.0-rc3",

字符串
控制钩

const [record, setRecord] = useState(false);


启动记录功能

async function handleRecord() {
    try {
      const permissions = await getPermissions();

      if (permissions) {
        const path = Platform.select({
          ios: `audio-${new Date().getTime()}.m4a`,
          android: `sdcard/audio-${new Date().getTime()}.mp3`
        });

        const audioSet = {
          AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
          AudioSourceAndroid: AudioSourceAndroidType.MIC,
          AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
          AVNumberOfChannelsKeyIOS: 2,
          AVFormatIDKeyIOS: AVEncodingOption.aac
        };

        const result = await audioRecorderPlayer.startRecorder(path, audioSet);

        setRecord(true);

        audioRecorderPlayer.addRecordBackListener(e => {
          return;
        });
      }
    } catch (err) {
    }
  }


停止记录功能

async function onStopRecord() {
    try {
      const result = await audioRecorderPlayer.stopRecorder();
      audioRecorderPlayer.removeRecordBackListener();

      setRecord(false);



       setFiles([
        ...files,
        {
          uri: result,
          type: "audio/mpeg",
          name: `audio-${new Date().getTime()}.mp3`
        }
      ]);  
    }catch(error){
      Alert.alert(
        "Erro",
        error
      );
    }
  }

q8l4jmvw

q8l4jmvw1#

注意文件格式。将文件另存为

const path = Platform.select({
          ios: `audio-${new Date().getTime()}.m4a`,
          android: `sdcard/audio-${new Date().getTime()}.mp3`
        });

字符串
如M4A和MP3
但在AudioSet中编码为不同的格式

const audioSet = {
          AudioEncoderAndroid: AudioEncoderAndroidType.AAC,===>here
          AudioSourceAndroid: AudioSourceAndroidType.MIC,
          AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
          AVNumberOfChannelsKeyIOS: 2,
          AVFormatIDKeyIOS: AVEncodingOption.aac ===>here
        };


移除AudioSet进行测试。一切都应该运作良好。之后,把正确的格式为AudioSet

相关问题