我正在用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不记录音频?我怎样才能让它这么做?
发布于 2017-06-05 17:28:42
问题在于stopRecording()函数中的行。在调用stop() (记录器)下面,我立即将AVAudioRecorder实例分配给nil。在完成后处理以创建真实的AVAudioRecorder文件之前,此文件将释放并结束该.aac。
发布于 2017-06-03 02:05:54
我就是这么做的,第一次进口
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)
}这应该可以解决你如何录制音频。
发布于 2017-06-04 17:17:23
如果您已经验证了您的url有"aac“扩展,那么我怀疑您只是忘了在您的记录器上调用stop()。这将导致一个未最后定稿的文件。
还在catch块中打印错误。
do{
try throwingFunc()
} catch {
print(error)
}https://stackoverflow.com/questions/44339500
复制相似问题