我很难使下面的场景像预期的那样工作(下面将提供代码)。
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
上。
更多可能有用的信息:
谢谢!
发布于 2018-11-02 05:58:30
好吧,这是超级不酷的调试会话。我必须研究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示例的缓冲区。
https://stackoverflow.com/questions/53029148
复制相似问题