class RecordSoundsViewController: UIViewController {
var audioRecorder:AVAudioRecorder!
@IBAction func recordAudio(sender: UIButton) {
...
try! audioRecorder = AVAudioRecorder(URL: filePath!, settings: [:])
...
}
}
class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate {
var audioRecorder:AVAudioRecorder!
@IBAction func recordAudio(sender: UIButton) {
...
try! audioRecorder = AVAudioRecorder(URL: filePath!, settings: [:])
...
}
}
3. After initialize the audioRecorder instance we declare that the class RecordSoundsViewController will be the delegate for audioRecorder
class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate {
var audioRecorder:AVAudioRecorder!
@IBAction func recordAudio(sender: UIButton) {
...
try! audioRecorder = AVAudioRecorder(URL: filePath!, settings: [:])
audioRecorder.delegate = self
...
}
}
4. We add a function audioRecorderDidFinishRecording in the class RecordSoundsViewController
class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate {
var audioRecorder:AVAudioRecorder!
@IBAction func recordAudio(sender: UIButton) {
...
try! audioRecorder = AVAudioRecorder(URL: filePath!, settings: [:])
audioRecorder.delegate = self
...
}
func audioRecorderDidFinishRecording(recorder: AVAudioRecorder, successfully flag: Bool) {
<#code#>
}
}
Concepts:
- Class AVAudioRecorder has a variable called delegate of type AVAudioRecorderDelegate
class AVAudioRecorder{
var delegate: AVAudioRecorderDelegate!
}
- AVAudioRecorderDelegate is a list of methods, one of them audioRecorderDidFinishRecording
protocol AVAudioRecorderDelegate {
func audioRecorderDidFinishRecording()
}
- In the RecordSoundsViewController of type AVAudioRecorderDelegate, when we see
audioRecorder.delegate = self
we say that RecordSoundsViewController is becoming AVAudioRecorder.delegate so RecordSoundsViewController can implement the functions inside AVAudioRecorderDelegate like audioRecorderDidFinishRecording
No hay comentarios:
Publicar un comentario