我试图在播放之前确定用户的库中是否仍然存在歌曲。如果用户删除其库中的歌曲,则作用于 MPMediaItem 的以下代码行仍会生成 URL:
NSURL *songURL=[song valueForProperty:MPMediaItemPropertyAssetURL];
songURL
的格式为“ipod-library://item/item.m4a?id=5050...etc”。然后下面的行在调用时崩溃:
[[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:nil];
我尝试使用以下行来查看歌曲是否仍然存在,但无论歌曲是否存在,它都会返回 nil。我相信这是因为它与“ipod-library”样式的 URL 不兼容。
NSData* songData = [NSData dataWithContentsOfURL:songURL];
任何建议都会很棒。感谢阅读!
最佳答案
你无法提前知道,但 AVAudioPlayer 的 initWithContentsOfURL 将返回 nil,如果创建播放器失败,错误将填充 NSError。只需检查返回码即可。
关于ios - 检查歌曲是否仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27930757/