我在创建的录音机应用程序中有两个函数(遵循Udacity教程。)我试图理解这两个函数是如何相关的:
@IBAction func recordButton(sender: UIButton) {
recordB.hidden = true
inProgress.hidden = false
stopButtonHide.hidden = false
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let currentDateTime = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "ddMMyyyy-HHmmss"
let recordingName = formatter.stringFromDate(currentDateTime)+".wav"
let pathArray = [dirPath, recordingName]
let filePath = NSURL.fileURLWithPathComponents(pathArray)
println(filePath)
var session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
audioRecorder = AVAudioRecorder(URL: filePath, settings: nil, error: nil)
audioRecorder.delegate = self
audioRecorder.meteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}
func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) {
if(flag) {
recordedAudio = RecordedAudio()
recordedAudio.filePathUrl = recorder.url
recordedAudio.title = recorder.url.lastPathComponent
self.performSegueWithIdentifier("stopRecording", sender: recordedAudio)
} else {
println("Recording was not succesful")
recordB.enabled = true
stopButtonHide.hidden = true
}
}
第一个函数开始录制(据我所知),创建并存储音频文件(命名并获取路径。)第二个函数检查录制是否完成。我的问题是,我无法看到两个功能如何连接。第二个功能如何知道在第一个功能中检查录音?有一个名为RecordedAudio.swift的独立类,它有两个变量:
import Foundation
class RecordedAudio: NSObject{
var filePathUrl: NSURL!
var title: String!
}
为什么我需要这门课?这个课程的目的是什么(我知道它是MVC的模型部分,但它是什么)?我试图了解我的代码中发生了什么,因为从我正在关注的教程中我并不清楚这一点。
答案 0 :(得分:1)
似乎recordedAudio
是RecordedAudio
个对象,因此需要该类。
audioRecorderDidFinishRecording
是(as stated in the docs)AVAudioRecorderDelegate
方法,自动生效...
当录制停止或由于达到其时间限制而完成时系统调用。
在这种情况下,“录制”会引用您在AVAudioRecorder
中创建的audioRecorder
recordButton:
(并为其设置了AVAudioRecorderDelegate
以触发audioRecorderDidFinishRecording:
方法。)