Converting AVAudioPCMBuffer to NSData

吃可爱长大的小学妹 提交于 2019-12-11 17:13:36

问题


I'm currently trying to convert the audio samples from AVAudioPCMBuffer to NSData - I had taken a look at the accepted answer on this SO Post and this code from GitHub but it appears some of the AVFAudio API's have changed...below is the extension I have for AVAudioPCMBuffer:

private extension AVAudioPCMBuffer {

    func toNSData() -> NSData {
        let channels = UnsafeBufferPointer(start: int16ChannelData, count: 1)
        let ch0Data = NSData(bytes: channels[0], length:Int(frameCapacity * format.streamDescription.inTotalBitsPerChannel))

        return ch0Data
    }

}

I'm seeing an error of Value of type 'UnsafePointer<AudioStreamBasicDescription>' has no member 'inTotalBitsPerChannel'. So far, I've not been able to find out any other way to find out the inTotalBitsPerChannel value...any help appreciated!


回答1:


I don't see any method named inTotalBitsPerChannel in either of the code samples you linked to; instead, they both seem to use mBytesPerFrame. You will also need .pointee to dereference the pointer. Finally, in modern Swift, you should generally prefer to use Data over NSData. So, basically, I think your extension should work if you rewrite the last line to:

let ch0Data = Data(bytes: channels[0], count: Int(frameCapacity * format.streamDescription.pointee.mBytesPerFrame))


来源:https://stackoverflow.com/questions/49677123/converting-avaudiopcmbuffer-to-nsdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!