使AVAudioPCMBuffer工作(AVAudioFile.mm错误代码-50) [英] Getting AVAudioPCMBuffer working (AVAudioFile.mm error code -50)
问题描述
我正在尝试在Swift游乐场(下面的代码)中建立一个基本示例,但也已使用Objective-C进行了尝试,获得了相同的结果.
I'm trying to set up a basic example in a Swift playground (code below) but have also tried it with Objective-C with the same result.
import AVFoundation
let fileURL = ... // have tried a wav file and a aiff file
let myAudioFile = AVAudioFile(forReading: fileURL, error: nil)
let myAudioFormat = myAudioFile.fileFormat
let myAudioFrameCount = UInt32(myAudioFile.length)
var myAudioBuffer = AVAudioPCMBuffer(PCMFormat: myAudioFormat, frameCapacity: myAudioFrameCount)
// have also tried a smaller frameCapacity
到目前为止,它仍然可以正常工作,但是在此终止:
It works fine up to this point, but then terminates with this:
var myError: NSError?
myAudioFile.readIntoBuffer(myAudioBuffer, error:&myError)
我也尝试用buffer:myAudioBuffer命名该参数,但这会导致外部参数"错误.
I have also tried naming the argument with buffer:myAudioBuffer but that gives an "extraneous argument" error.
最终,我想从缓冲区中获取原始的PCM数据,作为Swift的float数组.
Ultimately, I want to get out raw PCM data from the buffer as a Swift array of floats.
推荐答案
必须将AVAudioPCMBuffer
的PCMFormat
设置为AVAudioFile
的.processingFormat
,而 not .fileFormat
:我认为这些是相同的,但事实并非如此!
The AVAudioPCMBuffer
's PCMFormat
has to be set as the AVAudioFile
's .processingFormat
and not its .fileFormat
: I thought these were the same, but that's not the case!
这篇关于使AVAudioPCMBuffer工作(AVAudioFile.mm错误代码-50)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!