Hello,
I am wondering how one can play music videos (with the actual video playing) with the ApplicationMusicPlayer using MusicKit for Swift?
There is not much documentation on this, so any help would be appreciated.
Hello @ashinthetray,
Playing Music Videos with MusicKit for Swift is not currently possible.
However, if you really need to do this now, Media Player has some support for this use-case, but only with MPMusicPlayerController's systemMusicPlayer, which offers the method openToPlay(_:), which "Opens the Music app and plays the designated videos."
To bridge the gap between the two frameworks, you need to convert MusicVideo's playParameters into MPMusicPlayerPlayParameters. You can achieve this by leveraging the fact that both of these types conform to Codable.
Here's some sample code showing you how you can start playing a MusicVideo:
if let musicVideoPlayParameters = musicVideo.playParameters {
let encoder = JSONEncoder()
let musicVideoPlayParametersData = try encoder.encode(musicVideoPlayParameters)
let decoder = JSONDecoder()
let playParameters = try decoder.decode(MPMusicPlayerPlayParameters.self, from: musicVideoPlayParametersData)
let queueDescriptor = MPMusicPlayerPlayParametersQueueDescriptor(playParametersQueue: [playParameters])
MPMusicPlayerController.systemMusicPlayer.openToPlay(queueDescriptor)
}
That said, I would strongly encourage you to file a new ticket on Feedback Assistant asking for new public API to be introduced to play Music Videos using MusicKit for Swift.
I hope this helps.
Best regards,