ios 流媒体播放器实现流程及FreeStreamer的使用的示例

2020-01-21 03:53:28王振洲

3.锁屏后对音乐播放的操作及信息显示

需要重写remoteControlReceivedWithEvent,用来获取锁屏后对播放器的操作


- (void)remoteControlReceivedWithEvent: (UIEvent *) receivedEvent
{
  [CF_NOTI_CENTER postNotificationName:@"remoteControl" object:nil userInfo:@{@"event":receivedEvent}];
}

该通知发送到播放控制器,在播放控制器实现处理逻辑


- (void)remoteControl:(NSNotification *)note
{
  UIEvent *receivedEvent = note.userInfo[@"event"];
  if (receivedEvent.type == UIEventTypeRemoteControl)
  {
    switch (receivedEvent.subtype)
    {
      case UIEventSubtypeRemoteControlTogglePlayPause:
            [self.audioStream stop];
      break;
      case UIEventSubtypeRemoteControlPreviousTrack:

            [self.cdView scrollLeftWithPrev];
      break;
      case UIEventSubtypeRemoteControlNextTrack:
            [self.cdView scrollRightWIthNext];
      break;        
      case UIEventSubtypeRemoteControlPlay:
            [self.cdView playOrPause];
      break;        
      case UIEventSubtypeRemoteControlPause:
            //暂停歌曲时,动画也要暂停
            [self.cdView playOrPause];
      break;      
      default:
      break;
    }
  }
}

更新锁屏后音乐的显示信息


//锁屏显示信息
- (void)configNowPlayingInfoCenter
{
  if (NSClassFromString(@"MPNowPlayingInfoCenter")) {    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];    
    [dict setObject:CFUSER.currentSong.songName forKey:MPMediaItemPropertyTitle];    
    [dict setObject:@(self.playTime)forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    //音乐的总时间
    [dict setObject:@(self.totalTime)forKey:MPMediaItemPropertyPlaybackDuration];    
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];    
  }
}

4.关于FreeStreamer的使用

初始化,开始播放


- (void)buildStreamer
{
  weakSELF;
  // 网络文件
  NSURL *url = [NSURL URLWithString:CFUSER.currentSong.url];
  
  if (!_audioStream) {
    _audioStream = [[FSAudioStream alloc] initWithUrl:url];
    _audioStream.onFailure = ^(FSAudioStreamError error,NSString *description){
      NSLog(@"播放过程中发生错误,错误信息:%@",description);
      [weakSelf showAlertMsg:description];
    };
    _audioStream.onCompletion=^(){
      //播放完成后,执行下一步
      [weakSelf autoPlayNext];
    };
    
    // 设置声音
    [_audioStream setVolume:1];
    //开始播放
    [_audioStream play];
  }
  else
  {
    _audioStream.url = url;
    [_audioStream play];
  }
}

停止播放


[self.audioStream stop];

暂停播放和继续播放为同一个方法,别问为什么,作者就是这样写的


[self.audioStream pause];