0
在我的應用程序中,我希望每次按下表視圖中的不同單元格時播放不同的音頻文件。下面是我的實現,但我不斷收到錯誤:如何從同一AVAudioEngine播放多個不同的音頻文件? (錯誤:!nodeimpl-> HasEngineImpl())
Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: !nodeimpl->HasEngineImpl()'
我覺得這個錯誤意味着它崩潰,因爲音頻引擎已經包含的節點。但我不知道如何解決它在我的情況。
var audioPlayerFile : AVAudioFile!
var audioEngine = AVAudioEngine()
var pitchPlayer = AVAudioPlayerNode()
var timePitch = AVAudioUnitTimePitch()
var delay = AVAudioUnitDelay()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
pitchPlayer.stop()
audioEngine.stop()
audioEngine.reset()
let filePathResource = myConversation.conversation[indexPath.row].fileName
print("file:", filePathResource)
if (filePathResource != nil) {
setUpAudioFilePath(filePathRes: filePathResource!)
}
timePitch.pitch = 0
delay.delayTime = 0
//append more effect:
audioEngine.connect(pitchPlayer, to: timePitch, format: audioPlayerFile.processingFormat)
audioEngine.connect(timePitch, to: delay, format: audioPlayerFile.processingFormat)
audioEngine.connect(delay, to: audioEngine.outputNode, format: audioPlayerFile.processingFormat)
pitchPlayer.scheduleFile(audioPlayerFile, at: nil, completionHandler: nil)
do { try audioEngine.start()}
catch {
print("Error: Starting Audio Engine")
}
pitchPlayer.play()
}
func setUpAudioFilePath (filePathRes: String) {
if let filePath = Bundle.main.path(forResource: filePathRes, ofType: "m4a") {
let filePathUrl = NSURL.fileURL(withPath: filePath)
do { audioPlayerFile = try AVAudioFile(forReading: filePathUrl) }
catch {
print("Error: Reading Audio Player File")
}
audioEngine.attach(pitchPlayer)
audioEngine.attach(timePitch)
audioEngine.attach(delay)
} else {
print("Error: filePath is empty")
}
}