YOU APPLICATION --------------> MUSIC PLAYER(music play back)
| |
MEDIA PICKER MEDIA QUERY
| |
iPod Library
(data base access)
Music Player :
There are 2 flavors of music players available:
- Application music player : Plays music locally within your application.It does not effect built in iPod app
- iPod music player : In effect,employes the built-in ipod app on your behalf.When auger quits an app that is using this player,the music continues to play.
Creating iPodMusicPlayer:
MPMusicPlayerController* iPodMusicPlyer = [MPMusicPlyerController iPodMusicPlayer];
Data base access:
2 ways to retrieve items(data base access)
- The media item picker : It is an easy to use, pre-packaged view controller that behaves like the built-in iPod application's music selection interface.
- The media Query : To provide the specialized access control that you want, the media query interface.
Using the Media Item Picker:
MPMediaPickerController : It is a specialized view controller that you employ to provide a graphical interface for selecting media items.To respond to user selections and to dismiss a media item picker, use the MPMediaPickerControllerDelegate protocol.
MPMediaPickerControllerDelegate protocol : The delegate for a media item picker can respond to a user making media item selections.The delegate is also responsible for dismissing the media item picker from the parent view controller.
Steps for using media item picker:
- Designate a controller object as a delegate of the picker.
- Invoke the picker from the controller.
- When the use indicates that they are finished, the delegate receives the chosen collection of media items and dismiss the picker.
STEP1 : Add the protocol name in the interface declaration.
@interface myController : UIViewController { // interface declaration
}
STEP2 : Invoke the picker from controller
- Create a media item picker .MPMediaPickerController* picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
- Establishes your controller objects the delegate.[picker setDelegate:self];
- Specifies the user can pick multiple items. [picker setAllowsPickingMultipleItems: YES];
- Optional setting A prompt, for user,that appears above the navigation bar.picker.prompt = NSLocalizedString(@"Add songs to play",@"prompt in media item picker");
- Display the picker.[self presentModalViewController:picker animated: YES];[picker release];
STEP3 : Implement 2 delegate methods("mediaPicker:didPickMediaItems:" and "mediaPickerDidCancel") to suspend picker controller.
Useful methods:
mediaPicker:didPickMediaItems:
Called when a user has selected a set of media items.
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
mediaPickerDidCancel:
Called when a user dismisses a media item picker by tapping Cancel.
[self dismissModalViewControllerAnimated:YES]
Updating playback :
Code here is self explanatory.
- (void) updatePlayerQueueWithMediaCollection: (MPMediaItemCollection *) mediaItemCollection {
// Configure the music player, but only if the user chose at least one song to play
if (mediaItemCollection) {
// If there's no playback queue yet...
if (userMediaItemCollection == nil) {
// apply the new media item collection as a playback queue for the music player
[self setUserMediaItemCollection: mediaItemCollection];
[musicPlayer setQueueWithItemCollection: userMediaItemCollection];
[musicPlayer play];
// Obtain the music player's state so it can then be
// restored after updating the playback queue.
} else {
// Take note of whether or not the music player is playing. If it is
// it needs to be started again at the end of this method.
BOOL wasPlaying = NO;
if (musicPlayer.playbackState == MPMusicPlaybackStatePlaying) {
wasPlaying = YES;
}
// Save the now-playing item and its current playback time.
MPMediaItem *nowPlayingItem = musicPlayer.nowPlayingItem;
NSTimeInterval currentPlaybackTime = musicPlayer.currentPlaybackTime;
// Combine the previously-existing media item collection with the new one
NSMutableArray *combinedMediaItems = [[userMediaItemCollection items] mutableCopy];
NSArray *newMediaItems = [mediaItemCollection items];
[combinedMediaItems addObjectsFromArray: newMediaItems];
[self setUserMediaItemCollection: [MPMediaItemCollection collectionWithItems: (NSArray *) combinedMediaItems]];
[combinedMediaItems release];
// Apply the new media item collection as a playback queue for the music player.
[musicPlayer setQueueWithItemCollection: userMediaItemCollection];
// Restore the now-playing item and its current playback time.
musicPlayer.nowPlayingItem = nowPlayingItem;
musicPlayer.currentPlaybackTime = currentPlaybackTime;
// If the music player was playing, get it playing again.
if (wasPlaying) {
[musicPlayer play];
}
}
}
}
