Reputation: 16051
I have this code, to find and play a 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 log:
count: 1
item: <MPConcreteMediaItem: 0x200b0870> 12385304089059716916
url: ipod-library://item/item.m4a?id=-6061439984649834700
error: (null)
But the audio doesn't play. I have volume on, and another AVAudioPlayer, performing a different function, plays its audio fine later on.
Upvotes: 3
Views: 1695
Reputation: 1341
The AVAudioPlayer
must be declared as a property or ivar so it survives after the method is finalized.
Upvotes: 1