如何将苹果音乐中的所有播放列表都提供给我的项目

时间:2017-01-18 05:52:48

标签: ios swift

如何将苹果音乐上的所有播放列表都提供给我的项目。我想访问所有苹果音乐播放列表,目前我正在使用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)
            }
        }
    } `

2 个答案:

答案 0 :(得分:1)

要访问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)
    }
       }

答案 1 :(得分:1)

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

public static void main(String[] args) {
        Baap b = new Beta();
        System.out.print(b.h + " ");
        System.out.print(b.getH());
    }