I am using an AVPlayer to play an mp3 file. I am using this instead of AVAudioPlayer because it supports playing at speeds outside of the range of 0.0 ... 2.0
. However, I want the audio to loop continually, so I am using an AVQueuePlayer and an AVPlayerLooper to play the file. This works when the rate
property is positive, but when the reversed playback reaches the beginning of the file, it just stops, instead of going back to the end of the file and continuing to play in reverse. Are there any ways I can keep the player looping? Here's what I'm trying:
override func viewDidLoad() {
super.viewDidLoad()
//Initialize the audio file and audio player
guard let path = Bundle.main.path(forResource: "sideAMusic.mp3", ofType: nil) else {
Console.log("Uh oh - no path could be found for filename: \("sideAMusic")")
return
}
let url = URL(fileURLWithPath: path)
let playerItem = AVPlayerItem(url: url)
self.sideAPlayer = AVQueuePlayer(playerItem: playerItem)
sideALooper = AVPlayerLooper(player: sideAPlayer!, templateItem: playerItem)
sideAPlayer?.volume = 1
sideAPlayer?.play()
}
// Use a slider in the range of -4 ... 4 to set the playback speed
@IBAction func speedSlider(_ sender: UISlider) {
self.sideAPlayer?.rate = sender.value
}