- (void)viewDidLoad {
[super viewDidLoad];
mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAny];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES;
mediaPicker.editing = YES;
[self musicPlayerInitialize];
}
-(void)musicPlayerInitialize
{
myPlayer = [MPMusicPlayerController iPodMusicPlayer];
[myPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];
[myPlayer setRepeatMode:MPMusicRepeatModeAll];
[myPlayer beginGeneratingPlaybackNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeTitle)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:myPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleExternalVolumeChanged:)
name:MPMusicPlayerControllerVolumeDidChangeNotification
object:myPlayer];
}
-(void)changeTitle
{
MPMediaItem *item = [myPlayer nowPlayingItem];
NSString *titleStr = [item valueForProperty:MPMediaItemPropertyTitle];
lblSongTitle.text = titleStr;
NSLog(@"Now Playing Song Name: %@",titleStr);
}
-(IBAction)playButtonPressed
{
if(myPlayer.playbackState == MPMusicPlaybackStatePaused || myPlayer.playbackState == MPMusicPlaybackStateStopped)
{
[myPlayer play];
[btnMusicPlay setImage:[UIImage imageNamed:@"pause_btn.png"] forState:UIControlStateNormal];
}
else if(myPlayer.playbackState == MPMusicPlaybackStatePlaying)
{
[myPlayer pause];
[btnMusicPlay setImage:[UIImage imageNamed:@"play_btn.png"] forState:UIControlStateNormal];
}
[self changeTitle];
}
-(IBAction)nextButtonPressed
{
[myPlayer skipToNextItem];
[self changeTitle];
}
-(IBAction)prevButtonPressed
{
[myPlayer skipToPreviousItem];
[self changeTitle];
}
-(IBAction)volumeChange:(id)sender
{
[[NSUserDefaults standardUserDefaults] setFloat:myVolumeSlider.value forKey:@"musicVolume"];
myPlayer.volume = myVolumeSlider.value;
NSLog(@"volume changing: %f",myVolumeSlider.value);
}
- (void)handleExternalVolumeChanged:(id)notification {
// self.volumeSlider is a UISlider used to display music volume.
// self.musicPlayer.volume ranges from 0.0 to 1.0.
[myVolumeSlider setValue:myPlayer.volume animated:YES];
}
# pragma mark -
# pragma mark media picker Delegate
-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
[self dismissModalViewControllerAnimated:YES];
}
-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:mediaItemCollection];
MPMediaItemCollection *tempMediaItem = [tempArray objectAtIndex:0];
[appDelegate.myPlayer setQueueWithItemCollection:tempMediaItem];
[appDelegate.myPlayer play];
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction)playlistButtonPressed
{
[self presentModalViewController:mediaPicker animated:YES];
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment