我试图使用9.3版的新Apple Music API将歌曲添加到我的应用程序创建的播放列表中,而不将其添加到用户库中。
以productID 316654632为例,它是凤凰城的歌曲Lisztomania,在美国iTunes商店。
使用下面的代码,我可以播放这首歌
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
[musicPlayer setQueueWithStoreIDs:@[@"316654632"]];
[musicPlayer play]; 使用下面的代码,我可以将这首歌添加到我的Apple音乐库
[[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 "The requested id could not be found" UserInfo={NSLocalizedDescription=The requested id could not be found}
但是,如果我成功地将同一项添加到我的库中,怎么可能找不到它呢?
更新
好消息!苹果已经修复了10.2.1上的rdar://26408683!
发布于 2016-05-27 09:22:29
在我的播放列表转换应用程序(混合库)中,我发现向新创建的播放列表中可靠地添加一些歌曲的唯一解决方案是wait。
在我的测试中,等待5秒钟似乎就足够了。
[[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。
当它开始为播放列表工作时,它将始终有效。因此,您不需要在添加每个附加轨道之前等待,而只需要在添加第一个轨道之前。
https://stackoverflow.com/questions/36902854
复制相似问题