gpt4 book ai didi

swift - AVAudioRecorder从不录音

转载 作者:行者123 更新时间:2023-11-30 12:24:44 25 4
gpt4 key购买 nike

我正在使用 AVAudioRecorder 来录制人声。我的代码如下:

// Property of Class
var recorder:AVAudioRecorder?

func recordButtonTapped() {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryRecord)
try audioSession.setActive(true)
if audioSession.recordPermission() != .granted {
audioSession.requestRecordPermission({ (success) in
self.startRecording()
})
}
else {
self.startRecording()
}

} catch {
print("Unable To Set Category")
}
}

func startRecording() {
// libraryPathWith(media) just gets the path to the documents directory
// Like so: Documents/MediaLibrary/Audio/<mediaID>.<mediaExtension>
if let path = MMFileManager.libraryPathWith(media: self.media) {
isRecording = true
do {
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]

recorder = try AVAudioRecorder(url: path, settings: settings)

recorder?.delegate = self

if recorder!.prepareToRecord() {
recorder?.record()
}
}
catch {
isRecording = false
}
}
}

func stopRecording() {
self.recordingLabel.text = "Recording Complete"
self.recordingLabel.textColor = UIColor.white
if let rec = recorder {
rec.stop()
recorder = nil
}
isRecording = false
}
<小时/>

AVAudioRecorderDelegate

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
print("RECORDED AUDIO SUCCESSFULLY \(flag)")
}
func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) {
print("AUDIO RECORDER ERROR \(error?.localizedDescription)")
}
<小时/>

在 AVAudioRecorder 上调用 stop 后,audioRecorderEncodeErrorDidOccur 函数永远不会被调用,但 audioRecorderDidFinishRecording 函数会被调用,但 flag 始终为 。它打印出“RECORDED AUDIO SUCCESSFULLY false”

问题

当我使用上面的代码进行录制时,我确实将文件保存到指定位置的文档目录中。但这个文件不是我可以播放的。它写入一个文本文件,而不是音频文件,因为我将扩展名指定为 .aac

为什么AVAudioRecorder不录制音频?我怎样才能做到这一点?

最佳答案

我就是这样做的,首先导入

AVFoundation

并将 AVAudioRecorderDelegate 添加到您的 ViewController:

class RecordViewController: UIViewController, AVAudioRecorderDelegate

然后创建 AVAudioRecorder 的全局实例:

var audioRecorder : AVAudioRecorder!

然后我创建了一个开始录制的录制按钮:

@IBAction func playButton(_ sender: Any) {

let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let recordingName = "voiceRecording.wav"
let pathArray = [dirPath, recordingName]
let filePath = URL(string: pathArray.joined(separator: "/"))

let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)

try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}

dirPath 查找存储图像的目录。

recordingName将设置实际录制文件的名称

文件路径结合了最终位置的目录和录制名称

其余部分几乎是不言自明的。

然后创建一个更简单的暂停按钮:

@IBAction func pauseButton(_ sender: Any) {

audioRecorder.stop()
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setActive(false)
}

这应该可以解决您如何录制音频的问题。

关于swift - AVAudioRecorder从不录音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44339500/

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