Swift AVAudioEngine崩溃:玩家在处于断开状态时启动

Car*_*one 2 core-audio ios swift

所以下面的代码应该一遍又一遍地重放chimes.wav文件,音高更高,但是底部的错误会崩溃.任何人都可以找到导致此错误的原因吗?

import UIKit
import AVFoundation
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.



        var timePitch = AVAudioUnitTimePitch()
        timePitch.pitch = 2000
        let filePath: String = NSBundle.mainBundle().pathForResource("chimes", ofType: "wav")!
        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.attachNode(timePitch)
        audioEngine.connect(audioFilePlayer, to:mainMixer, format: audioFileBuffer.format)
        audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile.processingFormat)


        audioEngine.startAndReturnError(nil)

        audioFilePlayer.play()

        audioFilePlayer.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
        audioFilePlayer.scheduleBuffer(audioFileBuffer, atTime: nil, options:.Loops, completionHandler: nil)

    }
Run Code Online (Sandbox Code Playgroud)

2014-11-10 18:34:37.746 windChimes [2350:108235]****因未捕获的异常"com.apple.coreaudio.avfaudio"终止应用程序,原因:"玩家在处于断开状态时启动"****第一掷调用堆栈:(0x185f01e48 0x1965f40e4 0x185f01d08 0x1848726c0 0x18489a33c 0x18489975c 0x10009e638 0x10009e858 0x18a6b0e84 0x18a6b0b94 0x18a853ad4 0x18a765310 0x18a7650dc 0x18a76505c 0x18a6ada2c 0x18a005994 0x18a000564 0x18a000408 0x189fffc08 0x189fff98c 0x189ff93bc 0x185eba14c 0x185eb70d8 0x185eb74b8 0x185de51f4 0x18ef7b5a4 0x18a716784 0x1000a54f8 0x1000a5538 0x196c62a08)的libc ++ abi.dylib:与未捕获的异常终止类型NSException(lldb)

Dan*_*l J 6

声明:"播放器处于断开连接状态时启动"表示连接链存在问题.这意味着要么节点没有连接到引擎,要么节点没有正确链接在一起.因为audioFilePlayer和timePitch节点都已连接,我的印象就是说问题在于这两行:

audioEngine.connect(audioFilePlayer, to:mainMixer, format: audioFileBuffer.format)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile.processingFormat)
Run Code Online (Sandbox Code Playgroud)

连接应将所有组件链接在一起:

audioFilePlayer - > timePitch - > audioEngine.mainMixerNode(或outputNode)

所以连接应该如下所示:

audioEngine.connect(audioFilePlayer, to:timePitch, format: audioFile.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile.processingFormat)
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.