gpt4 book ai didi

ios - 如何从同一个 AVAudioEngine 播放多个不同的音频文件? (错误: !nodeimpl->HasEngineImpl())

转载 作者:行者123 更新时间:2023-11-30 12:10:57 24 4
gpt4 key购买 nike

在我的应用程序中,我想每次按下 TableView 中的不同单元格时播放不同的音频文件。以下是我的实现,但我不断收到错误:

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

最佳答案

我认为你需要混音器来播放多个音频。AVAudioEngine已经有与outputNode连接的混音器。试试这个

audioEngine.connect(pitchPlayer, to: timePitch, format: audioPlayerFile.processingFormat)
audioEngine.connect(timePitch, to: delay, format: audioPlayerFile.processingFormat)
audioEngine.connect(delay, to: audioEngine.mainMixerNode, format: audioPlayerFile.processingFormat)

关于ios - 如何从同一个 AVAudioEngine 播放多个不同的音频文件? (错误: !nodeimpl->HasEngineImpl()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46064929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com