Home > Net >  AVAudioPCMBuffer as Base64
AVAudioPCMBuffer as Base64

Time:04-03

How can the data from an AVAudioPCMBuffer be obtained as a Base64 encoded string, with Swift?

I'm asking as I'd like to send data from the microphone input on an iOS device to a WebSocket.

CodePudding user response:

Maybe you can convert the AVAudioPCMBuffer to Data and from Data to a base64 string.

private func encodeBuffer(_ buffer: AVAudioPCMBuffer) {
    
    let audioBuffer = buffer.audioBufferList.pointee.mBuffers
    
    if let audioDataReference = audioBuffer.mData
    {
        let bufferData = Data(bytes: audioDataReference,
                              count: Int(audioBuffer.mDataByteSize))
        
        let encodedString = bufferData.base64EncodedString()
        
        // do what you need with the base 64 string
    }
}
  • Related