问题
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