我用AVPlayer播放歌曲。 我可以在后台播放它。 有一个UIButton
叫showPlaylist
。 当我在敲打我需要显示的我从所选歌曲列表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;
}
}