gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:41:02 25 4
gpt4 key购买 nike

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

考虑产品 ID 316654632,它是美国 iTunes Store 中 Phoenix 的歌曲 Lisztomania。

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

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];  
[musicPlayer setQueueWithStoreIDs:@[@"316654632"]];
[musicPlayer play];

使用以下代码,我可以将歌曲添加到我的 Apple Music 音乐库

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

错误为零,我可以在我的库中看到这首歌。

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

[[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“找不到请求的 ID”UserInfo={NSLocalizedDescription=找不到请求的 ID}

但如果我成功将相同的项目添加到我的图书馆,怎么会找不到呢?

更新

好消息! 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);
}];
}
}];

我怀疑这是与服务器/网络相关的问题,因为它有时无需等待即可运行。未找到的“请求的 ID”可能是播放列表 ID,而不是轨道 ID。

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

关于ios - 使用时出错 -[MPMediaPlaylist addItemWithProductID :completionHandler:],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36902854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com