In my application i have to make music library. So for that i have to fetch songs from Music Library. I done till this bye following.
Test_MusicLibraryAppDelegate *appDeleg = (Test_MusicLibraryAppDelegate *)[[UIApplication sharedApplication]delegate];
//NSMutableDictionary *musicList = [NSMutableDictionary dictionary];
MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query addFilterPredicate:[MPMediaPropertyPredicate
predicateWithValue:[NSNumber numberWithInteger:(MPMediaTypeMusic)]
forProperty:MPMediaItemPropertyMediaType]];
for (MPMediaItem* item in [query items]) {
[appDeleg.arryMusic addObject:item];
}
NSLog(@"Count :: %d ",[appDeleg.arryMusic count]);
[query release];
than display it in table view.
when i have to play song on selected index i fetch song name but don`t know how to play that song,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Test_MusicLibraryAppDelegate *appDeleg = (Test_MusicLibraryAppDelegate *)[[UIApplication sharedApplication]delegate];
// MPMediaItem *song = [appDeleg.arryMusic objectAtIndex:indexPath.row];
NSURL *aURL = [appDeleg.arryMusic objectAtIndex:indexPath.row];
//[song valueForProperty:MPMediaItemPropertyAssetURL];
[[self avPlayer] replaceCurrentItemWithPlayerItem:
[AVPlayerItem playerItemWithURL:aURL]];
[avPlayer play];
}
but my sound file not going to play...
Hey any one know about this than help me..
Thanks AJPatel
You are not storing a URL in your array, you are storing instances of MPMediaItem
What you need to do is extract the assets url from the media item. You can do this as follows ...
MPMediaItem *item = [appDeleg.arryMusic objectAtIndex:indexPath.row];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
[[self avPlayer] replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:url]];
[self.avPlayer play];
You have to initialize a player object(appdelegate will be a good position)
MPMusicPlayerController *musicPlayer; //in .h file
self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[self.musicPlayer setRepeatMode:MPMusicRepeatModeNone];
[self.musicPlayer setShuffleMode:MPMusicShuffleModeOff];
[self.musicPlayer beginGeneratingPlaybackNotifications];
Then have to maintain an array for holding media elements
MPMediaItemCollection *userMediaItemCollection;// in .h file @property (nonatomic, retain) MPMediaItemCollection *userMediaItemCollection; //in .h file
You have to add your media item to the array(input list) and add that to the mediacollection list
[self setUserMediaItemCollection: [MPMediaItemCollection collectionWithItems: (NSArray *) inputList]];
Add your media collection list to the player
[APP_DELEGATE.musicPlayer setQueueWithItemCollection: self.userMediaItemCollection];
Now play
[APP_DELEGATE.musicPlayer play];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.