我的任务是接收音频流并播放它。在服务器端,音频采用 pcmInt16 编码(16bit,44100 采样率,2 通道)
我接受字节流并在 AVAudioPCMBuffer 中编码,然后在 playMusicFromBuffer 函数中将其传递给播放器。
while (inputStream!.hasBytesAvailable) {
let length = inputStream!.read(&buffer, maxLength: buffer.count)
if (length > 0) {
print("\(#file) > \(#function) > \(length) bytes read")
print(buffer)
let audioBuffer = bytesToAudioBuffer16(buffer)
playMusicFromBuffer(PCMBuffer: audioBuffer)
}
}
起初,在AVAudioPCMBuffer中编码,我使用了bytesToAudioBuffer方法(参数与服务器设置不匹配),但是,应用程序启动并且在转换时没有崩溃,但仍然没有声音。
func bytesToAudioBuffer(_ buf: [UInt8]) -> AVAudioPCMBuffer {
let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100.0, channels: 1, interleaved: true)
let frameLength = UInt32(buf.count) / (fmt?.streamDescription.pointee.mBytesPerFrame)!
let audioBuffer = AVAudioPCMBuffer(pcmFormat: fmt!, frameCapacity: frameLength)
audioBuffer!.frameLength = frameLength
let dstLeft = audioBuffer?.floatChannelData![0]
buf.withUnsafeBufferPointer {
let src = UnsafeRawPointer($0.baseAddress!).bindMemory(to: Float.self, capacity: Int(frameLength))
dstLeft!.initialize(from: src, count: Int(frameLength))
}
print("Convert to AVAudioPCMBuffer")
return audioBuffer!
}
func bytesToAudioBuffer16(_ buf: [UInt8]) -> AVAudioPCMBuffer {
let fmt = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 44100.0, channels: 2, interleaved: true)
let frameLength = UInt32(buf.count) / (fmt?.streamDescription.pointee.mBytesPerFrame)!
let audioBuffer = AVAudioPCMBuffer(pcmFormat: fmt!, frameCapacity: frameLength)
audioBuffer!.frameLength = frameLength
let dstLeft = audioBuffer?.int16ChannelData![0]
buf.withUnsafeBufferPointer {
let src = UnsafeRawPointer($0.baseAddress!).bindMemory(to: Int16.self, capacity: Int(frameLength))
dstLeft!.initialize(from: src, count: Int(frameLength))
}
print("Convert to AVAudioPCMBuffer")
return audioBuffer!
}
var audioEngine: AVAudioEngine = AVAudioEngine()
var audioFilePlayer: AVAudioPlayerNode = AVAudioPlayerNode()
func playMusicFromBuffer(PCMBuffer: AVAudioPCMBuffer) {
let mainMixer = audioEngine.mainMixerNode
audioEngine.attach(audioFilePlayer)
audioEngine.connect(audioFilePlayer, to:mainMixer, format: PCMBuffer.format)
try? audioEngine.start()
audioFilePlayer.play()
audioFilePlayer.scheduleBuffer(PCMBuffer, at: nil, options:AVAudioPlayerNodeBufferOptions.loops)
}
接下来,我尝试通过更改设置并编写 bytesToAudioBuffer16 函数来解决此问题。但是在这里启动应用程序时,当我将流转换为 AVAudioPCMBuffer 并将其传输到播放器时,会出现错误:
[reason __NSCFString * "[[busArray objectAtIndexedSubscript: (NSUInteger) element] setFormat: format error: & nsErr]: returned false, error Error Domain = NSOSStatusErrorDomain Code = -10868 \" (null) \ "" 0x0000600000d2e00.
问题是如何解决这种情况。我需要修复 bytesToAudioBuffer16 或 playMusicFromBuffer。如果是这样,怎么做?