I was looking at Apple's documentation, and I cannot seem to find a way to get whether or not an MPMediaItem is a 'favorite' track or not. See screenshot below, with the pink heart.
How can one get this property? I know since it's a new feature, it's availability would be limited to iOS 8.4 or later.
Here's some code I'm using to get other properties from MPMediaItems, via the music picker:
- (void) processMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
//iterate through selected songs
if (mediaItemCollection) {
NSArray *allSelectedSongs = [mediaItemCollection items];
for(MPMediaItem *song in allSelectedSongs)
{
NSURL *songURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
NSNumber *ident = [song valueForProperty:MPMediaEntityPropertyPersistentID];
NSString *identString = [BukketHelper convertULLToNSString:ident];
NSNumber *isCloud = [song valueForProperty:MPMediaItemPropertyIsCloudItem];
}
//do other stuff here
}
Anyone have ideas?
You have to use Apple Music API to get or set users's Like/Dislike to a song like this:
GET https://api.music.apple.com/v1/me/ratings/songs/{id}
From: Apple Docs link
Related
I am trying to get the video name from the metadata of a video source file in IOS app using Objective c, Cocoa Framework. I am using the following code
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
NSArray *metadata = [playerItem.asset metadata];
this url is #"http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"
I used the same AvPlayerItem class for retrieving the metadata from an audio file as this below:
NSArray *metadata = [playerItem.asset metadata];
for (AVMetadataItem *metadataItem in metadata)
{
NSLog(#"%#",metadataItem.commonKey);
[metadataItem loadValuesAsynchronouslyForKeys:#[AVMetadataKeySpaceCommon] completionHandler:^{
if ([metadataItem.commonKey isEqualToString:#"title"])
{
title = (NSString *)metadataItem.value;
}
}];
}
Here the commonkey 'title' will help me get the audio file name from metadata, but in case of video the same does not work. While debugging I am getting array as below
<__NSArrayM 0x14c777b50>(
As you can see, the commonKey = software whereas it should have been 'title'. Is there any way to get the video name from the metadata? Thanks.
I am a newbie iOS learner. Couldn't find answer to following question after searching for a while. Hence, here it is.
Building on a first app that displays recent pictures from a user's instagram feed, I am trying to display pictures from the follows of that users instead.
To call recent pictures from the Instagram feed, which worked well, I had created the following method "imageForPhoto"
+ (void)imageForPhoto:(NSDictionary *)photo size:(NSString *)size completion:(void(^)(UIImage *image))completion {
if (photo == nil || size == nil || completion == nil) {
return;
}
NSString *key = [[NSString alloc] initWithFormat:#"%#-%#", photo[#"id"], size];
NSURL *url = [[NSURL alloc] initWithString:photo[#"images"][size][#"url"]];
[self downloadURL:url key:key completion:completion];
}
Therefore, I first modified my code to get the data related to the "follows" in my PhotosViewController instead of recent media pictures ( v1/users/3/follows ):
NSURLSession *session = [NSURLSession sharedSession];
NSString *urlString = [[NSString alloc] initWithFormat:#"https://api.instagram.com/v1/users/3/follows?count=99&access_token=%#", self.accessToken];
Then, I created a new method that I called friendAvatarForPhoto to get the follows profile pictures from a photo NSDictionary that is passed in as the only method parameter. I placed this method in my PhotoController class:
+ (void)friendAvatarForPhoto:(NSDictionary *)photo completion:(void(^)(UIImage *image))completion {
if (photo == nil || completion == nil) {
return;
}
NSString *key = [[NSString alloc] initWithFormat:#"avatar-%#", photo[#"user"][#"id"]];
NSURL *url = [[NSURL alloc] initWithString:photo[#"profile_picture"]];
[self downloadURL:url key:key completion:completion];
}
It seems to work. I have manually checked that the pictures that are rendered on my UICollectionView are actually coming from the "profile_picture" key of the responseDictionary I get back from Instagram.
Here is the structure of this dictionary: https://www.dropbox.com/s/ldjqupadqg0j3nq/friends_response_dictionary.png
Specifically, as you can see, I modified both the key and the url:
NSString *key = [[NSString alloc] initWithFormat:#"avatar-%#", photo[#"user"][#"id"]];
NSURL *url = [[NSURL alloc] initWithString:photo[#"profile_picture"]];
but I am not really understanding what I need to initial the *key string with... I kept "avatar" for example but why should I? I am pretty sure it's wrong although the result seems to be fine from what is returned on my UICollectionView.
What should I initiate this string with instead?
What is the function of this key in the overall NSURLSession process?
I'd love to better understand the overall process and better connect the dots with Instagram's API so I can query the right key and be sure I am getting what I am looking for. In a reliable way, not an intuitive one as I have just done app-arently.
Any help from experienced developers in the community would be welcome, I've just started to explore iOS dev a few weeks ago based on rusty C skills from a long time back.
Thank you!
:) Arsene
Thanks for noticing this question. I want to do something about music recommendation, and what I am doing now is leveraging MPNowPlayingInfoCenter's nowPlayingInfo, like this:
NSDictionary *metaData = [[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo];
NSString *songTitle = metaData[MPMediaItemPropertyTitle];
NSString *albumnTitle = metaData[MPMediaItemPropertyAlbumTitle];
NSString *artist = metaData[MPMediaItemPropertyArtist];
But it always returns nil when "Music" app is playing music in background.
I looked up the related documents, it says
MPNowPlayingInfoCenter provides an interface for setting the current now
playing information for the application.
The default center holds now playing info about the current application
Seems there is no way to get other app's nowPlayingInfo through MPNowPlayingInfoCenter. So are there any other ways to get other app's music meta data displayed in remote control/lock screen? Thanks!
You can get what iPod currently is playing
MPMusicPlayerController* player = [MPMusicPlayerController iPodMusicPlayer];
//get now playing item
MPMediaItem*item = [player nowPlayingItem];
// get the title of song
NSString* titleStr = [item valueForProperty:MPMediaItemPropertyTitle];
NSLog(#"titlestr %#",titleStr);
This is how I've code to open media picker list
- (void)viewDidLoad
{
[super viewDidLoad];
player=[MPMusicPlayerController iPodMusicPlayer];
picker=[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
[picker setDelegate:self];
picker.prompt=#"Add an audio to application";
}
I've also implemented its delegate method
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
/*I know only here I can get path but don't know how?
mediaItemCollection don't have such kind of properties or any
method to get selected file path.
So is there any other way to do so?
Note: I required this path, so that I can store it somewhere and can play it in future.
*/
}
Initialize a MPMediaItem object from some index of the mediaItemCollection.items array:
MPMediaItem *anItem = (MPMediaItem *)[mediaItemCollection.items objectAtIndex: row];
Then call the -valueForProperty: method:
NSURL *assetURL = [anItem valueForProperty: MPMediaItemPropertyAssetURL];
There are other properties that you can get from the MPMediaItem described here:http://bit.ly/GGs3XI
Look under "General Media Item Property Keys"
Hope this helps!
Tams
I would like to get the file name and, if possible, album image from a streaming URL in a AVPlayerItem that I am playing with AVQueuePlayer but I don't know how to go about doing this.
Also if it turns out that my streaming URL doesn't have any metadata can I put metadata in my NSURL* before passing it to the AVPlayerItem?
Thanks.
Well I am surprised no one has answered this question.
In fact no one has answered any of my other questions.
Makes me wonder how much knowledge people in here truly have.
Anyways, I will go ahead and answer my own question.
I found out how to get the metadata by doing the following:
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
NSArray *metadataList = [playerItem.asset commonMetadata];
for (AVMetadataItem *metaItem in metadataList) {
NSLog(#"%#",[metaItem commonKey]);
}
Which gives me a list as follows:
title
creationDate
artwork
albumName
artist
With that list now I know how to access the metadata from my audio stream. Just simply go through the NSArray and look for an AVMetadataItem that has the commonKey that I want (for example, title). Then when I find the AVMetadataItem just get the value property from it.
Now, this works great but it may be possible that when you try to get the data it will take a while. You can load the data asynchronously by sending loadValuesAsynchronouslyForKeys:completionHandler: to the AVMetadataItem you just found.
Hope that helps to anyone who may find themselves with the same problem.
When retrieving a particular item I would use the Metadata common keys constant declared in AVMetadataFormat.h, i.e.: AVMetadataCommonKeyTitle.
NSUInteger titleIndex = [avItem.asset.commonMetadata indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
AVMutableMetadataItem *metaItem = (AVMutableMetadataItem *)obj;
if ([metaItem.commonKey isEqualToString:AVMetadataCommonKeyTitle]) {
return YES;
}
return NO;
}];
AVMutableMetadataItem *item = [avItem.asset.commonMetadata objectAtIndex:titleIndex];
NSString *title = (NSString *)item.value;