0
我很努力地理解爲什麼我無法聲明在這裏看到的記錄器變量,因爲可用於類中的任何操作..?如何將AVAudioRecorder聲明爲全局變量
編輯完全顯示一個新的測試項目,仍顯示相同的行爲。 我似乎無法全局聲明記錄器變量。看評論:
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()
}
}