ios如何实现流媒体播放器
更新:HHH   时间:2023-1-8


这篇文章主要为大家展示了“ios如何实现流媒体播放器”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“ios如何实现流媒体播放器”这篇文章吧。

演示效果

附上项目地址 chenfengxiaoxixi

实现功能

实现了流媒体音乐播放,后台持续播放,歌曲切换,进度条显示以及快进后退等功能。

实现技术点及流程

1.单例

播放器所在controller我是使用单例初始化的,不然pop到上一级控制器后,当前对象释放掉,就无法播放了

+ (instancetype)sharePlayerController
{
  @synchronized(self)
  {
    static CFPlayerController *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
      _instance = [[self alloc] init];
    });
    
    return _instance;
  }
}

这里使用了线程同步,避免由卡顿造成的多次初始化。

2.后台持续播放

先在xcode配置里面(TARGETS->Capabilities)打开Background Modes,勾选上Audio那一栏。现在只是满足了后台播放条件,要想连续不断在后台播放,还要申请后台任务id。

//添加后台播放任务
  UIBackgroundTaskIdentifier bgTask = 0;
  if([UIApplication sharedApplication].applicationState== UIApplicationStateBackground) {    
    NSLog(@"后台播放");    
    UIApplication*app = [UIApplication sharedApplication];    
    UIBackgroundTaskIdentifier newTask = [app beginBackgroundTaskWithExpirationHandler:nil];    
    if(bgTask!= UIBackgroundTaskInvalid) {      
      [app endBackgroundTask: bgTask];
    }
    
    bgTask = newTask;
    [self next];
  }
  else {    
    NSLog(@"前台播放");
    [self.cdView scrollRightWIthNext];
    
  }

播放完成一首歌后,这段代码用来判断当前处于前台还是后台,如果是后台,那就申请后台任务继续播放下一首。

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];

快进后退播放

- (void)dragSliderEnd:(UISlider *)slider{
  //滑动到底时,播放下一曲
  if (slider.value == 1) {
     [self.cdView scrollRightWIthNext];
  }
  else
  {
    if (slider.value > 0)
    {
      //初始化一个FSStreamPosition结构体
      FSStreamPosition pos;
      //只对position赋值,value由slider控制
      pos.position = slider.value;
      [self.audioStream seekToPosition:pos];// 到指定位置播放
    }
  }
}

以上是“ios如何实现流媒体播放器”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注天达云行业资讯频道!

返回移动开发教程...