腾讯直播SDK接入及封装(一)之录屏直播(推拉流)
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() } }
然后是客户端进行拉流操作
//拉流 public func anchorStartPlayer(playURL: String, streamId: String, mainstreamId: String, mainUserId: String, userId: String) { // 不接入连麦功能的话直接指定view就可以了 // self.livePlayer.setRenderView(renderView) self.livePlayer.startLivePlay(playURL) let config = V2TXLiveTranscodingConfig() config.videoWidth = 720 config.videoHeight = 1280 config.videoBitrate = 1500 config.videoFramerate = 20 config.videoGOP = 2 config.audioSampleRate = 48000 config.audioBitrate = 64 config.audioChannels = 1 config.outputStreamId = nil let mainStream = V2TXLiveMixStream() mainStream.streamId = mainstreamId mainStream.userId = mainUserId mainStream.height = 1280 mainStream.width = 720 mainStream.x = 0 mainStream.y = 0 mainStream.zOrder = 1 mainStream.inputType = .audioVideo let subStream = V2TXLiveMixStream() subStream.streamId = streamId subStream.userId = userId subStream.height = 1 subStream.width = 1 subStream.x = 0 subStream.y = 0 subStream.zOrder = 1 subStream.inputType = .pureAudio config.mixStreams = [mainStream, subStream] let state = self.livePusher?.setMix(config) if state == .TXLIVE_OK { print("混流成功") } else { print("混流失败") } }
然后是关闭拉流
//主播端停止拉流 public func anchorStopPlay() { self.livePlayer.stopPlay() self.livePusher?.setMix(nil) }
以上就是直播方面的封装代码,如果不接连麦功能可直接指定View去显示拉流的视图进行展示就行,拉流地址找后台要,有问题的请在下方留言