func loadSoundFont(_ pitch : String) { let path: String = Bundle.main.path(forResource: "\(self.ID)/\(pitch)",ofType: "f32")! let url = URL(fileURLWithPath: path) do { let file = try AVAudiofile(forReading: url,commonFormat: AVAudioCommonFormat.pcmFormatfloat32,interleaved: true) let format = file.processingFormat let capacity = AVAudioFrameCount(file.length) self.buffer = AVAudioPCMBuffer(pcmFormat: format,frameCapacity: capacity) try file.read(into: self.buffer!) } catch let error as NSError { print("ERROR HERE",error.localizedDescription) }}
我得到以下错误:1954115647这是:kAudiofileUnsupportedfileTypeError
我的A4.f32文件包含一个PCM浮动32.这里有什么我想念的吗?我的文件中没有标题,是否可能导致此问题?
解决方法 感谢Rhythmic Fistman的评论,我自己去加载它,就这样:func loadSoundFont(_ pitch : String) { let path: String = Bundle.main.path(forResource: "\(self.ID)/\(pitch)",ofType: "f32")! let url = URL(fileURLWithPath: path) do { let data = try Data(contentsOf: url) let format = AVAudioFormat(commonFormat: .pcmFormatfloat32,sampleRate: 44100,channels: 2,interleaved: true) self.buffer = AVAudioPCMBuffer(pcmFormat: format,frameCapacity: AVAudioFrameCount(data.count)) self.buffer!.floatChannelData!.pointee.withMemoryRebound(to: UInt8.self,capacity: data.count) { let stream = OutputStream(toBuffer: ,capacity: data.count) stream.open() _ = data.withUnsafeBytes { stream.write(,maxLength: data.count) } stream.close() } } catch let error as NSError { print("ERROR HERE",error.localizedDescription) }}总结
以上是内存溢出为你收集整理的swift – 将pcm加载到AVAudioPCMBuffer中全部内容,希望文章能够帮你解决swift – 将pcm加载到AVAudioPCMBuffer中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)