微信搜索superit|邀请体验:大数据, 数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

Swift 直播项目(推流、拉流)

IOS aide_941 25℃

Swift 直播项目(推流、拉流)

96

东城夕阳 

 0.2 2017.10.23 23:09* 字数 285 阅读 1461评论 1

直播项目中关键的是推流和拉流,对于推流推流拉流大厂当然可以自己开发,但是像我一样在公司独立开发的,是没有那个时间和精力去独立开发一套推流拉流的(不是没时间和精力,是因为没那个能力TnT),所以我们决定用别人的轮子

推流拉流的SDK非常多,我的项目推流用的是LFLiveKit(Adobe开源项目),拉流用的是IJKMediaFramework(B站开源项目)
之前用过阿里云的SDK,但是用起来差的一笔,不知道是不是我打开的方式不对

LFLiveKit可以用pod, IJKMediaFramework没有pod需要拖到项目里

LFLiveKit使用

 lazy var videoConfiguration : LFLiveVideoConfiguration = {
       
        let videoConfiguration = LFLiveVideoConfiguration()
        
        videoConfiguration.videoSize = CGSize.init(width: 540, height: 960)
        
        videoConfiguration.videoBitRate = 800*1024
        
        videoConfiguration.videoMaxBitRate = 1000*1024
        
        videoConfiguration.videoMinBitRate = 500*1024
        
        videoConfiguration.videoFrameRate = 20
        
        videoConfiguration.videoMaxKeyframeInterval = 40
        
        videoConfiguration.outputImageOrientation = .portrait
        
        videoConfiguration.sessionPreset =  .captureSessionPreset720x1280
        
        return videoConfiguration
    }()
    
    lazy var audioConfiguration : LFLiveAudioConfiguration = {
        
        let audioConfiguration = LFLiveAudioConfiguration()
        
        audioConfiguration.numberOfChannels = 2
        
        audioConfiguration.audioBitrate = ._128Kbps
        
        audioConfiguration.audioSampleRate =  ._44100Hz
        
        return audioConfiguration
    }()
    
    lazy var session : LFLiveSession = { 
       
        let session = LFLiveSession.init(audioConfiguration: self.audioConfiguration, videoConfiguration: self.videoConfiguration)
        
        session?.preView = self.preview
        
        session?.delegate = self

        session?.running = true
        
        session?.showDebugInfo = true
        
        return session!
    }()

videoConfiguration , audioConfiguration, session 分别是视频配置,音频配置,会话管理
session?.preView = self.preview为显示推流视频的view

推流

  let stream = LFLiveStreamInfo()
  stream.url = rtmpUrl
  session.startLive(stream)
     

rtmpUrl是后台人员给客户端的推流地址

结束推流

 session.stopLive()

美白

 func beautyWhite(value : Float)  {
        //value  白程度
        session.brightLevel = CGFloat(value)
}

磨皮

 func beautyBuffing(value : Float) {
     //value  磨皮成都
        session.beautyLevel = CGFloat(value)
}

切换摄像头

 let devicePosition = session.captureDevicePosition
 session.captureDevicePosition = (devicePosition == .back) ? .front : .back

打开闪光灯

       //闪光灯只有后置摄像头有
  if session.captureDevicePosition == .front {
 return
      
  }
 session.torch = !session.torch
  sender.isSelected = !sender.isSelected

静音

session.muted = true

代理方法

/**  推流状态有变化回调*/
- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange:(LFLiveState)state;
/** 推流信息,码率分辨率等 */
- (void)liveSession:(nullable LFLiveSession *)session debugInfo:(nullable LFLiveDebug *)debugInfo;
/** 推流错误码 */
- (void)liveSession:(nullable LFLiveSession *)session errorCode:(LFLiveSocketErrorCode)errorCode;

IJKMediaFramework使用

 let options = IJKFFOptions.byDefault()
        
//视频源地址
  let url = playerUrl
        
       
 //初始化播放器,播放在线视频或直播(RTMP)
        
player = IJKFFMoviePlayerController(contentURL: URL.init(string: url), with: options)
       
 //播放页面视图宽高自适应
       
 let autoresize = UIViewAutoresizing.flexibleWidth.rawValue |
  UIViewAutoresizing.flexibleHeight.rawValue
player?.view.autoresizingMask = UIViewAutoresizing(rawValue: autoresize)
player?.view.frame = self.preview.bounds
player?.scalingMode = .aspectFit //缩放模式
player?.shouldAutoplay = true //开启自动播放
self.view.autoresizesSubviews = true
self.preview.addSubview((player?.view)!)
 player?.prepareToPlay()

开始拉流

 createPlayer(playerUrl: url)
  player?.play()

结束拉流

player?.stop()
 player?.shutdown()

拉流状态改变通知

 NotificationCenter.default.addObserver(self, selector:#selector(ijkMediaPlayerStatusChanged(notif:)), name:NSNotification.Name.IJKMPMoviePlayerPlaybackDidFinish, object: nil)

转载请注明:SuperIT » Swift 直播项目(推流、拉流)

喜欢 (0)or分享 (0)