我正在尝试使用 AVCaptureAudioDataOutput 来分析音频输入,如此处所述。这不是我自己能弄清楚的东西,所以我正在复制这个例子,但我遇到了困难。
Swift 3 中的 Xcode 促使我进行了一些更改。我在行分配时遇到编译错误samples
。Xcode 说,“不能使用类型为 '(UnsafeMutableRawPointer?)' 的参数列表调用类型 'UnsafeMutablePointer<_> 的初始化程序”</p>
这是我修改后的代码:
func captureOutput(_ captureOutput: AVCaptureOutput!,
didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,
from connection: AVCaptureConnection!){
var buffer: CMBlockBuffer? = nil
var audioBufferList = AudioBufferList(mNumberBuffers: 1,
mBuffers: AudioBuffer(mNumberChannels: 1, mDataByteSize: 0, mData: nil))
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
sampleBuffer,
nil,
&audioBufferList,
MemoryLayout<AudioBufferList>.size, // changed for Swift 3
nil,
nil,
UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
&buffer
)
let abl = UnsafeMutableAudioBufferListPointer(&audioBufferList)
var sum:Int64 = 0
var count:Int = 0
var bufs:Int = 0
for buf in abl {
let samples = UnsafeMutableBufferPointer<Int16>(start: UnsafeMutablePointer(buf.mData), // Error here
count: Int(buf.mDataByteSize)/sizeof(Int16))
for sample in samples {
let s = Int64(sample)
sum = (sum + s*s)
count += 1
}
bufs += 1
}
print( "found \(count) samples in \(bufs) buffers, sum is \(sum)" )
}
谁能告诉我如何修复此代码?