首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将苹果音乐上所有可用的播放列表添加到我的项目中

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

Stack Overflow用户
提问于 2017-01-18 13:52:48
回答 2查看 1.1K关注 0票数 2

如何将苹果音乐上所有可用的播放列表添加到我的项目中。我想访问所有的苹果音乐播放列表,目前我正在使用MPMediaLibrary获取播放列表的方法,但没有获得任何数据或错误?

代码语言:javascript
复制
`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)
            }
        }
    } `
EN

回答 2

Stack Overflow用户

发布于 2017-01-18 13:58:30

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

代码语言:javascript
复制
    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
EN

Stack Overflow用户

发布于 2017-01-18 19:07:30

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

代码语言:javascript
复制
let query: MPMediaQuery = MPMediaQuery.playlists()
        let playlists = query.collections
        guard playlists != nil else{
            return
        }
        for collection in playlists!{
            print(playlists?.description)
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41712286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档