play apple music songs by third party applications

╄→尐↘猪︶ㄣ 提交于 2019-11-29 10:29:33

问题


I am using iTunes Search API in my application & playing the previewUrl songs from within the application by using

AVPlayer

If user wants to play the full song, he/she needs to purchase the song from iTunes Stores only then can he/she play the full song from application.

As apple released the Apple Music & giving the trial OR full membership to every one & allowing to play the full songs, is it possible to play Apple Music full songs from my application Like Previewurl by using

avplayer or mpmovieplayercontroller


回答1:


@"Ted Hosmann" Thanks for reply .

I would like to share some code from here

viewcontroller.m

@import StoreKit;

-(void) submitAppleMusicTrackWithProductID: (NSString *) productID // productID in US is the last numbers after i= in the share URL from Apple Music
{
    NSLog(@"submitAppleMusic has been called for productID: %@", productID);
    [SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
        NSLog(@"status is %ld", (long)status);
        SKCloudServiceController *cloudServiceController;
        cloudServiceController = [[SKCloudServiceController alloc] init];
        [cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {
            NSLog(@"%lu %@", (unsigned long)capabilities, error);

            if (capabilities >= SKCloudServiceCapabilityAddToCloudMusicLibrary)
            {
                NSLog(@"You CAN add to iCloud!");
                [[MPMediaLibrary defaultMediaLibrary] addItemWithProductID:productID completionHandler:^(NSArray<__kindof MPMediaEntity *> * _Nonnull           entities, NSError * _Nullable error)
                {
                    NSLog(@"added id%@ entities: %@ and error is %@", productID, entities, error);
                    NSArray *tracksToPlay = [NSArray arrayWithObject:productID];
                    [[MPMusicPlayerController systemMusicPlayer] setQueueWithStoreIDs:tracksToPlay];
                    [[MPMusicPlayerController systemMusicPlayer] play];

                    [self performSelectorOnMainThread:@selector(getInfoFromAddedAppleMusicTrack:) withObject:productID waitUntilDone:YES];

                }];
            }
            else
            {
                NSLog(@"Blast! The ability to add Apple Music track is not there. sigh.");
            }

        }];

    }];
}
-(void) getInfoFromAddedAppleMusicTrack: (NSString *) productID
{
    NSLog(@"FYI - musicplayer duration is: %f", [[[MPMusicPlayerController systemMusicPlayer] nowPlayingItem] playbackDuration]);
    //need to check for both the nowPlaying item and if there is a reported playbackDuration, as there is a variable time between a storeMediaItema and a concreteMediaItem
    if (([[MPMusicPlayerController systemMusicPlayer] nowPlayingItem]) && ([[[MPMusicPlayerController systemMusicPlayer] nowPlayingItem] playbackDuration]))
    {
        NSLog(@"Media item is playing: %@",[[MPMusicPlayerController systemMusicPlayer] nowPlayingItem]);
       NSLog(@"appleProductIDURL: %@",productID);
        NSLog(@"Ending time: %d",[[[[MPMusicPlayerController systemMusicPlayer] nowPlayingItem] valueForProperty:MPMediaItemPropertyPlaybackDuration] intValue]);
        NSLog(@"Track Name: %@", [[[MPMusicPlayerController systemMusicPlayer] nowPlayingItem] valueForProperty:MPMediaItemPropertyTitle]);
        NSLog(@"Artists Name: %@", [[[MPMusicPlayerController systemMusicPlayer] nowPlayingItem] valueForProperty:MPMediaItemPropertyArtist]);

    }
    else
    {
        NSLog(@"seems the track is not fully loaded so try again in 1 second");

        [self performSelector:@selector(getInfoFromAddedAppleMusicTrack:) withObject:productID afterDelay:1.0];

        // count loops and jump out if something is wrong - I've never seen more that 7 seconds needed
    }  
}



回答2:


Here is the update posted for iOS 9.3 that has info about accessing the Music Library and adding Apple Music tracks... https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS9_3.html



来源:https://stackoverflow.com/questions/36009561/play-apple-music-songs-by-third-party-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!