Search code examples
swiftaudiokit

audiokit recorder records the first time and doesn't record again the 2nd time


it's my first time using audioKit. great framework by the way

I am able to record, works beautifully.

 func prepareToAKrecord() {
        // Setup microphone and reverb
               mic = engine.input
               reverb = Reverb(mic)
                reverb.loadFactoryPreset(.mediumRoom) //large hall etc
               engine.output = reverb

        do {

            // Check if the recorder is already recording
                    if recorder?.isRecording == true {
                        print("Recorder is already recording. Attempting to stop before reinitializing.")
                        recorder?.stop()
                    }
            

                    // Reset the recorder if it exists
                    if let existingRecorder = recorder {
                        try existingRecorder.reset()
                        print("Recorder reset successfully.")
                    }
            recorder = try NodeRecorder(node: reverb, shouldCleanupRecordings: true)
             try self.engine.start()
             try self.recorder!.record()
        } catch {
            print("Failed to create recorder: \(error)")
        }
        
    }

then I stop it

    func stopAKRecordingAndSave() {
        self.isRecording = false
        
       
        //recorder!.stop()
        recorder.stop()
        engine.stop()
        
        if let recordedAudioFile = recorder!.audioFile {
            self.voiceURL = recordedAudioFile.url
            print("Recording saved to: \(self.voiceURL!)")
            //dec-4
            self.mergeVoiceSong() // Merge this voice reverb and backing track
            //self.merging = false
        } else {
            print("Failed to get the recorded audio file")
        }

        UserDefaults.standard.set(false,forKey: "newSearch")

    }

but if I try to start the engine and record again doesn't work, not even if I do a record.reset()

am I missing an special method to clear the bus? should I keep the engine running and just reset?

I get a bunch of warnings the 2nd time I try recording

enter image description here


Solution

  • Keep the engine running and try it without the reverb. As long as you don't stop the engine and it doesn't throw an error, it is running.

    Think how your node chain looks in the various conditions of your app.

    https://www.audiokit.io/AudioKit/documentation/audiokit/audioengine https://www.audiokit.io/AudioKit/documentation/audiokit/node