public Playlist CreatePlaylist(string name) { MPMediaPlaylist playlist = MPMediaLibrary.DefaultMediaLibrary.GetPlaylistAsync(new NSUuid(), new MPMediaPlaylistCreationMetadata(name)).Result; if (playlist != null) { return(new Playlist { Id = playlist.PersistentID, Title = playlist.Name, IsDynamic = true, DateModified = DateTime.Now }); } return(null); }
async Task CreatePlaylistIfNeededAsync() { if (MediaPlaylist != null) { return; } // To create a new playlist or lookup a playlist there // are several steps you need to do. NSUuid playlistUuid; MPMediaPlaylistCreationMetadata playlistCreationMetadata = null; var userDefaults = NSUserDefaults.StandardUserDefaults; if (userDefaults.StringForKey(playlistUuidKey) is string playlistUuidString) { // In this case, the sample already created a playlist in // a previous run. In this case we lookup the UUID that // was used before. playlistUuid = new NSUuid(playlistUuidString); } else { // Create an instance of `UUID` to identify the new playlist. playlistUuid = new NSUuid(); // Create an instance of `MPMediaPlaylistCreationMetadata`, // this represents the metadata to associate with the new playlist. playlistCreationMetadata = new MPMediaPlaylistCreationMetadata("Test Playlist") { DescriptionText = $"This playlist was created using {NSBundle.MainBundle.InfoDictionary ["CFBundleName"]} to demonstrate how to use the Apple Music APIs" }; // Store the `UUID` that the sample will use for looking // up the playlist in the future. userDefaults.SetString(playlistUuid.AsString(), playlistUuidKey); userDefaults.Synchronize(); } // Request the new or existing playlist from the device. MediaPlaylist = await MPMediaLibrary.DefaultMediaLibrary.GetPlaylistAsync(playlistUuid, playlistCreationMetadata); InvokeOnMainThread(() => NSNotificationCenter.DefaultCenter.PostNotificationName(LibraryDidUpdate, null)); }