播放音频流PCMBuffer,AVAudioPCMBuffer

时间:2020-06-21 12:48:46

标签: swift avaudioengine avaudiopcmbuffer

我的任务是接收音频流并播放它。在服务器端,音频由pcmInt16编码(16位,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。如果可以,怎么办?

0 个答案:

没有答案