【问题标题】:AVAudioEngine() Playback Not WorkingAVAudioEngine() 播放不工作
【发布时间】:2016-02-22 03:34:09
【问题描述】:

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

'类型的值没有成员'。

当我删除此行时,我收到以下错误:

'AVAudioPlayerNode.mm:333:开始:所需条件为假: _engine->IsRunning() 由于未捕获的异常“com.apple.coreaudio.avfaudio”而终止应用程序,原因:“所需条件为假: _engine->IsRunning()''

任何帮助将不胜感激。我在 xCode 和单视图应用程序中使用 Swift。

【问题讨论】:

    标签: ios xcode swift


    【解决方案1】:

    这是因为您运行的是过时版本的 XCode 或不兼容的 iOS 版本。我遇到了这个问题,在最新的 Swift 版本中没有这样的方法,而是提供了 .start()。

    【讨论】:

    • 不兼容的 iOS 版本适合我
    【解决方案2】:

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

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多