Post

Replies

Boosts

Views

Activity

Generic function for playing MusicItem
I'm attempting to write a function similar to the example shared over in https://developer.apple.com/forums/thread/700683 for playing a MusicItem using the SystemMusicPlayer. Currently, this function starts like this: func play<MusicItemType>(item: MusicItemType) async throws where MusicItemType: FilterableMusicItem, MusicItemType: Decodable, MusicItemType: PlayableMusicItem { var queue : MusicKit.MusicPlayer.Queue? switch MusicItemType.self { case is Playlist.Type: let request = MusicCatalogResourceRequest<Playlist>(matching: \.id, equalTo: MusicItemID("\(item.id)")) let response = try await request.response() if let responseItem = response.items.first { queue = MusicKit.MusicPlayer.Queue(for: [responseItem]) } case is Album.Type: let request = MusicCatalogResourceRequest<Album>(matching: \.id, equalTo: MusicItemID("\(item.id)")) let response = try await request.response() if let responseItem = response.items.first { queue = MusicKit.MusicPlayer.Queue(for: [responseItem]) } ... I'd love to clean up this case statement and instead construct a MusicCatalogResourceRequest using the generic MusicItemType type parameter, but I haven't yet been able to figure how to construct a KeyPath that matches the KeyPath<MusicItemType.FilterType, Value> type expected. Does anyone have any examples of interacting with MusicCatalogResourceRequest in a generic fashion that they'd be willing to share? Thanks!
1
0
703
Aug ’22
Clear ApplicationMusicPlayer queue after station queued
After an Album, Playlist, or collection of songs have been added to the ApplicationMusicPlayer queue, clearing the queue can be easily accomplished with: ApplicationMusicPlayer.shared.queue.entries = [] This transitions the player to a paused state with an empty queue. After queueing a Station, the same code cannot be used to clear the queue. Instead, it causes the queue to be refilled with a current and next MusicItem from the Station. What's the correct way to detect that the ApplicationMusicPlayer is in the state where it's being refilled by a Station and clear it? I've tried the following approaches with no luck: # Reinitialize queue ApplicationMusicPlayer.shared.queue = ApplicationMusicPlayer.Queue() # Create empty Queue let songs: [Song] = [] let emptyQueue = ApplicationMusicPlayer.Queue(for: songs) ApplicationMusicPlayer.shared.queue = emptyQueue
1
0
191
2w