syedhali / EZAudio

An iOS and macOS audio visualization framework built upon Core Audio useful for anyone doing real-time, low-latency audio processing and visualizations.
Other
4.94k stars 821 forks source link

Error in Console: "Failed to write audio data to recorded audio file (-38)" #343

Open starkshaw opened 7 years ago

starkshaw commented 7 years ago

Hi, I received some error when I trying to use the recorder feature of EZAudio in my app. It says "Error: Failed to write audio data to recorded audio file (-38)", then I put a try-catch clause to see what the real problem is. But it never works, the app just crashes. It is hard to replicate the error because it happens very random but it will happen somehow. My app only requires few seconds of recording so I don't know if the memory will be an issue.

Here's the code for the recorder:

- (IBAction)toggleMicButton:(id)sender {
    if (self.isMicOn) {
        @try{
            [self.recorder closeAudioFile];
            [self.microphone stopFetchingAudio];
            self.microphoneTextLabel.text = @"Analyzing";
            [self.audioPlot clear];
            self.isMicOn = NO;
            self.isRecording = NO;
            [self aubiopitch];
            self.microphoneTextLabel.text = @"";
        } @catch (NSException *exception) {
            UIAlertController *errorMsg = [UIAlertController
                                           alertControllerWithTitle:@"Error"
                                           message: exception.reason
                                           preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:errorMsg animated:YES completion:nil];
            NSLog(@"%@", exception.reason);
        }
    } else {
        @try {
            [self.microphone startFetchingAudio];
            self.microphoneTextLabel.text = @"Listening";
            self.recorder = [EZRecorder recorderWithURL:[self filePathURL]
                                           clientFormat:[self.microphone audioStreamBasicDescription]
                                               fileType:EZRecorderFileTypeWAV];
            self.isMicOn = YES;
            self.isRecording = YES;
        } @catch (NSException *exception) {
            UIAlertController *errorMsg = [UIAlertController
                                           alertControllerWithTitle:@"Error"
                                           message: exception.reason
                                           preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:errorMsg animated:YES completion:nil];
            NSLog(@"%@", exception.reason);
        }
    }
}

That [self aubiopitch] is a process to analysis the just-saved wav file.

glm4 commented 6 years ago

For me, the problem was causing by appending buffer to the recorder after the closeFile(). Check out the example, they use an isRecording var to control this. Hope it helps