我正在使用AVPlayer播放歌曲。我也能夠在後臺播放它。有一個UIButton
調用showPlaylist
。當我點擊它時,我需要顯示我從ipodLibrary
在UITableView
中選擇的歌曲列表,並且我應該能夠顯示藝術作品,歌曲中剩餘的分鐘數以及藝術家的名字(如果歌曲中可用)。如何顯示從avplayer播放的ipodLibrary中選定的歌曲在uitableview中播放
我有播放和暫停按鈕:當我點擊暫停按鈕,歌曲暫停,但當我再次點擊播放按鈕時,它會進入ipodLibrary。當我點擊播放按鈕時如何恢復播放?
當UITableView
中有多首歌曲時,我希望它在第一首曲目完成後立即繼續下一首歌曲。我想知道如何做到這一點。
-(IBAction)playButtonPressed:(UIButton *)sender {
// Create picker view
MPMediaPickerController* picker = [[MPMediaPickerController alloc] init];
picker.delegate = self;
if (userMediaItemCollection) {
MusicTableViewController *controller = [[MusicTableViewController alloc]
initWithNibName: @"MusicTableView" bundle: nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController: controller animated: YES];
} else {
MPMediaPickerController *picker = [[MPMediaPickerController alloc]
initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
picker.prompt = NSLocalizedString
(@"Add songs to play", "Prompt in media item picker");
[[UIApplication sharedApplication] setStatusBarStyle:
UIStatusBarStyleDefault animated: YES];
[self presentModalViewController: picker animated: YES];
}
}
-(IBAction)pauseButtonPressed:(UIButton *)sender {
[myPlayer pause];
}
-(void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:
(MPMediaItemCollection *)mediaItemCollection {
[self dismissViewControllerAnimated:YES completion:nil];
NSURL* assetUrl = [mediaItemCollection.representativeItem
valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:assetUrl options:nil];
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
myPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[myPlayer play];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
-(void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
if (myPlayer.rate == 0.0) {
[myPlayer play];
} else {
[myPlayer pause];
}
break;
case UIEventSubtypeRemoteControlPlay:
[myPlayer play];
break;
case UIEventSubtypeRemoteControlPause:
[myPlayer pause];
break;
default:
break;
}
}
你可能想看看這個問題,關於你的遠程事件問題http://stackoverflow.com/questions/11566512/how-to-choose-next-song-in-remotecontrolledevents/11567222#11567222 – Dustin 2012-07-19 18:42:13