gpt4 book ai didi

ios - AVAudioEngine() 播放不工作

转载 作者:搜寻专家 更新时间:2023-10-31 22:47:57 27 4
gpt4 key购买 nike

我正在尝试使用 Swift 中的 AVAudioEngine() 改变声音的音高。这是我的代码:

func setUpEngine() {
let fileString = NSBundle.mainBundle().pathForResource("400", ofType: "wav")
let url = NSURL(fileURLWithPath: fileString!)
do {
try audioFile = AVAudioFile(forReading: url)
print("done")
}
catch{

}
}
var engine = AVAudioEngine()
var audioFile = AVAudioFile()
var audioPlayerNode = AVAudioPlayerNode()
var changeAudioUnitTime = AVAudioUnitTimePitch()
override func viewDidLoad() {
setUpEngine()
let defaults = NSUserDefaults.standardUserDefaults()
audioPlayerNode.stop()
engine.stop()
engine.reset()
engine.attachNode(audioPlayerNode)
changeAudioUnitTime.pitch = 800
engine.attachNode(changeAudioUnitTime)
engine.connect(audioPlayerNode, to: changeAudioUnitTime, format: nil)
engine.connect(changeAudioUnitTime, to: engine.outputNode, format: nil)
audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
engine.startAndReturnError(nil)
audioPlayerNode.play()

我的其余代码在下面(我确实关闭了括号)。我在网上找到了大部分代码,但我在

行中遇到了错误
engine.startAndReturnError(nil)

'Value of type has no member'.

当我删除这一行时,出现以下错误:

'AVAudioPlayerNode.mm:333: Start: required condition is false: _engine->IsRunning() Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine->IsRunning()''

如有任何帮助,我们将不胜感激。我在 xCode 和单一 View 应用程序中使用 Swift。

最佳答案

错误是引擎没有运行。你需要像这样重新排序你的操作......

setUpEngine()
let defaults = NSUserDefaults.standardUserDefaults()
engine.attachNode(audioPlayerNode)
engine.attachNode(changeAudioUnitTime)
engine.connect(audioPlayerNode, to: changeAudioUnitTime, format: nil)
engine.connect(changeAudioUnitTime, to: engine.outputNode, format: nil)
changeAudioUnitTime.pitch = 800
engine.prepare()
engine.start()
audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
audioPlayerNode.play()

一段时间后...

engine.stop()

关于ios - AVAudioEngine() 播放不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813263/

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