[英]AKPlayer crashes when playing from buffer on channelCount condition
我努力使以下场景按预期工作(代码将在下面提供)。
记录我的麦克风输入并将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代码时,我可以看到该方法的line 162
行上的AKPlayer+Playback.swift
的断行: playerNode.scheduleBuffer
更多可能有用的信息:
谢谢!
好的,这是超级非冷却调试会话。 我不得不调查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样本的缓冲区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.