簡體   English   中英

通過第三方應用程序播放蘋果音樂歌曲

[英]play apple music songs by third party applications

我在我的應用程序中使用iTunes Search API並使用在應用程序內播放previewUrl歌曲

AVPlayer

如果用戶想要播放完整的歌曲,他/她需要從iTunes商店購買該歌曲,然后他/她才能從應用程序播放完整的歌曲。

隨着Apple發布了Apple Music並為每個人提供試用版或正式會員資格並允許播放完整的歌曲,是否可以通過使用我的應用程序Like Previewurl播放Apple Music完整歌曲

avplayer或mpmovieplayercontroller

@“Ted Hosmann”感謝您的回復。

我想從這里分享一些代碼

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
    }  
}

以下是針對iOS 9.3發布的更新,其中包含有關訪問音樂庫和添加Apple Music曲目的信息... https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS9_3.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM