I am working on an iOS app and I need to determine if a song has album art. I am using the MPMusicPlayerController to access the native iOS music library and I am using a MPMediaItemArtwork to capture the artwork sent from the iOS music library. This is the coding I use to get the artwork:
MPMediaItemArtwork *mpArt = [mpSong valueForProperty:MPMediaItemPropertyArtwork];
To test if artwork is present I use this:
if (mpArt)
{
imgArt = [mpArt imageWithSize:CGSizeMake(250, 250)];
}
else
{
imgArt = [UIImage imageNamed:#"Alternative_Artwork_Image.jpg"];
}
No matter what the song's artwork is, the result is always true.
Any help would be appreciated. Thank you in advance.
I think it will always return true if it is an iCloud selection because it will eventually download. Try looking for a correlation with MPMediaItemPropertyIsCloudItem
You can also try getting info from the bounds... perhaps the bounds is 0x0 when the image is not found.
Related
I try to get file list from the Music Library (iPod Music Library), but I can't do it, my list is always empty. I sure that I have tracks in Music Library, I check it in other app - and it works. But as I remember that application sent me a request to access the Music Library. Perhaps I also need to create such a request? Help me solve the problem. I use this code to get file list:
func fetchFileList() {
let mediaItems = MPMediaQuery.songs().items
let mediaCollection = MPMediaItemCollection(items: mediaItems ?? [])
print("mediaCollectionItems: \(mediaCollection.items)") //It's always empty
//Then I'd like to get url of the track
//let item = mediaCollection.items[0]
//let pathURL = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
//print("pathURL: \(pathURL)")
}
If you want to access the Music Library, you have to add NSAppleMusicUsageDescription key to your Info.plist with a description about what you want to do with the music.
Se apple documentation for more info: MediaPlayer Documentation
I having problem on displaying the item artwork of MPMediaItem.
Here is the code for setting the music cover image:
- (void)updateMusicInfo
{
if(_localSongList.count != 0)
{
_currentTrackNumber = [DataHolder getCurrentTrackNumber];
MPMediaItem *mediaItem = [_localSongList objectAtIndex:_currentTrackNumber];
if(mediaItem != nil)
{
MPMediaItemArtwork *itemArtwork = [mediaItem valueForKey:MPMediaItemPropertyArtwork];
UIImage *iconImage;
if(itemArtwork == nil)
{
iconImage = [UIImage imageNamed:#"default_music_cover.png"];
}
else
{
iconImage = [itemArtwork imageWithSize:CGSizeMake(_ivSongCover.frame.size.height, _ivSongCover.frame.size.width)];
}
[_ivSongCover setImage:iconImage];
}
}
}
It seems that itemArtwork isn't null. Because if it is null, then my default music cover image should be used instead.
Here is the screenshot of the result of a blank music cover
In this case, I am hoping that itemArtwork should be a null so that I could use my default cover image. But in this case, it isn't null, worse, it doesn't even give me anything to display at all. How can I solve this one.
I am thinking of adding another logic, which is detecting the dominant colour of the current image used. If it consist of exactly no colour at all or just plain clear colour, then I would conclude that there is no music cover at all, and make use of the default one.
However, I want to know is there anything wrong with my code?
Take note: This song does not really have a cover image, BUT I wish that the itemArtwork should return a null, but it doesn't.
I hope I made myself clear. Please help. Thanks in advance!
I am developing a chat app using parse. I want to play the vodeo when users click on the video message and Display expandable image when the user click on the picture message. for that I need to differentiate image and video. Kindly guide me to do that...
Surely the easiest way will be of course to look at the file extension...?
For future googlers ... on didTapMessageBubbleAtIndexPath delegate you should check for item class
let message = yourMessageArray[indexPath.item]
if message.isMediaMessage() {
if message.media().isKindOfClass(JSQPhotoMediaItem) {
//Handle image
} else if message.media().isKindOfClass(JSQVideoMediaItem) {
let video = message.media() as! JSQVideoMediaItem
let videoURL = video.fileURL
}
}
Save this information in another field on Parse during the asset upload.
I can't retrieve MPMediaItemPropertyArtwork while playing a song from Apple Music with iOS 8.4
I've try to read image of nowPlayingItem
(lldb) po [[[MPMusicPlayerController systemMusicPlayer] nowPlayingItem] valueForProperty:MPMediaItemPropertyArtwork]
<MPConcreteMediaItemArtwork: 0x174478940>
But the object returned is empty:
(lldb) p (CGRect)[[[[MPMusicPlayerController systemMusicPlayer] nowPlayingItem] valueForProperty:MPMediaItemPropertyArtwork] bounds]
(CGRect) $2 = (origin = (x = 0, y = 0), size = (width = 0, height = 0))
And obviously the returned image is always nil
there's another way to get the nowPlayingItem MPMediaItemPropertyArtworkimage?
This behaviour happens when you are streaming songs from Apple Music which are not saved to the users library.
I've filed a bug report for this, and I think you should do too 😉 You can dupe rdar://25413082 if you want.
The best workaround is to use the iTunes API (or similar) to retrieve the album Art although this won't give perfect results 100% of the time.
I'm trying to print all the image metadata in the imagePickerController: didFinishPickingMediaWithInfo function. When I use the info.objectForKey(UIImagePickerControllerReferenceURL) method, it returns nil and if try to use this result my app crashes. Does anyone know why it returns nil and what else can I use to print all the image metadata when I pick an image? (using UIImageJPEGRepresentation is not an option because the EXIF data is removed).
This is my code:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!)
{
let image = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage
let refURL : NSURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL
var localSourceRef: CGImageSourceRef = CGImageSourceCreateWithURL(refURL, nil)
var localMetadata: NSDictionary = CGImageSourceCopyPropertiesAtIndex(localSourceRef, 0, nil)
println("\n Photo data: \n")
println(localMetadata)
}
So it sounds like there are actually two questions here:
1) Why is UIImagePickerControllerReferenceURL returning a nil reference?
2) How can you get location data from the photo?
So, the answer to (1) is usually because you receive the callback didFinishPickingMedia before the OS as written the file to the image library.
The answer to #2 is much trickier, as showcased by this question's line of answers:
Reading the GPS data from the image returned by the camera in iOS iphone
There are a number of variables you need to account for:
iOS will strip the GPS data out if you haven't requested access to location data, so you'll need to prompt for location access using CLLocationManager.
If the user has geotagging disabled, you'll never get GPS coords.
If the phone can't get a GPS lock, iOS won't record the GPS coords.
Per this answer: https://stackoverflow.com/a/10338012/490180 you should be able to retrieve the raw UIImage and then create the CGImageSourceRef from the data property off of UIImage's CGImage. This effectively removes the need for you to ever access the ReferenceURL.