I have this code to find and play MPMediaItem:
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:self.persistentIDOfSongToPlay forProperty:MPMediaItemPropertyPersistentID comparisonType:MPMediaPredicateComparisonContains]; NSSet *predicateSet = [NSSet setWithObject:predicate]; MPMediaQuery *searchQuery = [[MPMediaQuery alloc] initWithFilterPredicates:predicateSet]; NSArray *queryResults = [searchQuery items]; NSLog(@"count: %i", queryResults.count); MPMediaItem *item = [queryResults objectAtIndex:0]; NSLog(@"item: %@", item); NSURL *itemURL = [item valueForProperty:MPMediaItemPropertyAssetURL]; NSLog(@"url: %@", itemURL); NSError *error; AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:itemURL error:&error]; [audioPlayer prepareToPlay]; [audioPlayer play]; NSLog(@"error: %@", error);
My journal:
count: 1 item: <MPConcreteMediaItem: 0x200b0870> 12385304089059716916 url: ipod-library:
But the sound does not play. I have a volume, and another AVAudioPlayer, which performs another function, later plays the sound.
source share