我正在开发一个应用程序,正在尝试使用音频,但是启动应用程序并开始录制音频时出现错误消息。
错误:线程1:第45-46行的EXC_BAD_ACCESS(代码= 1,地址= 0x0)(尝试!audioRecorder = AVAudioRecorder(url:filePath !,设置:[:])
import UIKit
import AVFoundation
class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate {
var audioRecorder = AVAudioRecorder()
@IBOutlet weak var recordingLabel: UILabel!
@IBOutlet weak var recordingButton: UIButton!
@IBOutlet weak var stopRecoding: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
stopRecoding.isEnabled = false
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("It loaded")
}
@IBAction func recordAudio(_ sender: AnyObject){
recordingLabel.text = "Recording in Progress"
stopRecoding.isEnabled = true
recordingButton.isEnabled = false
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
let recordingName = "recordedVoice.wav"
let pathArray = [dirPath, recordingName]
let filePath = URL(string: pathArray.joined(separator: "/"))
print(filePath!)
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}
@IBAction func stopRecord(_ sender: Any) {
stopRecoding.isEnabled = false
recordingButton.isEnabled = true
recordingLabel.text = "Tap to Record"
audioRecorder.stop()
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setActive(false)
}
}