繁体   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