Jot*_*ota 5 storekit ios apple-music ios9.3
我正在尝试使用9.3中的新Apple Music API将歌曲添加到我的应用程序创建的播放列表中,而不将其添加到用户的库中.
考虑产品ID 316654632,它是凤凰城的歌曲Lisztomania,位于美国iTunes Store.
使用以下代码,我可以播放这首歌
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
[musicPlayer setQueueWithStoreIDs:@[@"316654632"]];
[musicPlayer play];
Run Code Online (Sandbox Code Playgroud)
使用以下代码,我可以将歌曲添加到Apple Music库中
[[MPMediaLibrary defaultMediaLibrary] addItemWithProductID:@"316654632" completionHandler:^(NSArray<__kindof MPMediaEntity *> * _Nonnull entities, NSError * _Nullable error) {
NSLog(@"%@", error);
}];
Run Code Online (Sandbox Code Playgroud)
错误是零,我可以在我的库中看到这首歌.
但尝试使用播放列表同样不起作用.
[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:uuid creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:@"Test Playlist"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {
NSLog(@"%@", error);
if (!error) {
[playlist addItemWithProductID:@"316654632" completionHandler:^(NSError * _Nullable error) {
NSLog(@"%@", error);
}];
}
}];
Run Code Online (Sandbox Code Playgroud)
播放列表已创建,我可以在Music.app中看到它,但是当我尝试添加我播放并添加到我的库中的相同产品ID到播放列表时,我收到错误
Error Domain=MPErrorDomain Code=4 "The requested id could not be found" UserInfo={NSLocalizedDescription=The requested id could not be found}
但如果我成功将相同的项目添加到我的库中,怎么可能找不到呢?
好消息!Apple在10.2.1修复了rdar:// 26408683!
在我的播放列表转换应用程序(mixlib)中,我发现可靠地将一些曲目添加到新创建的播放列表的唯一解决方案是等待。
在我的测试中,等待五秒钟似乎就足够了。
[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:uuid creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:@"Test Playlist"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {
if (!error) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 /*seconds*/ * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)), ^() {
[playlist addItemWithProductID:@"316654632" completionHandler:^(NSError * _Nullable error) {
NSLog(@"%@", error);
}];
}
}];
Run Code Online (Sandbox Code Playgroud)
我怀疑这是与服务器/网络相关的问题,因为它有时无需等待即可工作。未找到的“请求的id”可能是播放列表id,而不是曲目id。
当它开始适用于播放列表时,它将始终有效。因此,您无需在添加每个附加曲目之前等待,只需在添加第一个曲目之前即可。
| 归档时间: |
|
| 查看次数: |
961 次 |
| 最近记录: |