我正在努力理解为什么我不能在这里声明记录变量,因为该类中的任何动作都可以使用。?
编辑为完整显示新测试项目,仍然显示相同的行为。 我似乎无法全局声明记录器var。见评论:
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()
}
}
答案 0 :(得分:0)
经过大量实验并发现答案是,我必须声明全局记录变量如下:
var recorder:AVAudioRecorder!