我正在使用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