2014-11-15 2 views
0

Я пытаюсь понять, почему я не могу объявить переменную записывающего устройства, представленную здесь, как доступную для любого действия в классе ..?Как объявить AVAudioRecorder как глобальную переменную

Отредактировано для демонстрации нового тестового проекта в полном объеме, который по-прежнему демонстрирует то же поведение. Я просто не могу объявить рекордер 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

участки экспериментирования и обнаружили, что ответ, я должен объявить глобальную переменную магнитофона, как это:

var recorder:AVAudioRecorder! 
Смежные вопросы