ios - AVAudioEngine() 播放不工作

标签 ios xcode swift

我正在尝试使用 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/

相关文章:

ios - ContainerViewController popView 的 funky 内部的 UINavigationController

ios - Cocoapods 在 Xcode 中重置 pod 配置

ios - 类型推断(自动类型检测)如何快速工作?

c++ - XCode 附加进程/分离进程(以编程方式分离调试器)

ios - Swift 2/XCode 7 中惰性变量中的字符串文字 [无法转换字符串类型的值 ...]

image - 如何判断PFFile是Video还是Image?

ios - 在所有 View 中使用数组变量

iphone - 将“设置首选项”的“键盘类型”更改为带小数点的数字键盘

iOS Quickblox 聊天 : getting error: Code=-999 "You need to enable stream management to use this feature

ios - Objective-C : “Property implementation must have its declaration in interface”