簡體   English   中英

從我的iOS應用程序打開Apple Music

[英]Opening Apple Music from my iOS app

上下文 :我目前正在開發一個與Apple Music API集成的iOS應用程序。 我可以搜索歌曲並在我的應用上播放它們。 為了尊重他們的政策,我允許用戶在Apple Music應用程序上啟動和播放歌曲。 Shazam為Apple Music,Deezer和Google Play做到了這一點(見下圖)。

在此輸入圖像描述

我已經使用這個線程為Spotify做了這個 ,基本上使用了URL方案。 這個 Reddit線程上,我找到了一個iOS URL Scheme的列表,但我找不到任何關於Apple Music的信息 - 這個請求在同一個線程上確認它仍然不可用。

Apple Music API也沒有運氣。

問題 :有人知道如何使用Apple Music實現相同的行為嗎? 順便說一下,沒有必要使用URL方案。

這里的目標是啟動Apple Music並將歌曲播放到Apple Music應用程序中,而不是在我的應用程序上。 我已經在我的應用上播放Apple Music的歌曲了。

我錯過了API文檔的內容嗎? 任何想法將不勝感激。

您應該使用深層鏈接在Apple Music應用程序中打開大頭釘: https//affiliate.itunes.apple.com/resources/documentation/linking-to-the-itunes-music-store/

首先,您需要通過SKCloudServiceController API請求授權來檢查您的功能(例如,您的設備是否允許播放Apple Music曲目)。

[SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
        self.cloudServiceController = [[SKCloudServiceController alloc] init];
        [self.cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {           
            [self.cloudServiceController requestStorefrontIdentifierWithCompletionHandler:^(NSString * _Nullable storefrontIdentifier,
                                                                                            NSError * _Nullable error) {
                NSString *identifier = [[storefrontIdentifier componentsSeparatedByString:@","] firstObject];
                identifier = [[identifier componentsSeparatedByString:@"-"] firstObject];
                NSString *countryCode = [self countryCodeWithIdentifier:identifier];                  
            }];

        }];
    }];

接下來,您將能夠請求商店前端標識符,您將使用該標識符來定義國家/地區代碼。 我建議在項目中包含一個.plist文件,其中包含所有標識符和相應的國家/地區代碼。 (你可以在這里找到.plist文件https://github.com/bendodson/storefront-assistant/blob/master/StorefrontCountries.plist )。 您需要Apple Music API請求的國家/地區代碼。

- (NSString *)countryCodeWithIdentifier:(NSString *)identifier {
     NSURL *plistURL = [[NSBundle mainBundle] URLForResource:@"CountryCodes" withExtension:@"plist"];
     NSDictionary *countryCodeDictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];

     return countryCodeDictionary[identifier];
}

獲得相應的國家/地區代碼后,您就可以在Apple Music的API中搜索曲目了。 使用以下參數向GET https://itunes.apple.com/search請求:

 NSDictionary *parameters = @{
                               @"isStreamable" : @(YES),
                               @"term" : @"your search parameter"
                               @"media" : @"music",
                               @"limit" : @(5),
                               @"country" : @"your country code"
                               };

作為此請求的響應,您將收到一系列跟蹤結果,其中包含許多參數。 其中之一是“trackViewUrl”。 只需將以下參數添加到此trackViewUrl,以便深入鏈接到Apple Music應用程序:

NSString *appleMusicDeepLinking = [NSString stringWithFormat:@"%@&mt=1&app=music", response[0][@"trackViewUrl"]];

在這篇SO帖子中有一些示例代碼,您可能會覺得有用: 通過第三方應用程序播放蘋果音樂歌曲 我能夠激活播放器並播放一首歌:

#import <MediaPlayer/MediaPlayer.h>

[[MPMusicPlayerController systemMusicPlayer] setQueueWithStoreIDs:tracks];
[[MPMusicPlayerController systemMusicPlayer] play];

此外,Apple Music API文檔在此處介紹:

應用程序開發人員的Apple Music最佳實踐

Apple Music API常見問題解答

我希望它有所幫助。

暫無
暫無

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

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