我想使用AVAudioEngine播放pcm文件,使用Swift 2.0播放AVAudioPlayerNode。我是音频编程的新手,不了解我的代码的问题:
ERROR: [0x1a1b6e000] AVAudioFile.mm:32: AVAudioFileImpl: error 1954115647
我能够构建代码,但是当我尝试运行它时,我收到以下错误:
{{1}}
我知道文件路径正确且文件存在。
有人知道我做错了吗?
答案 0 :(得分:0)
使用Swift 4
func playPCM(_ filefullpathstr: String) {
do {
let audioFile = try AVAudioFile(forReading: URL(fileURLWithPath: filefullpathstr))
let audioFormat = audioFile.processingFormat
let audioFrameCount = UInt32(audioFile.length)
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)
try audioFile.read(into: audioFileBuffer!, frameCount: audioFrameCount)
let mainMixer = audioEngine.mainMixerNode
audioEngine.attach(audioFilePlayer)
audioEngine.connect(audioFilePlayer, to:mainMixer, format: audioFileBuffer?.format)
try audioEngine.start()
audioFilePlayer.play()
audioFilePlayer.scheduleBuffer(audioFileBuffer!, at: nil, options: [], completionHandler: {
DispatchQueue.main.async {
//do sth ui ...
}
})
} catch {
debugPrint(#line, error)
}
}