Running into weird error when trying to record audio

Please provide the following:

  1. SDK Version: 35
  2. Platforms(Android/iOS/web/all): tested on physical android

I’m running into an issue with recording Audio. For some reason, whenever I try to record with Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY, I get the error:

You attempted to set the key `outputFormat` with the value `”aac”` on an object that is meant to be immutable and has been frozen

What’s weird is that if I change it to Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY, then that error disappears, BUT the recording doesn’t commence and I get thrown a different error,

Start encountered an error: recording not started

Not sure what I’m doing wrong here.

Here’s the relevant code:

  const handleRecordAudio = async () => {
    console.log("trying to record..");
    try {
      if (permissions) {
        await Audio.setAudioModeAsync({
          allowsRecordingIOS: true,
          staysActiveInBackground: false,
          interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
          playsInSilentModeIOS: true,
          shouldDuckAndroid: true,
          interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
          playThroughEarpieceAndroid: true
        });
        recording
          .prepareToRecordAsync(Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY)
          .then(async () => {
            recording.setOnRecordingStatusUpdate(({ durationMillis }) =>
              setRecordingTime(msToTime(durationMillis))
            );
            recording.setProgressUpdateInterval(50);

            await recording.startAsync();
          })
          .catch(err => {
            console.log("some err", err);
          });
      } else {
        alert("This function requires microphone permissions");
      }
    } catch (err) {
      console.log("err caught with recording video", err);
    }
  };

  const handleStopRecordingAudio = async () => {
    try {
      await recording.stopAndUnloadAsync();

      const { uri, size } = await FileSystem.getInfoAsync(recording.getURI());
      console.log(`FILE INFO: ${JSON.stringify({ uri, size })}`);

      setRecording(new Audio.Recording());
      setRecordingTime(0);

      onSendAudio(uri, size);
    } catch (err) {
      console.log("Error caught stopping recording", err);
    }
  };

  return (
    <TouchableOpacity
      onPressIn={() => handleRecordAudio()}
      onPressOut={() => handleStopRecordingAudio()}
    >
      <Icon
        name="md-microphone"
        type="ionicon"
        color={currentVideoInFullScreenMode ? "#9f24b5" : "#e74c3c"}
      />
    </TouchableOpacity>
  );

I’ve resolved this issue. This was happening because I was trying to make changes to recording after I called recording.prepareToRecordAsync().

recording.setProgressUpdateInterval() and recording.setOnRecordingStatusUpdate() must come before recording.prepareToRecordAsync()

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.