将AVAudioPCMBuffer转换为NSData [英] Converting AVAudioPCMBuffer to NSData
问题描述
我目前正在尝试将音频样本从AVAudioPCMBuffer
转换为NSData
-我已经看过了来自GitHub的代码,但似乎某些AVFAudio
API的内容已更改...以下是我对AVAudioPCMBuffer
的扩展名:
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
}
}
我看到错误Value of type 'UnsafePointer<AudioStreamBasicDescription>' has no member 'inTotalBitsPerChannel'
.到目前为止,我还无法找到其他方法来找出inTotalBitsPerChannel
值...任何帮助表示赞赏!
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!
推荐答案
在您链接到的两个代码示例中,我都没有看到任何名为inTotalBitsPerChannel
的方法.相反,它们似乎都使用mBytesPerFrame
.您还需要.pointee
取消引用指针.最后,在现代Swift中,通常应首选使用Data
而不是NSData
.因此,基本上,我认为如果将最后一行重写为:
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))
这篇关于将AVAudioPCMBuffer转换为NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!