iOS - MPMediaItem 显示默认图稿
我目前正在开发一个应用程序,可以显示您正在音乐播放器中收听的艺术家、曲目和专辑封面。一切都很顺利,除了当我播放一首没有艺术作品的歌曲时,我希望能够显示我自己的默认图像(而不是显示空白屏幕)。
下面是我想象它应该如何工作,但它永远不会进入其他,因为 itemArtwork 永远不会为零!
感谢您的帮助。
谢谢,本
_item = [_player nowPlayingItem];
MPMediaItemArtwork *itemArtwork = [_item valueForProperty:MPMediaItemPropertyArtwork];
if (itemArtwork != nil) {
UIImage *albumArtworkImage = [itemArtwork imageWithSize:CGSizeMake(250.0, 250.0)];
_albumArtImageView.image = albumArtworkImage;
} else { // no album artwork
NSLog(@"No ALBUM ARTWORK");
_albumArtImageView.image = [UIImage imageNamed:@"kol.jpg"];
}
I am currently developing an app that shows what artist, track and album art you're listening to in the Music player. All is going well apart from when I play a song with no artwork I want to be able to show my own default image (as opposed to showing a blank screen).
The below is how I imagined it SHOULD work however it never gets into the else as the itemArtwork is never nil!
You're help is appreciated.
Thanks, Ben
_item = [_player nowPlayingItem];
MPMediaItemArtwork *itemArtwork = [_item valueForProperty:MPMediaItemPropertyArtwork];
if (itemArtwork != nil) {
UIImage *albumArtworkImage = [itemArtwork imageWithSize:CGSizeMake(250.0, 250.0)];
_albumArtImageView.image = albumArtworkImage;
} else { // no album artwork
NSLog(@"No ALBUM ARTWORK");
_albumArtImageView.image = [UIImage imageNamed:@"kol.jpg"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MPMediaItemArtwork 似乎始终存在,即使对于没有插图的曲目也是如此。
我检测是否没有图像的方法是查看 MPMediaItemArtwork 的 imageWithSize 是否返回 NULL。
或者,稍微调整一下你的代码:
我希望这些信息可以帮助你(如果是这样,请将此答案标记为选中:-)
MPMediaItemArtwork seem to always exist, even for tracks that don't have artwork.
The way I detect if there's no image is to see if MPMediaItemArtwork's imageWithSize returns NULL.
Or, rejiggering your code a bit:
I hope this info helps you out (and if so, mark this answer as checked :-)
如果您只需要检查艺术品是否存在(而不可能抓取图像,这会消耗大量 CPU 周期),您还可以检查
itemArtwork.bounds
属性。如果艺术品不存在,则bounds.size.width
和bounds.size.height
属性将为 0:If you just need to check if the artwork exists or not (without possibly grabbing the image, which burns a lot of CPU cycles) you can also check the
itemArtwork.bounds
property. If the artwork does not exist, thebounds.size.width
andbounds.size.height
properties will be 0: