使用AVAudioEngine反复播放音频文件

使用AVAudioEngine反复播放音频文件

本文介绍了使用AVAudioEngine反复播放音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift和Xcode 6开发iOS应用.我想做的是使用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. But how can I play it without stopping, I mean, that when it ends playing it starts again?

这是我的代码:

/*==================== 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

在说我应该为此使用AVAudioPlayer之前,我不能这样做,因为以后我将不得不使用某些效果并同时重复播放三个音频文件.

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.

推荐答案

我在另一个问题中找到了解决方案,并由@CarveDrone进行了自动回答,所以我只复制了他使用的代码:

I found the solution in another question, asked and also auto-answered by @CarveDrone , so I've just copied the code he used:

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

您唯一需要更改的是filePath常量.这是原始答案的链接:​​让AVAudioEngine重复播放声音

The only thing you have to change is the filePath constant. Here is the link to the original answer: Having AVAudioEngine repeat a sound

这篇关于使用AVAudioEngine反复播放音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:27