问题描述
因此,我下面的代码应该以较高的音高一次又一次地重播chimes.wav文件,但由于底部错误而崩溃。谁能找到导致此错误的原因?
So my code below is supposed to replay the chimes.wav file over and over again, with a higher pitch, but crashes with the error at the bottom. Can anyone find what is causing this error?
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)
}
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)
2014-11-10 18:34:37.746 windChimes[2350:108235] **** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'player started when in a disconnected state'**** First throw call stack:(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: terminating with uncaught exception of type NSException(lldb)
推荐答案
语句:处于断开状态时启动表示连接链有问题。这意味着要么节点未连接到引擎,要么节点未正确链接在一起。由于audioFilePlayer和timePitch节点都已连接,我的印象是问题出在这两行:
The statement: "player started when in a disconnected state" indicates that there is a problem with the connection chain. This means either the nodes were not attached to the engine or the nodes were not linked together properly.Because both the audioFilePlayer and the timePitch nodes were attached, my impression would be to say that the problem lies with these two lines:
audioEngine.connect(audioFilePlayer, to:mainMixer, format: audioFileBuffer.format)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile.processingFormat)
连接应将所有组件链接在一起:
The connection should link all components together:
audioFilePlayer-> timePitch-> audioEngine.mainMixerNode(或outputNode)
audioFilePlayer -> timePitch -> audioEngine.mainMixerNode (or outputNode)
所以连接应该像这样:
audioEngine.connect(audioFilePlayer, to:timePitch, format: audioFile.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile.processingFormat)
我希望这会有所帮助。
这篇关于Swift AVAudioEngine崩溃:播放器在断开状态下启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!