I have a requirement where I need to search and play the songs within the application, so I am using the iTunes Search API in my application for playing the songs but it unable to play it.
Please find the request and response parameter from the iTunes Search API.
Request : https://itunes.apple.com/search?term=A+R+Rahman&entity=song&country=in&limit=1
Response :
{
resultCount = 1;
results = (
{
artistId = 3249567;
artistName = "A. R. Rahman, Javed Ali & Mohit Chauhan";
artistViewUrl = "https://itunes.apple.com/in/artist/a-r-rahman/id3249567?uo=4";
collectionId = 1123241840;
collectionViewUrl = "https://itunes.apple.com/in/album/kun-faya-kun/id1123241840?i=1123241921&uo=4";
country = IND;
currency = INR;
discCount = 1;
discNumber = 1;
isStreamable = 1;
kind = song;
previewUrl = "http://audio.itunes.apple.com/apple-assets-us-std-000001/AudioPreview20/v4/00/a6/82/00a68217-deef-d90c-d63b-1fab15acb840/mzaf_8630030048415899809.plus.aac.p.m4a";
primaryGenreName = Bollywood;
releaseDate = "2011-09-30T07:00:00Z";
trackCensoredName = "Kun Faya Kun";
trackCount = 14;
trackExplicitness = notExplicit;
trackId = 1123241921;
trackName = "Kun Faya Kun";
trackNumber = 4;
trackPrice = 15;
trackTimeMillis = 470500;
trackViewUrl = "https://itunes.apple.com/in/album/kun-faya-kun/id1123241840?i=1123241921&uo=4";
wrapperType = track;
}
);
}
I am using the trackId and passed it to the setQueueWithStoreIDs
method of MPMusicPlayerController
.
Note: I had also tried the previewUrl, trackViewUrl, collectionId and collectionViewUrl but did not get success.
And I had also gone through the process for checking the authorization and capabilities of the device for playing the song of Apple Music.
Please find the below complete code.
[SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
if(status == SKCloudServiceAuthorizationStatusAuthorized) {
SKCloudServiceController *cloudServiceController = [[SKCloudServiceController alloc] init];
[cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {
if (capabilities == SKCloudServiceCapabilityMusicCatalogPlayback) {
NSLog(@"You can play the song");
[[MPMusicPlayerController systemMusicPlayer] setQueueWithStoreIDs:@[@"1123241921"]];
[[MPMusicPlayerController systemMusicPlayer] prepareToPlayWithCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"Error : %@",error.description);
}];];
}
else {
NSLog(@"You did not have the capability for playing the Apple Music");
}
}];
}
else {
NSLog(@"You do not authorize for Apple Music");
}
}];
Getting Error while playing the song.
Error Domain=MPErrorDomain Code=4 "(null)"
Please guide me on that what implementation is required and can this doable or not.
For download the source code of the application from below link. https://www.dropbox.com/s/2n5110qmaqpvkdf/TestAppleMusic.zip?dl=0