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)")
}
}
Post
Replies
Boosts
Views
Activity
I've tried to play the same song on mac and iOS, in Mac the play count seem to update when I tap "info" of the song. But when I try to access it on iOS, even after playing the song on the same iOS device, the count and play date is always nil.
I've checked my MusicKit which = authorized.
Let me know if this is some thing iOS doesn't support
Has anyone see this constantly show up, it comes 5-10 of times on every MusicKit request, although the requests gets proper responses, I'm not sure if this error is important or not?
[Default] <ICUserIdentityStoreACAccountBackend: 0x281f47660> Failed to register for account monitoring. err=Error Domain=com.apple.accounts Code=7 "(null)"
Hello it seems there was an unannounced API change to the Music API. Specifically the maximum offset has been limited to 75 from 1000 for all search terms as of Jan 19, 2023. It still worked yesterday.
https://api.music.apple.com/v1/catalog/US/search?offset=1000&types=playlists&term=pop &limit=25
This used to be able to get me much more results, but now it maxes out at offset=75.
Is there a reason why it was limited? Thanks.