Music preview with MusicKit

Hi,

I apologize for asking so many questions. I don't know that I'll ever ship anything but I'm experimenting with an old fashioned radio station like we used to program in the old top forties days.

I'd like to allow the music programmers to listen to sets that they're building by skimming through songs - does MusicKit or any part of the API allow us to access the Music preview for a given song? i.e. - I want users with a subscription to be able to hear the 30 s preview that you can find in Music.

I may be requesting that a Song have a preview as one of its playing mode options.

Thank you,

Daniel

Replies

Hello @dimsumthinking,

The underlying web service for MusicKit, i.e. Apple Music API, does expose information about preview assets for catalog songs.

We are working on exposing those in the MusicKit for Swift framework.

Stay tuned.

Best regards,

Hello @dimsumthinking,

We have exposed these preview assets in iOS 15 beta 4. See Song.previewAssets for more information.

I hope this helps.

Best regards,

Downloading beta 4 right now - I just want to tell you how much I appreciate you taking the time to close the circle on this.

Thank you,

Daniel

Should a PreviewAsset be a PlayableMusicItemType so that it can easily be played?

Hello @dimsumthinking,

We did consider this. Our current thinking on PreviewAsset is that it doesn't need to be played using ApplicationMusicPlayer or SystemMusicPlayer since it's designed to be DRM free.

So you should be able to play it directly with standard APIs like AVFoundation's AVQueuePlayer or AVFAudio's AVAudioPlayer.

We think that either the user is able to play catalog content for Apple Music, in which case apps will want to play full assets with MusicKit's playback API, or users don't have an active Apple Music subscription, in which case apps can only play preview assets.

Hence, we don't anticipate there is much of a need to interleave full DRM'd assets for music items and preview asset within the same playback queue, so we don't see a very compelling use case at the moment for the ability to begin playing PreviewAsset with ApplicationMusicPlayer or SystemMusicPlayer.

Aside from that, we don't really have a clearly defined id for PreviewAsset, which is a prerequisite for conformance to PlayableMusicItem.

That said, we'd be happy to hear your feedback on this point. If you find a compelling reason why we should support this, please let us know about your use case or your motivations. Your feedback is useful for us to decide how the API should evolve.

Best regards,

Hello @JoeKun,

I would like to know how to play the preview assets using AVFoundation's AVQueuePlayer or AVFAudio's AVAudioPlayer. It would be really great if you could share a code snnipet.

Regards

Hello @Nitishbll

Since preview assets for MusicKit are not protected by DRM, you should be able to play them with AVFoundation‘s AVQueuePlayer or AVFAudio’s AVAudioPlayer like you would for any other content.

I'm sorry, but I'm going to have to defer to folks working on AVFoundation for those questions.

My team is focused on MusicKit, and as much as I try to be as helpful as possible with questions that are partly about AVFoundation when they're tangentially related to MusicKit, I have be reasonable about the amount of time I spend on the developer forums, so I can also work on actually improving MusicKit itself.

I hope you'll understand.

Best regards,

Hi @JoeKun, @dimsumthinking

I just found a way to play the 30 seconds preview using AVFoundation's AVPlayer. I am giving a code sinnpet to help other developers to play the music.

// Explicit Class for soundmanager

    class SoundManager : ObservableObject {var audioPlayer: AVPlayer?  func playSound(sound: String) {

      if let url = URL(string: sound) {

       self.audioPlayer = AVPlayer(url: url) }

  } }

  var songProfile: Song? 

// Fetch a song from MusicCatalogSearchRequest

 let song = appleMusicAPI.songs[0]

// Rendering Image from the url

 AsyncImage(url: song.artwork?.url(width: 120, height: 120)) .onAppear { self.songProfile = song }

// Add a button to play/pause the song

soundManager.playSound(sound: songProfile?.previewAssets?.first?.url?.absoluteString ?? "") soundManager.audioPlayer?.play()

Hello @Nitishbll,

Thank you for sharing these code snippets.

First of all, I want to call out that you should consider using MusicKit's ArtworkImage to render a piece of Artwork from a Song or any other music item.

Secondly, I think you might as well change your playSound function to accept a URL directly, as that is what PreviewAsset exposes.

I hope this helps.

Best regards,