2016-03-15 166 views
6

我在我的应用程序&播放来自应用程序内的previewUrl歌曲使用iTunes Search API使用播放苹果的音乐歌曲

AVPlayer

如果用户想打全曲由第三方应用程序,他/她需要从iTunes Store购买歌曲,然后才能从应用程序播放完整歌曲。

苹果发布了Apple Music &给予试用或正式成员到每一个&允许播放完整的歌曲,是有可能通过使用

发挥 苹果音乐从我的应用程序就像Previewurl整首歌曲

avplayer或的MPMoviePlayerController

回答

4

@“Ted Hosmann”感谢您的回复。

我想从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 
    } 
}