在下面的代码中,我收到一个运行时错误,你可以在这张图片中看到:
在我登录到twitter后,按下Record按钮,然后在下一个ViewController中按下Record按钮。我怀疑原因是不知何故audioRecorder是零并且强制解包时使用! at"如果是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) {
}
}
答案 0 :(得分:0)
哦,小伙子,你是多么正确的节奏拳手。我刚刚调用了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) {
}
}