I am struggling to understand why i can not declare the recorder variable seen in here, as available to any action in the class..?
Edited to show a new test project in full, that still displays the same behaviour. I just can not seem to declare the recorder var globally. See comments :
import Cocoa
import AVFoundation
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, AVAudioRecorderDelegate {
var recorder : AVAudioRecorder = AVAudioRecorder() // remove this and clean up un-declared recorder vars, and it appears to record as expected
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
@IBAction func startRecording(sender: AnyObject) {
var recordSettings = [
AVFormatIDKey: kAudioFormatAppleLossless,
AVEncoderAudioQualityKey : AVAudioQuality.Medium.rawValue,
AVEncoderBitRateKey : 16,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0
]
var theRecordingUrl = "/Users/myMbp/Desktop/test.wav" // !!! #### ammend the url for testing #### !!!
var theRecordingUrlAsUrl = NSURL(string: theRecordingUrl)!
println(theRecordingUrlAsUrl) // still looks good before crash
var recorderError: NSError?
recorder = AVAudioRecorder(URL: theRecordingUrlAsUrl, settings: recordSettings, error: &recorderError) // with the global recorder var it crashes here
if let e = recorderError {
println(e.localizedDescription)
} else {
recorder.delegate = self
recorder.meteringEnabled = true
if recorder.prepareToRecord() {
println("recording prepared")
}else{
println("fialed to prepare recordeing")
}
if recorder.record() {
println("started recording")
}else{
println("fialed to start recordeing")
}
}
}
@IBAction func stopRecording(sender: AnyObject) {
recorder.stop()
}
}
Lots of experimenting and found that the answer is, i must declare the global recorder variable like this :