2

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
  • Yes, MPMusicPlayerController.systemMusicPlayer can play tracks returned from iTunes search API using trackId. One potential problem preventing playback might be the mismatching storeFrontId of the track and user’s Apple Music subscription. You can try prepareToPlayWithCompletionHandler instead of play, and check for the potential errors. For the mismatching storeFrontId case, you would get an error with domain=MPErrorDomain and code=MPErrorNotFound. – Bahri Okuroglu Jul 06 '17 at 08:26
  • Thanks for the reply buddy, yes after implementing the prepareToPlayWithCompletionHandler method, I am getting the error with Error Domain=MPErrorDomain Code=4 "(null)", please let me know for next further steps. – Ramkrishna Sharma Jul 06 '17 at 08:32
  • Code=4 is NotFound error. You can verify this from the header. Apple documentation (https://developer.apple.com/documentation/mediaplayer/mperrorcode) says it is about store ID problem. You need to pick correct trackID for your test user, or pick another test user with Apple Music subscription from your storeFront. – Bahri Okuroglu Jul 06 '17 at 08:43
  • Thanks so much buddy, I will review the storeFront as per your suggestion. – Ramkrishna Sharma Jul 06 '17 at 08:47
  • @BahriOkuroglu : I had checked the storeFrontId via requestStorefrontIdentifierWithCompletionHandler method which gives the result "143467,29" and 143467 is the StorefrontId of the India, and I have passed the keyword _in_ while construct the URL like "country=in". Still it gives the Error Domain=MPErrorDomain Code=4, please guide me on this. – Ramkrishna Sharma Jul 06 '17 at 09:22

0 Answers0