我已经开始学习iOS应用开发了。在制作第一个应用程序时,我遇到了一个问题。我也试图寻找已经提出问题的解决方案,但可能发现任何错误。任何人都可以告诉我,代码中的错误是什么。我使用了2个按钮和1个标签,并加入了3个IBOutlets和2个带有2个IBAction的按钮。 请指导我... 我找到了同样的问题,并尝试了我在那里找到的几乎所有东西。在那之后,我问了我的问题......
class RecordSoundsViewController: UIViewController {
var audioRecorder: AVAudioRecorder!
@IBOutlet weak var recordingLabel: UILabel!
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var stopRecordingButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
stopRecordingButton.isEnabled = false
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
@IBAction func recordAudio(_ sender: AnyObject) {
recordingLabel.text = "Recording in progress"
stopRecordingButton.isEnabled = true
recordButton.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: "/"))
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)
try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}
@IBAction func stopRecording(_ sender: UIButton) {
recordButton.isEnabled = true
stopRecordingButton.isEnabled = false
recordingLabel.text = "Tap to Record"
audioRecorder.stop()
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setActive(false)
}
}