0
我正在致力於爲iOS app(swift)
提供一種更快捷的方式讓用戶從Apple Music library
創建播放列表。在閱讀文檔後,我仍然無法弄清楚如何獲取用戶庫。使用Apple Music API創建播放列表?
有沒有一種方法可以訪問用戶庫中的所有歌曲,並將歌曲ID添加到array
?
我正在致力於爲iOS app(swift)
提供一種更快捷的方式讓用戶從Apple Music library
創建播放列表。在閱讀文檔後,我仍然無法弄清楚如何獲取用戶庫。使用Apple Music API創建播放列表?
有沒有一種方法可以訪問用戶庫中的所有歌曲,並將歌曲ID添加到array
?
要訪問Apple的音樂庫,您需要將「隱私 - 媒體庫使用說明」添加到您的info.plist。然後你需要讓你的類符合MPMediaPickerControllerDelegate。要顯示Apple音樂庫,請出示MPMediaPickerController。要將歌曲添加到數組中,可以實現MPMediaPickerControllerDelegate的didPickMediaItems方法。
class MusicPicker:UIViewController, MPMediaPickerControllerDelegate {
//the songs the user will select
var selectedSongs: [URL]!
//this method is to display the music library.
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)
}
}
謝謝!爲什麼將showCloudItems設置爲false?我想使用icloud歌曲以及訪問只需將它更改爲true即可? –
是的,你可以設置它的真實。在我的例子中,我只是把它弄錯了。 – gwinyai
我可以將所有用戶的歌曲放在一個數組中嗎? –