MusicKit Queue broke in iOS18

It's simple to reproduce. The bug is simply when you queue a bunch of songs to play, it will always queue less than what you gave it.

Here, I'm attempting to play an apple curated playlist, it will only queue a subset, usually less than 15, but as low as 1 out of 100. Use the system's forward and backwards to test it out.

Here is the code, just paste it in to the ContentView file and make sure you have the capibility to run it.

    import SwiftUI
    import MusicKit


    struct ContentView: View {
        var body: some View {
            VStack{
                Button("Play Music") {
                    Task{
                        await playMusic()
                    }
                }
            }

        }
    }


    func getOnlySongsFromTracks(tracks:MusicItemCollection<Track>?) async throws ->MusicItemCollection<Song>?{
        var songs:[Song]?
        
        if let t = tracks{
            songs = [Song]()
        
            for track in t {
                
                if case let .song(song) = track {
                    
                    songs?.append(song)
                    print("track is song \(track.debugDescription)")
                }else{
                    print("track not song \(track.debugDescription)")
                }
            }
        }

        if let songs = songs {
            
            let topSongs = MusicItemCollection(songs)
            return topSongs
        }
        
        return nil
    }

    func playMusic() async {
        // Request authorization
            let status = await MusicAuthorization.request()
            guard status == .authorized else {
                print("Music authorization denied.")
                return
            }
            
        
        do {
            // Perform a hardcoded search for a playlist
            let searchTerm = "2000"
            let request = MusicCatalogSearchRequest(term: searchTerm, types: [Playlist.self])
            let response = try await request.response()
            
            guard let playlist = response.playlists.first else {
                print("No playlists found for the search term '\(searchTerm)'.")
                return
            }
            
            // Fetch the songs in the playlist
            let detailedPlaylist = try await playlist.with([.tracks])
            guard let songCollection = try await getOnlySongsFromTracks(tracks: detailedPlaylist.tracks) else {
                print("no songs found")
                return }

            guard let t = detailedPlaylist.tracks else {
                print("no tracks")
                return
            }
            // Create a queue and play
            let musicPlayer = ApplicationMusicPlayer.shared
            let q = ApplicationMusicPlayer.Queue(for: t)
            musicPlayer.queue = q
            try await musicPlayer.play()
            
            print("Now playing playlist: \(playlist.name)")
            
        } catch {
            print("An error occurred: \(error.localizedDescription)")
        }
    }

This looks like a bug to me. You should definitely file a bug report for that. In my app I am using musicPlayer.setQueuer(with:)You can test whether that makes a difference.

Hello @m3kw9, I tested your code on iOS 18 but I was't able to reproduce the issue you are describing. If you are able to consistently reproduce the issue, please capture a sysdiagnose and use Feedback Assistant to submit a bug report, attaching the sysdiagnose. Please mention in your bug report the exact time you were able to reproduce the issue while capturing the sysdiagnose.

    import MusicKit


    struct ContentView: View {
        var body: some View {
            VStack{
                Button("Play Music") {
                    Task{
                        await playMusic()
                    }
                }
            }

        }
    }

func fetchPlaylist(withID playlistID: String) async -> Playlist? {
    do {
        // Create a request for the playlist resource
        let request = MusicCatalogResourceRequest<Playlist>(matching: \.id, equalTo: MusicItemID(playlistID))
        
        // Perform the request
        let response = try await request.response()
        
        // Access the fetched playlist
        if let playlist = response.items.first {
            print("Playlist Title: \(playlist.name)")
            print("Tracks Count: \(playlist.tracks?.count)")
            return playlist
        } else {
            print("Playlist not found")
            return nil
        }
        
    } catch {
        print("Error fetching playlist: \(error.localizedDescription)")
    }
    return nil
}

    func getOnlySongsFromTracks(tracks:MusicItemCollection<Track>?) async throws ->MusicItemCollection<Song>?{
        var songs:[Song]?
        
        if let t = tracks{
            songs = [Song]()
        
            for track in t {
                
                if case let .song(song) = track {
                    
                    songs?.append(song)
                    print("track is song \(track.debugDescription)")
                }else{
                    print("track not song \(track.debugDescription)")
                }
            }
        }

        if let songs = songs {
            
            let topSongs = MusicItemCollection(songs)
            return topSongs
        }
        
        return nil
    }

    func playMusic() async {
        // Request authorization
            let status = await MusicAuthorization.request()
            guard status == .authorized else {
                print("Music authorization denied.")
                return
            }
            
        
        do {
            // Perform a hardcoded search for a playlist
            let searchTerm = "2000"
            let request = MusicCatalogSearchRequest(term: searchTerm, types: [Playlist.self])
            let response = try await request.response()
            
            guard let playlist = response.playlists.first else {
                print("No playlists found for the search term '\(searchTerm)'.")
                return
            }
            
            print("playlist id \(playlist.id)")
            // Fetch the songs in the playlist
            guard let playlistHardcoded = await fetchPlaylist(withID: "pl.e50ccee7318043eaaf8e8e28a2a55114") else{
                return
            }
            
            
            
            let detailedPlaylist = try await playlistHardcoded.with([.tracks])
            guard let songCollection = try await getOnlySongsFromTracks(tracks: detailedPlaylist.tracks) else {
                print("no songs found")
                return }

            guard let t = detailedPlaylist.tracks else {
                print("no tracks")
                return
            }
            // Create a queue and play
            let musicPlayer = ApplicationMusicPlayer.shared
            let q = ApplicationMusicPlayer.Queue(for: t)
            musicPlayer.queue = q
            try await musicPlayer.play()
            
            print("Now playing playlist: \(playlist.name)")
            
        } catch {
            print("An error occurred: \(error.localizedDescription)")
        }
    }
MusicKit Queue broke in iOS18
 
 
Q