swift锁屏播放,音乐进度更新,专辑,歌手名显示

    1. 我自己用的音乐播放器是自带的AVPlayer
    2. 导入头文件#import <MediaPlayer/MediaPlayer.h>
    3. 远程控制事件接收与处理
      - (void)viewWillAppear:(BOOL)animated
      {
      [super viewWillAppear:animated];
      [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
      [self becomeFirstResponder];
      }
      -(void)viewDidDisappear:(BOOL)animated{
          [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
      [self resignFirstResponder];
      }

      - (void)remoteControlReceivedWithEvent:(UIEvent *)event
      {
      if (event.type == UIEventTypeRemoteControl) {
              switch (event.subtype) {
      case UIEventSubtypeRemoteControlPlay:
                      [self play]; // 播放
                      break;

      case UIEventSubtypeRemoteControlPause:
                      [self pause];//暂停  
                      break;

      case UIEventSubtypeRemoteControlPreviousTrack:
                      [self forwardItem]; // 播放上一曲按钮
                      break;

      case UIEventSubtypeRemoteControlNextTrack:
                      [self nextItem]; // 播放下一曲按钮
                      break;

                  default:
                      break;
              }
          }
      }
    4. 传递信息到锁屏状态下- (void)configPlayingInfo 此方法在播放歌曲与切换歌曲时调用即可
      {
      if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
              if ((lastPlayItem != self.player.currentItem) && !isRepeat) {
                  lastPlayItem = self.player.currentItem;
      NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
                  [dict setObject:self.titleLabel.text forKey:MPMediaItemPropertyTitle];//歌曲名设置

                  [dict setObject:self.artistLabel.text forKey:MPMediaItemPropertyArtist];//歌手名设置


                  [dict setObject:[[MPMediaItemArtwork alloc] initWithImage:self.artwork.image]  forKey:MPMediaItemPropertyArtwork];//专辑图片设置

                  [dict setObject:[NSNumber numberWithDouble:CMTimeGetSeconds(self.player.currentItem.currentTime)] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; //音乐当前已经播放时间
                  [dict setObject:[NSNumber numberWithFloat:1.0] forKey:MPNowPlayingInfoPropertyPlaybackRate];//进度光标的速度 (这个随 自己的播放速率调整,我默认是原速播放)
                  [dict setObject:[NSNumber numberWithDouble:CMTimeGetSeconds(self.player.currentItem.duration)] forKey:MPMediaItemPropertyPlaybackDuration];//歌曲总时间设置
                  [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];

              }
      }
      }
    5. 有几个注意点是,每次你暂停时需要保存当前的音乐播放进度和锁屏下进度光标的速度设置为接近0的数(0.00001),以便下次恢复播放时锁屏下进度光标位置能正常。如下代码:NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo]];
              [dict setObject:[NSNumber numberWithDouble:CMTimeGetSeconds(CMTimeMakeWithSeconds((mSlider.value/timess)*timess, self.player.currentItem.currentTime.timescale))] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
              [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值