Shmidt
Shmidt

Reputation: 16664

AVAudioRecorder delegate not assigned in Swift

I decided to rewrote audiorecorder class from Objective C to Swift. In Objective C recording works, but in Swift AVAudioRecorderDelegate delegate methods not called, recorder starts successfully.

How can I fix this?

class VoiceRecorder: NSObject, AVAudioPlayerDelegate, AVAudioRecorderDelegate {
    var audioRecorder: AVAudioRecorder!
    var audioPlayer: AVAudioPlayer?

    override init() {
        super.init()
        var error: NSError?

        let audioRecordingURL = self.audioRecordingPath()
        audioRecorder = AVAudioRecorder(URL: audioRecordingURL,
            settings: self.audioRecordingSettings(),
            error: &error)

        audioRecorder.meteringEnabled = true
        /* Prepare the recorder and then start the recording */
        audioRecorder.delegate = self
        if audioRecorder.prepareToRecord(){
            println("Successfully prepared for record.")
        }
    }

    func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) {
        println("stop")
        if flag{
            println("Successfully stopped the audio recording process")
            if completionHandler != nil {
                completionHandler(success: flag)
            }
        } else {
            println("Stopping the audio recording failed")
        }
    }
}

func record(){
    audioRecorder.record()
}

//UPD.
func stop(#completion:StopCompletionHandler){
    self.completionHandler = completion
    self.audioRecorder?.stop
}

Upvotes: 1

Views: 1199

Answers (1)

matt
matt

Reputation: 534893

The problem is this line:

self.audioRecorder?.stop

That is not a call to the stop method. It merely mentions the name of the method. You want to say this:

self.audioRecorder?.stop()

Those parentheses make all the difference.

Upvotes: 2

Related Questions