ios - 检查歌曲是否仍然存在

标签 ios iphone ipad avaudioplayer

我试图在播放之前确定用户的库中是否仍然存在歌曲。如果用户删除其库中的歌曲,则作用于 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。只需检查返回码即可。

https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/index.html#//apple_ref/occ/instm/AVAudioPlayer/initWithContentsOfURL:fileTypeHint:error :

关于ios - 检查歌曲是否仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27930757/

相关文章:

iphone - Facebook iPhone SDK,应用程序在 iOS 4.2 之前崩溃,但在 iOS 4.2+ 中运行良好

iphone - 如何在全屏模式下获取 iPhone 或 iPad 浏览器/固件版本

ios - 应用程序可以在 iPad 上自动从纵向旋转为横向,但不能在 iPhone 上

objective-c - 如何清除ipad中的webview缓存

objective-c - iOS 中的 PngCrush 是什么?

ios - 如何在 ios 中使用自动布局更改 ScrollView 内容大小

ios - 提交付费应用程序时出错

iphone - "Unable to read symbols for..."导致设备上的 iPhone 应用程序崩溃(不是模拟器)

iphone - UIBarButtonItem 偏移量?

iphone - ImageView 的 UIView 动画框架