gpt4 book ai didi

ios - Swift -- 录音机/变声器应用程序 : How are these functions related?

转载 作者:行者123 更新时间:2023-11-29 02:16:22 40 4
gpt4 key购买 nike

我正在创建的录音机应用程序中有两个函数(遵循 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 的模型部分,但仅此而已)?我试图了解我的代码中发生了什么,因为我在遵循的教程中并不清楚。

最佳答案

  1. 似乎 recordedAudio 是一个 RecordedAudio 对象,因此需要该类。

  2. 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.

在这种情况下,“录音”将指的是您在 recordButton: 中创建的 AVAudioRecorder audioRecorder(并且您为其创建已设置其 AVAudioRecorderDelegate 以触发 audioRecorderDidFinishRecording: 方法)。

关于ios - Swift -- 录音机/变声器应用程序 : How are these functions related?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709299/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com