关于ios:检查一首歌是否还存在 | 珊瑚贝

Checking if a song still exists


我正在尝试确定一首歌曲在播放之前是否仍存在于用户的库中。如果用户在其库中删除一首歌曲,则作用于 MPMediaItem 的以下代码行仍会产生一个 URL:

1
NSURL *songURL=[song valueForProperty:MPMediaItemPropertyAssetURL];

songURL 的格式为 “ipod-library://item/item.m4a?id=5050…etc.” 然后调用时以下行崩溃:

1
[[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:nil];

我尝试使用以下行来查看歌曲是否仍然存在,但无论歌曲是否存在,它都会返回 nil。我相信这是因为它与 “ipod-library” 样式的 URL 不兼容。

1
NSData* songData = [NSData dataWithContentsOfURL:songURL];

任何建议都会很棒。感谢阅读!

  • 尝试将 NSError 传递给您的 initWithContentsOfURL:error:。如果您收到错误消息,您将知道该文件不存在(或已损坏)。
  • @IanMacDonald 谢谢!


你无法提前知道,但 AVAudioPlayer 的 initWithContentsOfURL 将返回 nil,如果创建播放器失败,错误将填充 NSError。只需检查返回码。

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

  • 好的,太糟糕了,无法提前检查,但该解决方案有效。谢谢回复!


来源:https://www.codenong.com/27930757/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8958.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?