AVQueuePlayer only plays the first item in queue

The first time I tap the button it plays all 3 sound files / items. But if I tap the button again, it would only play the first item in the queue, even though there are 3 items in the queue.


import SwiftUI
import AVFoundation

let item1 = AVPlayerItem(url: Bundle.main.url(forResource: "Cshort", withExtension: "mp3")!)
let item2 = AVPlayerItem(url: Bundle.main.url(forResource: "Dshort", withExtension: "mp3")!)
let item3 = AVPlayerItem(url: Bundle.main.url(forResource: "Eshort", withExtension: "mp3")!)

var itemArray = [item1, item2, item3]

var player: AVQueuePlayer = {
    let queue = AVQueuePlayer(items: itemArray)
    return queue
}()

struct ContentView: View {
    var body: some View {
        Button(action: {
            if player.items().isEmpty {
                print("queue is empty")
                print("about to insert")
                for item in itemArray {
                    if player.items().isEmpty {
                        player.insert(itemArray[0], after: nil)
                    } else {
                        player.insert(item, after: player.items().last)
                     
                    }
                }
                print("inserted these items \(player.items())")
            } else {
                print("queue has items to play, no need to insert")
            }
         
            player.seek(to: .zero)
            print("will play from this item: \(player.currentItem!)")
            print("about to play these items: \(player.items())")
            player.play()
            print("done playing——————————————————")
         
        }){
            Text("Button")
        }
    }
}


If I tap the button for the first time, it plays all 3 items one after another, and this is what gets printed in the console:

queue has items to play, no need to insert

will play from this item: <AVPlayerItem: 0x283a44390, asset = <AVURLAsset: 0x283853ce0, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Cshort.mp3>>

about to play these items: [<AVPlayerItem: 0x283a44390, asset = <AVURLAsset: 0x283853ce0, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Cshort.mp3>>, <AVPlayerItem: 0x283a44b20, asset = <AVURLAsset: 0x28386ca60, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Dshort.mp3>>, <AVPlayerItem: 0x283a44c60, asset = <AVURLAsset: 0x28386c9c0, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Eshort.mp3>>]

done playing——————————————————



And after that, every I tap the button it only plays the first item, and this is what gets printed in the console every time:
queue is empty

about to insert

inserted these items [<AVPlayerItem: 0x283a44390, asset = <AVURLAsset: 0x283853ce0, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Cshort.mp3>>, <AVPlayerItem: 0x283a44b20, asset = <AVURLAsset: 0x28386ca60, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Dshort.mp3>>, <AVPlayerItem: 0x283a44c60, asset = <AVURLAsset: 0x28386c9c0, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Eshort.mp3>>]

will play from this item: <AVPlayerItem: 0x283a44390, asset = <AVURLAsset: 0x283853ce0, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Cshort.mp3>>

about to play these items: [<AVPlayerItem: 0x283a44390, asset = <AVURLAsset: 0x283853ce0, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Cshort.mp3>>, <AVPlayerItem: 0x283a44b20, asset = <AVURLAsset: 0x28386ca60, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Dshort.mp3>>, <AVPlayerItem: 0x283a44c60, asset = <AVURLAsset: 0x28386c9c0, URL = file:///private/var/containers/Bundle/Application/F30EC8BB-9C51-42AD-AEF7-840DBAEFCF79/AVQueue.app/Eshort.mp3>>]

done playing——————————————————