我正在尝试使用 AVaudioPlayer 构建自动平移(在左、中、右声道之间移动音频)来播放我的音乐文件。我可以通过 AVAudioPlayer.pan 方法得到它。通过在我的 swift 代码中使用计时器来实现自动平移。现在的问题是,音频播放不流畅,并且介于两者之间。
这是我现在的代码,
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,并要求音频顺利播放 AUTOPANNED。