如何使用音频引擎和 AVaudioplayernode 播放音频文件以允许音高控制

问题描述

我已经按照在线教程播放音频,但它没有播放任何内容

// // ViewController.swift

导入 UIKit 导入 AVKit 导入语音

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    let engine = AVAudioEngine()
    let pitchControl = AVAudioUnitTimePitch()
    var url = URL(fileURLWithPath: Bundle.main.path(forResource: "TheScotts",ofType:"aif")!)
    
    func play(_ url: URL) throws {
        let audioPlayer: AVAudioPlayerNode!
        //speedControl.rate += 0
        pitchControl.pitch += 20
        // 1: load the file
        let file = try AVAudioFile(forReading: url)
        // 2: create the audio player
        audioPlayer = AVAudioPlayerNode()

        // 3: connect the components to our playback engine
        engine.attach(audioPlayer)
        engine.attach(pitchControl)

        // 4: arrange the parts so that output from one is input to another
        engine.connect(audioPlayer,to: pitchControl,format: nil)
        engine.connect(pitchControl,to: engine.mainmixerNode,format: nil)
        engine.prepare()
        do {
              try engine.start()
           } catch {}
        // 5: prepare the player to play its file from the beginning
        audioPlayer.scheduleFile(file,at: nil)

        // 6: start the engine and player
        try engine.start()
        audioPlayer.play()
    }
    
    do {
    try play(url)
    }
    catch {
        print("Error playing audio")
    }

}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)