[英]How to convert AVAudioPCMBuffer to NSData in Objective-C?
[英]Converting AVAudioPCMBuffer to NSData
我目前正在嘗試將音頻樣本從AVAudioPCMBuffer
轉換為NSData
我已經看了此SO Post和GitHub上的這段代碼的可接受答案,但似乎某些AVFAudio
API有所更改...下面是擴展名我有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'
的Value of type 'UnsafePointer<AudioStreamBasicDescription>' has no member 'inTotalBitsPerChannel'
的錯誤Value of type 'UnsafePointer<AudioStreamBasicDescription>' has no member 'inTotalBitsPerChannel'
。 到目前為止,我還沒有找到其他方法來找出inTotalBitsPerChannel
值...任何幫助表示贊賞!
在您鏈接到的任何一個代碼示例中,我都沒有看到名為inTotalBitsPerChannel
任何方法。 相反,它們似乎都使用mBytesPerFrame
。 您還需要.pointee
來取消引用指針。 最后,在現代Swift中,通常應NSData
使用Data
不是NSData
。 因此,基本上,我認為如果將最后一行重寫為:
let ch0Data = Data(bytes: channels[0], count: Int(frameCapacity * format.streamDescription.pointee.mBytesPerFrame))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.