3

我正在使用 AVPlayer 播放歌曲。我也可以在后台播放它。有一个UIButtonshowPlaylist. 当我点击它时,我需要显示我从中选择的歌曲列表,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;
  }
}
4

2 回答 2

3

我会查看 Apple 的示例代码“添加音乐”,它清楚地展示了如何执行您所描述的所有操作。

这个示例应用程序将向您介绍如何使用UITableView从 iPod 库中保存到MPMediaItemCollection. 它还显示了如何使用MPMediaItems以及MPMediaItemCollections您可以将轨道特定属性显示为单元格标题标签等。

  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:取消注释项目中的以下行:

- (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 文件中

在此处输入图像描述

于 2012-07-19T16:48:49.503 回答
1

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

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

至于你的其他问题,你应该看看这个:Play multiple audio files using AVAudioPlayer

只是让你知道,你真的不应该在同一个帖子中问多个问题。这使得给出简洁、有用的答案变得更加困难,并且会使网站更难找到内容。

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

于 2012-07-19T16:22:14.733 回答