簡體   English   中英

將AVAudioPCMBuffer轉換為NSData

[英]Converting AVAudioPCMBuffer to NSData

我目前正在嘗試將音頻樣本從AVAudioPCMBuffer轉換為NSData我已經看了此SO PostGitHub上的這段代碼的可接受答案,但似乎某些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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM