iOS之基于FreeStreamer的简单音乐播放器示例

2020-01-21 02:24:13于海丽

这里继承了FSAudioStream,并且采用了单例模式


+ (instancetype)defaultPlayer
{
  static GLMusicPlayer *player = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    FSStreamConfiguration *config = [[FSStreamConfiguration alloc] init];
    config.httpConnectionBufferSize *=2;
    config.enableTimeAndPitchConversion = YES;
    
    
    player = [[super alloc] initWithConfiguration:config];
    player.delegate = (id)self;
    player.onFailure = ^(FSAudioStreamError error, NSString *errorDescription) {
      //播放错误
      //有待解决
    };
    player.onCompletion = ^{
      //播放完成
        NSLog(@" 打印信息: 播放完成1");
    };
  
    
    player.onStateChange = ^(FSAudioStreamState state) {
      switch (state) {
        case kFsAudioStreamPlaying:
        {
          NSLog(@" 打印信息 playing.....");
          player.isPause = NO;
          
          [GLMiniMusicView shareInstance].palyButton.selected = YES;
        }
          break;
        case kFsAudioStreamStopped:
        {
          NSLog(@" 打印信息 stop.....%@",player.url.absoluteString);
        }
          break;
        case kFsAudioStreamPaused:
        {
          //pause
          player.isPause = YES;
          [GLMiniMusicView shareInstance].palyButton.selected = NO;
            NSLog(@" 打印信息: pause");
        }
          break;
        case kFsAudioStreamPlaybackCompleted:
        {
          NSLog(@" 打印信息: 播放完成2");
          [player playMusicForState];
        }
          break;
        default:
          break;
      }
    };
    //设置音量
    [player setVolume:0.5];
    //设置播放速率
    [player setPlayRate:1];
    player.loopState = GLForeverLoop;
  });
  return player;
}

然后实现了播放方法


- (void)playFromURL:(NSURL *)url
{
  //根据地址 在本地找歌词
  NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"musiclist" ofType:@"plist"]];
  for (NSString *playStringKey in dic.allKeys) {
    if ([[dic valueForKey:playStringKey] isEqualToString:url.absoluteString]) {
      self.currentTitle = playStringKey;
      break;
    }
  }
  
  [self stop];
  if (![url.absoluteString isEqualToString:self.url.absoluteString]) {
    [super playFromURL:url];
  }else{
    [self play];
  }
  
  NSLog(@" 当前播放歌曲:%@",self.currentTitle);
  
  [GLMiniMusicView shareInstance].titleLable.text = self.currentTitle;
  
  //获取歌词
  NSString *lrcFile = [NSString stringWithFormat:@"%@.lrc",self.currentTitle];
  self.musicLRCArray = [NSMutableArray arrayWithArray:[GLMusicLRCModel musicLRCModelsWithLRCFileName:lrcFile]];
  
  if (![self.musicListArray containsObject:url]) {
    [self.musicListArray addObject:url];
  }
  
  //更新主界面歌词UI
  if (self.glPlayerDelegate && [self.glPlayerDelegate respondsToSelector:@selector(updateMusicLrc)])
  {
    [self.glPlayerDelegate updateMusicLrc];
  }
  _currentIndex = [self.musicListArray indexOfObject:url];
  
  if (!_progressTimer) {
    _progressTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateProgress)];
    [_progressTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
  }
}