URL for MPMediaItem

I have a simple question, but I can not find the right answer. I have a song url stored in my database. Way Like This.

aSound.path = [[item valueForProperty: MPMediaItemPropertyAssetURL] absoluteString]; 

How to convert back to an MPMediaItem object that has a song artist and artwork?

Is it possible?

Thanks for answers.

+6
source share
2 answers

Preservation:

  NSNumber* persistentID = [mediaItem valueForProperty:MPMediaItemPropertyPersistentID]; 

Loading:

  MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate predicateWithValue:persistentID forProperty:MPMediaItemPropertyPersistentID]; 

Another example: NSNumber for MPMediaItemPropertyPersistentID for NSString and vice versa

Song song URLs are unreliable because any DRM song returns a null URL

+4
source

Alternatively, a nasty hack that works for me (tm).

 - (MPMediaItem *)getMediaItemForURL:(NSURL *)url { // We're going to assume that the last query value in the URL is the media item persistent ID and query off that. NSString *queryString = [url query]; if (queryString == nil) // shouldn't happen return nil; NSArray *components = [queryString componentsSeparatedByString:@"="]; if ([components count] < 2) // also shouldn't happen return nil; id trackId = [components objectAtIndex:1]; MPMediaQuery *query = [[MPMediaQuery alloc] init]; [query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:trackId forProperty:MPMediaItemPropertyPersistentID]]; NSArray *items = [query items]; if ([items count] < 1) // still shouldn't happen return nil; return [items objectAtIndex:0]; } 

Comments appreciated where this might go wrong, or how to improve it. I prefer to pass the URL instead of PersistentID, as I have several media sources, all of which can use URLs. Using PersistentID will mean a lot "if it is a media identifier, do it, otherwise do it using a URL."

+3
source

Source: https://habr.com/ru/post/911757/


All Articles