在Swift iOS中播放PCM文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Swift iOS中播放PCM文件相关的知识,希望对你有一定的参考价值。

我想使用AVAudioEngine播放pcm文件,使用Swift 2.0播放AVAudioPlayerNode。我是音频编程的新手,不了解我的代码问题:

    var audioEngine: AVAudioEngine = AVAudioEngine()
    var audioFilePlayer: AVAudioPlayerNode = AVAudioPlayerNode()


    @IBAction func playButton(sender: AnyObject) {

    var file = "file7.pcm"
    var fileManager = NSFileManager.defaultManager()

    var wayToFile = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
    var passMusicFileURL: NSURL?

    if let documentPath: NSURL = wayToFile.first as NSURL! {
        let musicFile = documentPath.URLByAppendingPathComponent(file)

        passMusicFileURL = musicFile

        var audioFile = AVAudioFile()
        var audioBuffer = AVAudioPCMBuffer()

        do{
            audioFile = try AVAudioFile(forReading: passMusicFileURL!)
            let audioFormat = audioFile.processingFormat
            let audioFrameCount = UInt32(audioFile.length)

            audioBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount)

        } catch {
            print("error!!! ")
        }

        do {
            try audioFile.readIntoBuffer(audioBuffer)
        } catch _{
            print("error")
        }

        var mainMixer = audioEngine.mainMixerNode
        audioEngine.attachNode(audioFilePlayer)
        audioEngine.connect(audioFilePlayer, to:mainMixer, format: audioBuffer.format)

        do {
            try audioEngine.start()
        } catch _{

            print("error")
        }

        audioFilePlayer.play()
        audioFilePlayer.scheduleBuffer(audioBuffer, atTime: nil, options: AVAudioPlayerNodeBufferOptions.Loops, completionHandler: nil)
    }
}

我能够构建代码,但是当我尝试运行它时,我收到以下错误:

ERROR: [0x1a1b6e000] AVAudioFile.mm:32: AVAudioFileImpl: error 1954115647

我知道文件路径是正确的,文件存在。

有人知道我做错了什么吗?

答案

使用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)
    }
}

以上是关于在Swift iOS中播放PCM文件的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 中添加带有 PCM 数据/缓冲区的可播放(如 wav、wmv)标头?

Swift iOS实现把PCM语音转成MP3格式

在 IOS 9 上使用 AVPlayer 和 Swift 反向播放视频

如何在 iOS 中播放来自服务器的流式 PCM 数据?

AudioUnit播放PCM问题

如何播放 pcm 文件