【发布时间】:2014-11-10 23:38:42
【问题描述】:
所以我下面的代码应该一遍又一遍地重播 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)
}
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)
【问题讨论】:
-
我也有这个问题,过去4个小时试图修复它,但没有成功。我尝试了各种组合、执行顺序、连接方式以及与其他音频节点的连接,但这条消息仍然存在。我现在将求助于其他东西来播放音频缓冲区。
标签: ios swift core-audio