Swift -- 音频 Recorder/Voice Changer App:这些功能有什么关系?
Swift -- Audio Recorder/Voice Changer App: How are these functions related?
我正在创建的录音机应用程序中有两个功能(遵循 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 的单独 class,它有两个变量:
import Foundation
class RecordedAudio: NSObject{
var filePathUrl: NSURL!
var title: String!
}
为什么我需要这个 class?这个 class 的目的是什么(我知道它是 MVC 的模型部分,但仅此而已)?我试图了解我的代码中发生了什么,因为我在遵循的教程中并不清楚。
似乎 recordedAudio
是一个 RecordedAudio
对象,因此需要 class.
audioRecorderDidFinishRecording
是 (as stated in the docs) 一种 AVAudioRecorderDelegate
方法并且会自动...
called by the system when a recording is stopped or has finished due to reaching its time limit.
在这种情况下,"recording" 将引用您在 recordButton:
中创建的 AVAudioRecorder
audioRecorder
(并且您已为其设置 AVAudioRecorderDelegate
以触发 audioRecorderDidFinishRecording:
方法)。
我正在创建的录音机应用程序中有两个功能(遵循 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 的单独 class,它有两个变量:
import Foundation
class RecordedAudio: NSObject{
var filePathUrl: NSURL!
var title: String!
}
为什么我需要这个 class?这个 class 的目的是什么(我知道它是 MVC 的模型部分,但仅此而已)?我试图了解我的代码中发生了什么,因为我在遵循的教程中并不清楚。
似乎
recordedAudio
是一个RecordedAudio
对象,因此需要 class.audioRecorderDidFinishRecording
是 (as stated in the docs) 一种AVAudioRecorderDelegate
方法并且会自动...
called by the system when a recording is stopped or has finished due to reaching its time limit.
在这种情况下,"recording" 将引用您在 recordButton:
中创建的 AVAudioRecorder
audioRecorder
(并且您已为其设置 AVAudioRecorderDelegate
以触发 audioRecorderDidFinishRecording:
方法)。