How to play Music Videos with MusicKit for Swift?

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.

Answered by Frameworks Engineer in 696385022

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,

Accepted Answer

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,

Is there any update on that with iOS 18?

Could you provide instructions on how to use the application player vs the system player with this work around. MPMusicPlayerController.systemMusicPlayer.openToPlay(queueDescriptor)

We'd live to understand why it's not possible via the API. Video Previews are available. It seems to me that if an Apple Music Subscriber authenticates within your MusicKit app developers should be able to write music kit apps that also play video content. The suggested workaround is NOT a great experience, the preloading sequence brings the user to a random looking view state before loading the video. The video's audio is clipped and pops during this loading sequence. Developer wanting to re-skin the interface need full access to the applications player. MusicKit is awesome with this major oversight and omission as an exception. Calling the Apple Music API also fails to return the actual video URL Pleas explain a better work around.

How to play Music Videos with MusicKit for Swift?
 
 
Q