播放器后台播放

  • (void) viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
    }

  • (void) viewWillDisappear:(BOOL)animated
    {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
    }

pragma mark - Player Controll

  • (void)remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
    NSMutableDictionary * info = [NSMutableDictionary dictionary];
    //音乐的标题
    [info setObject:@"leao" forKey:MPMediaItemPropertyTitle];
    //音乐的艺术家
    [info setObject:@"chocolate" forKey:MPMediaItemPropertyArtist];
    //音乐的播放时间
    [info setObject:@(self.audioPlayer.duration) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    //音乐的播放速度
    [info setObject:@(1) forKey:MPNowPlayingInfoPropertyPlaybackRate];
    //音乐的总时间
    [info setObject:@(self.audioPlayer.duration) forKey:MPMediaItemPropertyPlaybackDuration];
    //音乐的封面
    MPMediaItemArtwork * artwork = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"main_icon_review"]];
    [info setObject:artwork forKey:MPMediaItemPropertyArtwork];
    //完成设置
    [[MPNowPlayingInfoCenter defaultCenter]setNowPlayingInfo:info];

    if (receivedEvent.type == UIEventTypeRemoteControl) {

      switch (receivedEvent.subtype) {
    
          case UIEventSubtypeRemoteControlTogglePlayPause:
    
              break;
    
          case UIEventSubtypeRemoteControlPreviousTrack:
    
              break;
    
          case UIEventSubtypeRemoteControlNextTrack:
    
              break;
    
          default:
              break;
      }
    

    }
    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容