我正在使用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
始终为false
。它打印出"RECORDED AUDIO SUCCESSFULLY false"
问题
当我使用上面的代码录制时,我确实将文件保存到指定位置的文档目录中。但是这个文件不是我可以玩的东西。它写入文本文件,而不是音频文件,因为我指定扩展名为.aac
。
为什么AVAudioRecorder
没有录制音频?我该怎么做呢?
答案 0 :(得分:0)
我就是这样做的,先导入
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 将设置实际录制文件的名称
filepath 将最终位置的目录和recordingName组合在一起
其余的几乎是自我解释。
然后创建一个更简单的暂停按钮:
@IBAction func pauseButton(_ sender: Any) {
audioRecorder.stop()
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setActive(false)
}
这应解决您录制音频的方式。
答案 1 :(得分:0)
如果您确认自己的网址有“aac”扩展名,那么我怀疑您只是忘记在录制器上调用stop()。这导致未完成的文件。
还在catch块中打印错误。
do{
try throwingFunc()
} catch {
print(error)
}
答案 2 :(得分:0)
问题在于stopRecording()
函数中的行。在致电stop()
录音机的下方,我立即将AVAudioRecorder
实例分配给nil
。这可以在后期处理之前解除分配并结束AVAudioRecorder
以创建可靠的.aac
文件。