简体   繁体   English

使用 AVAudioEngine 重复播放音频文件

[英]Playing an audio file repeatedly with AVAudioEngine

I'm working on an iOS app with Swift and Xcode 6. What I would like to do is play an audio file using an AVAudioEngine, and until this point everything OK.我正在使用 iOS 应用程序 Swift 和 Xcode 6. 我想做的是使用 AVAudioEngine 播放音频文件,直到此时一切正常。 But how can I play it without stopping, I mean, that when it ends playing it starts again?但是我怎样才能不停地播放它,我的意思是,当它结束播放时又会重新开始播放?

This is my code:这是我的代码:

/*==================== CONFIGURATES THE AVAUDIOENGINE ===========*/
    audioEngine.reset() //Resets any previous configuration on AudioEngine

    let audioPlayerNode = AVAudioPlayerNode() //The node that will play the actual sound
    audioEngine.attachNode(audioPlayerNode) //Attachs the node to the audioengine

    audioEngine.connect(audioPlayerNode, to: audioEngine.outputNode, format: nil) //Connects the applause playback node to the sound output
    audioPlayerNode.scheduleFile(applause.applauseFile, atTime: nil, completionHandler: nil)

    audioEngine.startAndReturnError(nil)
    audioPlayerNode.play() //Plays the sound

Before saying me that I should use AVAudioPlayer for this, I can't because later I will have to use some effects and play three audio files at the same time, also repeatedly.在说我应该为此使用 AVAudioPlayer 之前,我不能,因为稍后我将不得不使用一些效果并同时播放三个音频文件,也重复播放。

I found the solution in another question, asked and also auto-answered by @CarveDrone , so I've just copied the code he used: 我在另一个问题中找到了解决方案,并且也被@CarveDrone自动回答,所以我刚刚复制了他使用的代码:

class aboutViewController: UIViewController {


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

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        let filePath: String = NSBundle.mainBundle().pathForResource("chimes", ofType: "wav")!
        println("\(filePath)")
        let fileURL: NSURL = NSURL(fileURLWithPath: filePath)!
        let audioFile = AVAudioFile(forReading: fileURL, error: nil)
        let audioFormat = audioFile.processingFormat
        let audioFrameCount = UInt32(audioFile.length)
        let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount)
        audioFile.readIntoBuffer(audioFileBuffer, error: nil)

        var mainMixer = audioEngine.mainMixerNode
        audioEngine.attachNode(audioFilePlayer)
        audioEngine.connect(audioFilePlayer, to:mainMixer, format: audioFileBuffer.format)
        audioEngine.startAndReturnError(nil)

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

The only thing you have to change is the filePath constant. 您唯一需要更改的是filePath常量。 Here is the link to the original answer: Having AVAudioEngine repeat a sound 这是原始答案的链接: 让AVAudioEngine重复声音

Swift 5 , thanks at @Guillermo Barreiro Swift 5 ,感谢@Guillermo Barreiro

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

    override func viewDidLoad() {
        super. viewDidLoad()

        guard let filePath: String = Bundle.main.path(forResource: "chimes", ofType: "wav") else{ return }
        print("\(filePath)")
        let fileURL: URL = URL(fileURLWithPath: filePath)
        guard let audioFile = try? AVAudioFile(forReading: fileURL) else{ return }

        let audioFormat = audioFile.processingFormat
        let audioFrameCount = UInt32(audioFile.length)
        guard let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)  else{ return }
        do{
            try audioFile.read(into: audioFileBuffer)
        } catch{
            print("over")
        }
        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:AVAudioPlayerNodeBufferOptions.loops)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM