이미 AVAudioEngine &으로 녹음하여 sampleRate 22050 aac format audioFile을 출력하는 예제 코드를 발견했습니다.AVAudioEngine, AVAudioSession을 사용하여 aac 형식으로 레코드 및 리 샘플링합니다. setCategory AVAudioSessionCategoryRecord?
코드를 검토 한 후에 AVAudioSessionCategoryRecord 대신 Category AVAudioSessionCategoryPlayAndRecord를 설정해야한다는 문제가 있습니다.
How to improve the code for using "AVAudioSessionCategoryRecord" without "PlayAndRecord"?
(이 경우에는 내가 바로, "AVAudioSessionCategoryRecord"를 사용해야합니까?)
내가 AVAudioSessionCategoryRecord에 setCategory 경우, 내가 오류 얻을 :
AVAudioEngineGraph.mm:1070: Initialize: required condition is false: IsFormatSampleRateAndChannelCountValid(outputHWFormat)
Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(outputHWFormat)'
마이크를 사용하는 방법에 대한 예제가있다을 AVAudioSessionCategoryRecord에서 inputNode로만 레코드 할 수있는 것처럼 보입니다. https://developer.apple.com/library/prerelease/content/samplecode/SpeakToMe/Introduction/Intro.html
다음 코드는 현재 작동합니다.
import UIKit
import AVFoundation
class ViewController: UIViewController {
let audioEngine = AVAudioEngine()
lazy var inputNode : AVAudioInputNode = {
return self.audioEngine.inputNode!
}()
let sampleRate = Double(22050)
lazy var outputFormat : AVAudioFormat = {
return AVAudioFormat(commonFormat: self.inputNode.outputFormatForBus(0).commonFormat,
sampleRate: self.sampleRate,
channels: AVAudioChannelCount(1),
interleaved: false)
}()
let converterNode = AVAudioMixerNode()
lazy var aacFileURL : NSURL = {
let dirPath = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let filePath = dirPath.URLByAppendingPathComponent("rec.aac")
return filePath
}()
lazy var outputAACFile : AVAudioFile = {
var settings = self.outputFormat.settings
settings[AVFormatIDKey] = NSNumber(unsignedInt: kAudioFormatMPEG4AAC)
return try! AVAudioFile(forWriting: self.aacFileURL,
settings:settings)
}()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try! audioSession.setActive(true)
self.audioEngine.attachNode(converterNode)
self.audioEngine.connect(inputNode,
to: converterNode,
format: inputNode.outputFormatForBus(0))
self.audioEngine.connect(converterNode,
to: self.audioEngine.mainMixerNode,
format: outputFormat)
converterNode.volume = 0
converterNode.installTapOnBus(0, bufferSize: 1024, format: converterNode.outputFormatForBus(0)) { (buffer, when) in
try! self.outputAACFile.writeFromBuffer(buffer)
}
try! self.audioEngine.start()
}
}
관련 링크
How can I specify the format of AVAudioEngine Mic-Input?
Recording audio file using AVAudioEngine