首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用时出错-[MPMediaPlaylist addItemWithProductID:completionHandler:]

使用时出错-[MPMediaPlaylist addItemWithProductID:completionHandler:]
EN

Stack Overflow用户
提问于 2016-04-27 23:49:03
回答 1查看 1.1K关注 0票数 6

我试图使用9.3版的新Apple Music API将歌曲添加到我的应用程序创建的播放列表中,而不将其添加到用户库中。

以productID 316654632为例,它是凤凰城的歌曲Lisztomania,在美国iTunes商店。

使用下面的代码,我可以播放这首歌

代码语言:javascript
复制
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];  
[musicPlayer setQueueWithStoreIDs:@[@"316654632"]];  
[musicPlayer play];  

使用下面的代码,我可以将这首歌添加到我的Apple音乐库

代码语言:javascript
复制
[[MPMediaLibrary defaultMediaLibrary] addItemWithProductID:@"316654632" completionHandler:^(NSArray<__kindof MPMediaEntity *> * _Nonnull entities, NSError * _Nullable error) {  
    NSLog(@"%@", error);  
}];  

错误是零,我可以在我的图书馆看到这首歌。

但是尝试同样的播放列表是行不通的。

代码语言:javascript
复制
[[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);  
        }];  
    }  
}];  

创建了播放列表,我可以在Music.app中看到它,但是当我尝试添加与播放列表相同的产品ID &添加到我的库中时,我会得到一个错误。

Error Domain=MPErrorDomain Code=4 "The requested id could not be found" UserInfo={NSLocalizedDescription=The requested id could not be found}

但是,如果我成功地将同一项添加到我的库中,怎么可能找不到它呢?

更新

好消息!苹果已经修复了10.2.1上的rdar://26408683!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-27 09:22:29

在我的播放列表转换应用程序(混合库)中,我发现向新创建的播放列表中可靠地添加一些歌曲的唯一解决方案是wait

在我的测试中,等待5秒钟似乎就足够了。

代码语言:javascript
复制
[[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);  
    }];  
}  
}];  

我怀疑这是一个与服务器/网络相关的问题,因为它有时不需要等待就能工作。找不到的“所请求的id”可能是播放列表id,而不是轨道id。

当它开始为播放列表工作时,它将始终有效。因此,您不需要在添加每个附加轨道之前等待,而只需要在添加第一个轨道之前。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36902854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档