How to use MusicPlayer to play a song ?

Hi, I am currently learning how to use MusicKit, after fetching a song I want, I am not really sure how to use MusicPlayer in MusicKit to play that song,

For example

let request = MusicCatalogResourceRequest<Song>(matching: \.id,
equalTo: //sonmeMusicItemID)

let response = try await request.response()

guard let song = response.items.first else { return }

After having the song, how can I use MusicPlayer to play that song ? Thank you so much

Answered by snuff4 in 746180022

You can set queue:

ApplicationMusicPlayer.shared.queue = [song]

or

SystemMusicPlayer.shared.queue = [song]

Then:

try await ApplicationMusicPlayer.shared.play()

or

try await SystemMusicPlayer.shared.play()

Accepted Answer

You can set queue:

ApplicationMusicPlayer.shared.queue = [song]

or

SystemMusicPlayer.shared.queue = [song]

Then:

try await ApplicationMusicPlayer.shared.play()

or

try await SystemMusicPlayer.shared.play()

How to use MusicPlayer to play a song ?
 
 
Q