我正在尝试使用AVaudioPlayer构建自动平移(在左,中和右声道之间移动音频)以播放音乐文件。我可以通过AVAudioPlayer.pan方法获得它。通过在我的快速代码中使用计时器实现自动平移。现在的问题是,音频不能流畅播放,并且会在两者之间中断。

这是我目前的代码,

class AudioPlayer: UIViewController {
  var player = AVAudioPlayer()

override func viewDidLoad() {
        super.viewDidLoad()
      prepareAudioSession()
    }

  func prepareAudioSession() {

           let audioFileName = "LPNumb"
           let audioFileExtension = "mp3"

           guard let filePath = Bundle.main.path(forResource: audioFileName, ofType: audioFileExtension) else {
               print("Audio file not found at specified path")
               return
           }
        do {
           let alertSound = URL(fileURLWithPath: filePath)
           try? player = AVAudioPlayer(contentsOf: alertSound)

        } catch {
            print(error)
        }
       }

  @IBAction func play(_ sender: AnyObject){
        player.play()
        if  player.isPlaying{
        _ = Timer.scheduledTimer(timeInterval: 0.50, target: self, selector: #selector(self.update1), userInfo: nil, repeats: true)
        _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.update2), userInfo: nil, repeats: true)
            _ = Timer.scheduledTimer(timeInterval: 1.50, target: self, selector: #selector(self.update3), userInfo: nil, repeats: true)
        }



    }
   @objc func update1() {
        player.pan = 0
     }
    @objc func update2() {
        player.pan = 1

    }
    @objc func update3() {
        player.pan = -1
    }

}


我想将输出音频制作为MONO,并要求音频自动平滑播放。

最佳答案

我认为,您只需为该任务创建一个Timer,请看一下代码:

import UIKit
import AVFoundation

enum PanState {
    case up
    case down
}

class ViewController: UIViewController {

    var audioPlayer: AVAudioPlayer?
    var timerForPan: Timer?
    var pan: Double = 0.0
    var panState: PanState = .up

    override func viewDidLoad() {
        super.viewDidLoad()

        if let path = Bundle.main.path(forResource: "LPNumb", ofType: "mp3") {
            let record = URL(fileURLWithPath: path)
            setupRecordToPlayer(from: record)
        }
        setTimers()
    }

    func setTimers() {
        timerForPan = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updatePan), userInfo: nil, repeats: true)
    }

    func setupRecordToPlayer(from url: URL) {
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: url)
        } catch let error {
            debugPrint(error.localizedDescription)
        }
    }

    @IBAction func playButtonPressed(_ sender: UIButton) {
        audioPlayerToPlay()
    }

    @objc func updatePan() {
        if audioPlayer?.isPlaying ?? false {
            switch panState {
            case .up:
                self.pan += 0.1
                if pan >= 1 {
                    self.panState = .down
                }
            case .down:
                self.pan -= 0.1
                if pan <= -1 {
                    self.panState = .up
                }
            }
            audioPlayer?.pan = Float(self.pan)
        }
    }

    /// Prepare Audio player to play
    func audioPlayerToPlay() {
        audioPlayer?.prepareToPlay()
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
        } catch {
            print(error.localizedDescription)
        }
        audioPlayer?.play()
    }
}

关于ios - 借助AVAudioPlayer在iOS中自动平移,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58950402/

10-10 20:33