1.省略集成SDK环节,具体的集成以官方为主,这里只是快速接入的参考;
2.获取系统相机、麦克风、屏幕录制等的权限,这里以麦克风为例:
func checkMicroPermission() -> Bool { let authStatus = AVCaptureDevice.authorizationStatus(for: .audio) switch (authStatus) { case .authorized: //授权 return true case .notDetermined, .restricted, .denied: return false default: return false } }
根据用户设置询问是否授权
if !checkMicroPermission() { AVCaptureDevice.requestAccess(for: .audio) { granted in if granted { DispatchQueue.main.async { //授权成功后进行后续操作 } } }
}
3.直播封装
1.创建一个单例保持直播一直在运作 class LiveManager: NSObject { static let shared = LiveManager() //防止外界init private override init() { } } 2.创建直播推流及直播拉流 private var livePusher: V2TXLivePusher? private var livePlayer: V2TXLivePlayer = { let txPlayer = V2TXLivePlayer() return txPlayer }() 3.开启录屏直播(注:在此之前从后端拿到推流地址,pushUrl:推流地址) func startRecord(_ pushUrl: String) { if self.livePusher != nil { self.livePusher?.stopScreenCapture() self.livePusher?.stopPush() } //开启录屏 TRTCBroadcastExtensionLauncher.launch() //推流,liveMode选择RTC,因为后面要接入连麦功能的时候只能用这个,如果选RTMP连麦混流会有问题 let pusher = V2TXLivePusher(liveMode: .RTC) //注册观察者 pusher?.setObserver(self) self.livePusher = pusher //这里可以根据文档来填 pusher?.startScreenCapture("xx.xx.xx") pusher?.startMicrophone() let videoParam = V2TXLiveVideoEncoderParam() //这个resolution640x360根据自己的情况来,并不是越高越好 videoParam.videoResolution = .resolution640x360 //横竖屏模式,我们这边是录制游戏横屏,根据自己的情况设置 videoParam.videoResolutionMode = .landscape pusher?.setVideoQuality(videoParam) pusher?.setAudioQuality(.default) let code = pusher?.startPush(pushUrl) if code != V2TXLiveCode.TXLIVE_OK { //这里是失败的情况 pusher?.stopMicrophone() pusher?.stopScreenCapture() } }
然后是关闭直播
func stopRecord() { if self.livePusher != nil { self.livePusher?.stopScreenCapture() self.livePusher?.stopPush() } }
然后是客户端进行拉流操作
标签:return,pusher,self,直播,let,之录,livePusher,SDK From: https://www.cnblogs.com/wm941142146/p/18552288