如何在uitableview中显示从avplayer播放的ipodLibrary中选择的歌曲

我正在使用AVPlayer播放歌曲。 我也可以在后台播放它。 有一个名为showPlaylistUIButton 。 当我点击它时,我需要显示我在UITableViewipodLibrary中选择的歌曲列表,我应该能够显示艺术作品,歌曲中剩余的分钟数以及歌曲中可用的艺术家姓名。

我有播放和暂停按钮:当我点击暂停按钮时,歌曲暂停,但当我再次点击播放按钮时,它会转到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; } } 

我会查看Apple的示例代码“添加音乐” ,它清楚地演示了如何完成您所描述的所有内容。

此示例应用程序将指导您完成填充UITableView ,其中包含保存在MPMediaItemCollection的iPod库中所选歌曲的可变副本的内容。 它还显示了如何使用MPMediaItemsMPMediaItemCollections将跟踪特定属性显示为单元格标题标签等。

  1. 要填充表视图,您可以将其设置为:

    MPMediaItem *mediaItem = (MPMediaItem *)[collectionMutableCopy objectAtIndex:row];

    MPMediaItemArtwork *artwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];

     if (mediaItem) { cell.textLabel.text = [mediaItem valueForProperty:MPMediaItemPropertyTitle]; cell.detailTextLabel.text = [mediaItem valueForProperty:MPMediaItemPropertyArtist]; if (artwork != nil) { cell.imageView.image = [artwork imageWithSize:CGSizeMake (40, 40)]; }else{ cell.imageView.image = [UIImage imageNamed:@"no_artwork.png"]; } } 
  2. 将链接到播放按钮的按钮重命名为“选择音乐”,并创建一个名为“播放”的新按钮,其操作设置为:

    - (IBAction)playMusic { [myPlayer play]; }

编辑:MPMediaItemCollection内容创建数组:

 NSMutableArray *collectionMutableCopy = [[NSMutableArray alloc] initWithArray:myMediaItemCollection.items]; 

编辑2:取消注释项目中的以下行:

In - (void) registerForMediaPlayerNotifications

 /* // This sample doesn't use libray change notifications; this code is here to show how // it's done if you need it. [[NSNotificationCenter defaultCenter] removeObserver: self name: MPMediaLibraryDidChangeNotification object: musicPlayer]; [[MPMediaLibrary defaultMediaLibrary] endGeneratingLibraryChangeNotifications]; */ 

- (void)dealloc

 /* // This sample doesn't use libray change notifications; this code is here to show how // it's done if you need it. [notificationCenter addObserver: self selector: @selector (handle_iPodLibraryChanged:) name: MPMediaLibraryDidChangeNotification object: musicPlayer]; [[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications]; */ 

替换此行:

 [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; 

有了这个

  [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil]; UInt32 doSetProperty = 0; AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (doSetProperty), &doSetProperty ); 

然后将以下内容添加到项目info.plist文件中

在此处输入图像描述

你的问题是你的播放按钮正在做的事情不仅仅是告诉音乐播放器播放。 你需要一个只调用[myPlayer play] ; 在播放按钮以外的地方进行所有设置。

让你的播放按钮也选择歌曲并不是一个很好的编码练习(最好保持一个按钮的想法 – >一个function)。 所以有一个按钮可以立即执行播放按钮中的操作,然后让您的播放按钮调用[myPlayer play]

至于你的另一个问题,你应该看一下: 使用AVAudioPlayer播放多个音频文件

你知道,你真的不应该在同一篇文章中提出多个问题。 它使得提供简洁,有用的答案变得更加困难,并且可以使网站更难找到。

我不确定你是否在询问如何显示歌曲信息,但我想这是在你的标题中。 对于这个, iPhone sdk – 通过应用程序访问当前歌曲信息 。