i want to select songs from ipod Library and play it using avplayer i want the music to continue playing even after the app goes to background i am new to iOS programming can anyone help me out ..
Thanks
i want to select songs from ipod Library and play it using avplayer i want the music to continue playing even after the app goes to background i am new to iOS programming can anyone help me out ..
Thanks
To allow the user to pick a song (or songs) from their music library, use the MPMediaPickerController
class.
-(void) pickSong {
// Create picker view
MPMediaPickerController* picker = [[MPMediaPickerController alloc] init];
picker.delegate = self;
// Check how to display
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
// Show in popover
[popover dismissPopoverAnimated:YES];
popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
// Present modally
[self presentViewController:picker animated:YES completion:nil];
}
}
Change self.navigationItem.rightBarButtonItem
if you're not showing it from a button on the right side of title bar.
Then you need to listen for the result by implementing the delegate:
Called when the user cancelled the selection:
-(void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
// Dismiss selection view
[self dismissViewControllerAnimated:YES completion:nil];
[popover dismissPopoverAnimated:YES];
popover = nil;
}
Called when the user chose something:
-(void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
// Dismiss selection view
[self dismissViewControllerAnimated:YES completion:nil];
[popover dismissPopoverAnimated:YES];
popover = nil;
// Get AVAsset
NSURL* assetUrl = [mediaItemCollection.representativeItem valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:assetUrl options:nil];
// Create player item
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
// Play it
AVPlayer* myPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[myPlayer play];
}
You'll need a UIPopoverController* popover;
in your class .h file. Also you should retain myPlayer
somewhere...
To allow music to continue in the background, add an audio
string to the array in your Info.plist under the UIBackgroundModes
key.