2015-10-20 118 views
0

在下面的代碼我得到一個運行時錯誤,你可以在這裏看到的這張照片:斯威夫特,展開零運行時錯誤記錄音頻

enter image description here

後,我登錄到Twitter的這種事,Push記錄按鈕,並在下一個ViewController中按下Record按鈕。我懷疑原因是,不知何故audioRecorder是零,當強制解包使用!在「如果self.audioRecorder!.recording」,代碼崩潰。但我不明白爲什麼audioRecorder應該在這裏。你們有什麼想法我做錯了什麼? 這裏是完整的代碼。 最佳,

import UIKit 
import AVFoundation 

class RecordViewController: UIViewController { 

var audioRecorder : AVAudioRecorder? 

func setUpAudioRecorder() { 
    do { 
     let baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first! 
     let pathComponents = [baseString, "sound.m4a"] 
     let audioURL = NSURL.fileURLWithPathComponents(pathComponents) 

     let session = AVAudioSession.sharedInstance() 
     try session.setCategory(AVAudioSessionCategoryPlayAndRecord) 
     try session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker) 
     try session.setActive(true) 

     var recordSettings = [String : AnyObject]() 
     recordSettings[AVFormatIDKey] = Int(kAudioFormatMPEG4AAC) 
     recordSettings[AVSampleRateKey] = 44100.0 
     recordSettings[AVNumberOfChannelsKey] = 2 

     self.audioRecorder = try AVAudioRecorder(URL: audioURL!, settings: recordSettings) 
     self.audioRecorder!.meteringEnabled = true 
     self.audioRecorder!.prepareToRecord() 
    } catch (_) { 
    } 
} 

@IBOutlet weak var recordButton: UIButton! 
@IBOutlet weak var playButton: UIButton! 
@IBOutlet weak var saveButton: UIBarButtonItem! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.playButton.enabled = false 
    self.saveButton.enabled = false 
} 

@IBAction func cancelTapped(sender: AnyObject) { 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 

@IBAction func saveTapped(sender: AnyObject) { 
} 

@IBAction func recordTapped(button: AnyObject) { 
    if self.audioRecorder!.recording { 
     self.audioRecorder!.stop() 
     button.setTitle("Record", forState: UIControlState.Normal) 
    } else { 
     do{ 
     try AVAudioSession.sharedInstance().setActive(true) 
     self.audioRecorder!.record() 
     button.setTitle("Stop Recording", forState: UIControlState.Normal) 
     } catch (_) {} 
     } 
    self.playButton.enabled = true 
} 

@IBAction func playTapped(sender: AnyObject) { 
} 

}

+1

確定'實際上是被稱爲'recordTapped'前setUpAudioRecorder'? –

回答

0

哦,孩子,你是藝術Fistman多麼正確。我只是在IBOutlet中調用函數,現在它正在工作。非常感謝您的幫助。 這裏是工作的代碼別人感興趣:

import UIKit 
import AVFoundation 

class RecordViewController: UIViewController { 

var audioRecorder : AVAudioRecorder? 

func setUpAudioRecorder() { 
    do { 
     let baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first! 
     let pathComponents = [baseString, "sound.m4a"] 
     let audioURL = NSURL.fileURLWithPathComponents(pathComponents) 

     let session = AVAudioSession.sharedInstance() 
     try session.setCategory(AVAudioSessionCategoryPlayAndRecord) 
     try session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker) 
     try session.setActive(true) 

     var recordSettings = [String : AnyObject]() 
     recordSettings[AVFormatIDKey] = Int(kAudioFormatMPEG4AAC) 
     recordSettings[AVSampleRateKey] = 44100.0 
     recordSettings[AVNumberOfChannelsKey] = 2 

     self.audioRecorder = try AVAudioRecorder(URL: audioURL!, settings: recordSettings) 
     self.audioRecorder!.meteringEnabled = true 
     self.audioRecorder!.prepareToRecord() 
    } catch (_) { 
    } 
} 

@IBOutlet weak var recordButton: UIButton! 
@IBOutlet weak var playButton: UIButton! 
@IBOutlet weak var saveButton: UIBarButtonItem! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.playButton.enabled = false 
    self.saveButton.enabled = false 
} 

@IBAction func cancelTapped(sender: AnyObject) { 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 

@IBAction func saveTapped(sender: AnyObject) { 
} 

@IBAction func recordTapped(button: AnyObject) { 
    if (self.audioRecorder?.recording == nil || !self.audioRecorder!.recording) { 
     if (self.audioRecorder?.recording == nil) { 
      setUpAudioRecorder() 
     } 
     do { 
      try AVAudioSession.sharedInstance().setActive(true) 
      self.audioRecorder!.record() 
      button.setTitle("Stop Recording", forState: UIControlState.Normal) 
     } catch (_) {} 
    } else { 
     self.audioRecorder!.stop() 
     button.setTitle("Record", forState: UIControlState.Normal) 
    } 
    self.playButton.enabled = true 
} 

@IBAction func playTapped(sender: AnyObject) { 
} 

}