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