I integrate apple music API and get songID from API response and pass into MPMusicPlayerController in setQueue method.
Here is Code:
API: https://api.music.apple.com/v1/catalog/us/search?term=pablo-honey
Response:
{
"results": {
"songs": {
"href": "/v1/catalog/us/search?limit=5&term=pablo-honey&types=songs",
"next": "/v1/catalog/us/search?offset=5&term=pablo-honey&types=songs",
"data": [
{
"id": "35903761",
"type": "songs",
"href": "/v1/catalog/us/songs/35903761",
"attributes": {
"albumName": "The Jerky Boys 2",
"genreNames": [
"Comedy",
"Music"
],
"trackNumber": 1,
"releaseDate": "1994-01-01",
"durationInMillis": 41933,
"isrc": "US39N0400253",
"artwork": {
"width": 1425,
"height": 1425,
"url": "https://is1-ssl.mzstatic.com/image/thumb/Music/y2004/m12/d07/h21/s06.cfbpldlx.jpg/{w}x{h}bb.jpg",
"bgColor": "0d091a",
"textColor1": "fdf2f8",
"textColor2": "f3e7da",
"textColor3": "cdc3cb",
"textColor4": "c5bab3"
},
"url": "https://music.apple.com/us/album/pablo-honey/35903759?i=35903761",
"playParams": {
"id": "35903761",
"kind": "song"
},
"discNumber": 1,
"hasCredits": false,
"isAppleDigitalMaster": false,
"hasLyrics": false,
"name": "Pablo Honey",
"previews": [
{
"url": "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview125/v4/e8/ed/7b/e8ed7bc0-4111-d3a6-e5b3-3929138e682d/mzaf_11433510887326099537.plus.aac.p.m4a"
}
],
"artistName": "The Jerky Boys"
}
},
{
"id": "1097862228",
"type": "songs",
"href": "/v1/catalog/us/songs/1097862228",
"attributes": {
"albumName": "Pablo Honey",
"genreNames": [
"Alternative",
"Music"
],
"trackNumber": 1,
"releaseDate": "1993-02-22",
"durationInMillis": 208667,
"isrc": "GBAYE9200113",
"artwork": {
"width": 4000,
"height": 4000,
"url": "https://is1-ssl.mzstatic.com/image/thumb/Music126/v4/0a/30/e8/0a30e802-d9a3-ba01-243c-774f7cd966fd/634904077969.png/{w}x{h}bb.jpg",
"bgColor": "ffffff",
"textColor1": "040a04",
"textColor2": "541407",
"textColor3": "363b36",
"textColor4": "764338"
},
"composerName": "Colin Greenwood, Ed O'Brien, Jonny Greenwood, Phil Selway & Thom Yorke",
"playParams": {
"id": "1097862228",
"kind": "song"
},
"url": "https://music.apple.com/us/album/you/1097862062?i=1097862228",
"discNumber": 1,
"hasCredits": false,
"isAppleDigitalMaster": false,
"hasLyrics": true,
"name": "You",
"previews": [
{
"url": "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview125/v4/43/0d/18/430d185f-5f4e-b4a3-04d1-05d4c2130dc8/mzaf_2514685066057802275.plus.aac.p.m4a"
}
],
"artistName": "Radiohead"
}
},
{
"id": "1097862419",
"type": "songs",
"href": "/v1/catalog/us/songs/1097862419",
"attributes": {
"albumName": "Pablo Honey",
"genreNames": [
"Alternative",
"Music"
],
"trackNumber": 5,
"durationInMillis": 161533,
"releaseDate": "1993-02-22",
"isrc": "GBAYE9200114",
"artwork": {
"width": 4000,
"height": 4000,
"url": "https://is1-ssl.mzstatic.com/image/thumb/Music126/v4/0a/30/e8/0a30e802-d9a3-ba01-243c-774f7cd966fd/634904077969.png/{w}x{h}bb.jpg",
"bgColor": "ffffff",
"textColor1": "040a04",
"textColor2": "541407",
"textColor3": "363b36",
"textColor4": "764338"
},
"composerName": "Colin Greenwood, Ed O'Brien, Jonny Greenwood, Phil Selway & Thom Yorke",
"playParams": {
"id": "1097862419",
"kind": "song"
},
"url": "https://music.apple.com/us/album/thinking-about-you/1097862062?i=1097862419",
"discNumber": 1,
"hasCredits": false,
"hasLyrics": true,
"isAppleDigitalMaster": false,
"name": "Thinking About You",
"previews": [
{
"url": "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview125/v4/67/77/7e/67777e27-7dd3-6e9a-6b76-d8e8f717f8f0/mzaf_10939723814371564312.plus.aac.p.m4a"
}
],
"artistName": "Radiohead"
}
}
Pass ID value in MPMusicController but getting not prepare to play error.
Here is code of MPMusicController
private func getParametersFrom(ids: [String]) -> [MPMusicPlayerPlayParameters] {
var parameters = [MPMusicPlayerPlayParameters]()
for id in ids {
if let parameter = MPMusicPlayerPlayParameters(dictionary: ["id": id, "kind": "song"]) {
parameters.append(parameter)
} else {
print("PlayerQueue - Could not create play parameters from catalog id \(id)")
}
}
return parameters
}
Calling this function
let parameters = getParametersFrom(ids: ids)
player.setQueue(with: MPMusicPlayerPlayParametersQueueDescriptor(playParametersQueue: parameters))
So,Please help me about this issue.
Any help should be appreciated. Thank you.