Вот очень простой пример.
Это предполагает 5 .mp3
файлов в вашем пакете и 4 кнопки:
- Воспроизведение / перезапуск
- Пауза
- Возобновить
- Перемешать и воспроизвести
подключено к функциям @IBAction
:
class TestAVQueuViewController: UIViewController {
var myQueuePlayer: AVQueuePlayer?
var avItemsArray: [AVPlayerItem] = []
override func viewDidLoad() {
super.viewDidLoad()
// assuming I have 5 .mp3 files in the bundle, named:
let mySongs: [String] = [
"clip1", "clip2", "clip3", "clip4", "clip5",
]
// build the array of URLs for the song files
for clip in mySongs {
if let url = Bundle.main.url(forResource: clip, withExtension: ".mp3") {
avItemsArray.append(AVPlayerItem(url: url))
} else {
print("Could not get URL for \(clip).mp3")
}
}
if avItemsArray.count == 0 {
fatalError("Failed to get URL for ANY songs!")
}
}
func playQueue() -> Void {
// if first time
if myQueuePlayer == nil {
// instantiate the AVQueuePlayer
myQueuePlayer = AVQueuePlayer()
}
guard let player = myQueuePlayer else {
// I suppose it's possible that AVQueuePlayer() failed to instantiate
// so print a message to debug console and return
print("AVQueuePlayer failed to instantiate!")
return
}
// this will make sure the player is stopped and remove any remaining avItems
player.removeAllItems()
// every time this is called,
// loop through and reset the time on each avItem
for item in avItemsArray {
item.seek(to: .zero, completionHandler: nil)
}
// add avItems to the player
avItemsArray.forEach {
player.insert($0, after: nil)
}
// start playing
player.play()
}
@IBAction func playRestartTapped(_ sender: Any) {
playQueue()
}
@IBAction func pauseTapped(_ sender: Any) {
guard let player = myQueuePlayer, player.items().count > 0 else {
return
}
player.pause()
}
@IBAction func resumeTapped(_ sender: Any) {
guard let player = myQueuePlayer, player.items().count > 0 else {
return
}
player.play()
}
@IBAction func restartShuffledTapped(_ sender: Any) {
// shuffle the items
avItemsArray.shuffle()
playQueue()
}
}