将AVAudioPCMBuffer转换为NSData [英] Converting AVAudioPCMBuffer to NSData

查看:249
本文介绍了将AVAudioPCMBuffer转换为NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将音频样本从AVAudioPCMBuffer转换为NSData-我已经看过了

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆