gpt4 book ai didi

ios - 如何让 Apple Music 上的所有播放列表都可用到我的项目

转载 作者:可可西里 更新时间:2023-11-01 01:25:38 25 4
gpt4 key购买 nike

如何让 apple music 上的所有播放列表都可用于我的项目。我想访问所有苹果音乐播放列表,目前我正在使用 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)
}
}

关于ios - 如何让 Apple Music 上的所有播放列表都可用到我的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41712286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com