Creating a song using MediaPlayer so it shows up in a playlist, from stored file on device.

I'm trying to accomplish creating a new playlist on device that appears in AppleMusic, and adding into the playlist a selection of MP3s within a small IOS app. Now the MP3's are either a stream of bytes, or a flat file already stored on the device (the app itself generates these - they aren't downloaded, they are created in app, and then stored on the local device) in it's local storage space.

The idea is that created tracks can show up in a specific play list on the device.

Now, there appears to be some conflict as to what framework I need to use. I've found MPMediaPlayer, which appears to allow me to create a playlist using the GetPlaylist call, although the documentation on this seems pretty sparse and there's not a lot of examples I can find on how to use this? It looks like a UUID is passed in, but there is no documentation on what this UUID is or where it comes from? If I want to create a new Playlist, I presume I need to generate a UUID, and then store that locally in order to be able to access that playlist again later, yes?

There's an AddItem call which looks like it's how you add a track to a playlist, but there's no documentation on how you generate an entry. The documentation for this function talks about a Product ID, without describing what the product ID is, or where it needs to come from. Is this a GUID? Is it a name/description? Does it have to be unique? I'm assuming this Product ID refers to that which is being added to the playlist, but the documentation is sadly lacking in terms of explaining what the product ID refers to. Is it a media Item, or is that what is created when whatever entity the Product ID is referring to is added to the playlist?

I'm assuming I can create a NSURL of the file that is stored that is actually the MP3 sample, but what I do with that in order to actually add it as a playlist entry is unknown. I'm sure there is a mechanism to do this, it's just not clear what that is.

There's a lack of understanding or explanation of what the process is here, and some illumination would be helpful.

Replies

So, I've made progress. Got the authorization request working fine, can now create a playlist (correctly localized too), but am still having no luck creating a song.

I have an mp3 stored in the documents directory, so full filenameURL (or it could be a byte array, doesn't really matter - I can supply it in either form, and I need to be able to create a MusicKit Song, or MPMediaItem, using that URL/Byte stream, so the music file shows up in Apple Music. Once I have that, I can use the son.id.raw value as a productID I can pass ingo MPMediaPlaylists addItem function call.

I found this text elsewhere that helps that along -

Furthermore, you can definitely use the new MusicKit framework alongside MediaPlayer framework. For example, if you have a MusicKit Song, you could pass song.id.rawValue as the productID parameter to the MPMediaPlaylist's addItem(withProductID: ).

The issue is that I have no idea how to create a musicKit Song from a file URL. How do I go about that? Or how about creating a MPMediaItem instead? Would that work?

@joekun - might you have ideas about this? I'm completely blocked on this right now.