我努力使以下情况按预期工作(代码将在下面提供)。
记录我的麦克风输入并将AVAudioPCMBuffer
存储在内存中,这是通过AVAudioPCMBuffer
扩展方法copy(from buffer: AVAudioPCMBuffer, readOffset: AVAudioFrameCount = default, frames: AVAudioFrameCount = default)
完成的。 我确实在录制结束时得到了缓冲。
记录结束后,将缓冲区传递到AKPlayer
并播放。这是一个代码片段,以演示我的工作(我知道这不是完整的应用程序代码,如果需要,我可以共享它):
。
private var player: AKPlayer = AKPlayer()
self.player.buffering = .always
// in the record complete callbak:
self.player.buffer = self.bufferRecorder?.pcmBuffer
self.player.volume = 1
self.player.play()
当我检查和调试应用程序时,我可以看到缓冲区的长度正确,并且我所有的输出/输入设置都使用相同的处理格式(采样率,通道,比特率等)以及记录的缓冲区,但是仍然我的应用程序在此行崩溃:
2018-10-28 08:40:32.625001+0200 BeatmanApp[71037:6731884] [avae] AVAEInternal.h:70:_AVAE_Check:
required condition is false: [AVAudioPlayerNode.mm:665:ScheduleBuffer: (_outputFormat.channelCount == buffer.format.channelCount)]
当我调试和遍历AudioKit代码时,我可以看到在方法AKPlayer+Playback.swift
上line 162
上的playerNode.scheduleBuffer
上的折线是
更多有用的信息:
谢谢!
答案 0 :(得分:1)
好的,这是超级超酷的调试会话。我必须研究AVAudioEngine
以及在那里如何完成这种情况,这当然不是我寻找的最终结果。这个任务帮助我了解了如何使用AudioKit
解决它(我的应用程序的一半是使用AudioKit
的工具实现的,因此用AVFoundation
重写它没有任何意义)。
AFFoundation
解决方案:
private let engine = AVAudioEngine()
private let bufferSize = 1024
private let p: AVAudioPlayerNode = AVAudioPlayerNode()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playAndRecord, mode: .default, options: .defaultToSpeaker)
} catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
let inputNode = self.engine.inputNode
engine.connect(inputNode, to: engine.mainMixerNode, format: inputNode.inputFormat(forBus: 0))
// !!! the following lines are the key to the solution.
// !!! the player has to be attached to the engine before actually connected
engine.attach(p)
engine.connect(p, to: engine.mainMixerNode, format: inputNode.inputFormat(forBus: 0))
do {
try engine.start()
} catch {
print("could not start engine \(error.localizedDescription)")
}
recordBufferAndPlay(duration: 4)
recordBufferAndPlay
功能:
func recordBufferAndPlay(duration: Double){
let inputNode = self.engine.inputNode
let total: Double = AVAudioSession.sharedInstance().sampleRate * duration
let totalBufferSize: UInt32 = UInt32(total)
let recordedBuffer : AVAudioPCMBuffer! = AVAudioPCMBuffer(pcmFormat: inputNode.inputFormat(forBus: 0), frameCapacity: totalBufferSize)
var alreadyRecorded = 0
inputNode.installTap(onBus: 0, bufferSize: 256, format: inputNode.inputFormat(forBus: 0)) {
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
recordedBuffer.copy(from: buffer) // this helper function is taken from audio kit!
alreadyRecorded = alreadyRecorded + Int(buffer.frameLength)
print(alreadyRecorded, totalBufferSize)
if(alreadyRecorded >= totalBufferSize){
inputNode.removeTap(onBus: 0)
self.p.scheduleBuffer(recordedBuffer, at: nil, options: .loops, completionHandler: {
print("completed playing")
})
self.p.play()
}
}
}
AudioKit
解决方案:
因此,在AudioKit解决方案中,应在AKPlayer对象上调用这些行。请注意,应该在实际启动引擎之前完成此操作。
self.player.buffering = .always
AudioKit.engine.attach(self.player.playerNode)
AudioKit.engine.connect(self.player.playerNode, to: self.mixer.inputNode, format: AudioKit.engine.inputNode.outputFormat(forBus: 0))
完成记录的方式与在AVAudioEngine中完成记录的方式非常相似,您需要在节点(麦克风或其他节点)上安装拍子,并记录PCM样本的缓冲区。