如何将苹果音乐上可用的所有播放列表添加到我的项目中



如何将苹果音乐上的所有播放列表都提供给我的项目 .我想访问所有苹果音乐播放列表,目前我正在使用MPMediaLibrary获取播放列表方法,但没有获得任何数据或错误?

`func getUserPlaylist()
    {
MPMediaLibrary.requestAuthorization { (status) in
print(status)
        }
        let lib = MPMediaLibrary()
       // let name = "playlist name"
       // let id:NSUUID = NSUUID()
       // let metadata = MPMediaPlaylistCreationMetadata.init(name: name)
       // metadata.authorDisplayName = "author"
       // metadata.descriptionText = "description"
        lib.getPlaylist(with:id as UUID, creationMetadata: nil) { (playlist, error) in
            guard error == nil
                else
            {
                print(error.debugDescription)
                return
            }
            if let currentPlaylist = playlist
            {
                print(currentPlaylist.name)
            }
        }
    } `

要访问Apple的音乐库,您需要将"隐私 - 媒体库使用说明"添加到info.plist中。然后,您需要使您的类符合 MPMediaPickerControllerDelegate。要显示 Apple Music 资料库,请提供 MPMediaPickerController。若要将歌曲添加到数组中,请实现 MPMediaPickerControllerDelegate 的 didPickMediaItems 方法。

    class MusicPicker:UIViewController, MPMediaPickerControllerDelegate {
//the songs the user will select
var selectedSongs: [URL]!
//this method is to display the music library. You might call it when a user taps a button to add songs to their playlist
func getSongs() {
var mediaPicker: MPMediaPickerController?
mediaPicker = MPMediaPickerController(mediaTypes: .music)
mediaPicker?.delegate = self
mediaPicker?.allowsPickingMultipleItems = true
mediaPicker?.showsCloudItems = false
//present the music library
present(mediaPicker!, animated: true, completion: nil)
    }
    //this is called when the user selects songs from the library
    func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
      //these are the songs that were selected. We are looping over the choices
      for mpMediaItem in mediaItemCollection.items {
 //the song url, add it to an array
let songUrl = mpMediaItem.assetURL
selectedSongs.append(songURL)
       }
       //dismiss the Apple Music Library after the user has selected their songs
       dismiss(animated: true, completion: nil)
       }
       //if the user clicks done or cancel, dismiss the Apple Music library
       func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
      dismiss(animated: true, completion: nil)
    }
       }

我使用以下代码获得了所有播放列表,我使用了MPMediaQuery类。

let query: MPMediaQuery = MPMediaQuery.playlists()
        let playlists = query.collections
        guard playlists != nil else{
            return
        }
        for collection in playlists!{
            print(playlists?.description)
        }

最新更新