我正在遵循一个样例 Swift 代码,用于从 iPhone 启动 Facebook 直播流会话:
override func viewDidLoad() {
super.viewDidLoad()
liveButton.layer.cornerRadius = 15
session = VCSimpleSession(videoSize: CGSize(width: 1280, height: 720), frameRate: 30, bitrate: 4000000, useInterfaceOrientation: false)
contentView.addSubview(session.previewView)
session.previewView.frame = contentView.bounds
session.delegate = self
}
func startFBLive() {
if FBSDKAccessToken.current() != nil {
FBLiveAPI.shared.startLive(privacy: livePrivacy) { result in
guard let streamUrlString = (result as? NSDictionary)?.value(forKey: "stream_url") as? String else {
return
}
let streamUrl = URL(string: streamUrlString)
guard let lastPathComponent = streamUrl?.lastPathComponent,
let query = streamUrl?.query else {
return
}
self.session.startRtmpSession(
withURL: "rtmp://rtmp-api.facebook.com:80/rtmp/",
andStreamKey: "\(lastPathComponent)?\(query)"
)
self.livePrivacyControl.isUserInteractionEnabled = false
}
} else {
fbLogin()
}
}
这段代码可以正确地将摄像头视频流传送到Facebook。
但是,我应该如何在摄像头的视频上面覆盖一个UIView,例如倒计时文本呢?我不是要在手机上本地显示UIView,而是要将UIView覆盖到视频中,以便UIView被传输到Facebook直播流,并且其他设备上的直播观众可以看到它。
我可以看到VCSimpleSession有这个方法:
addPixelBufferSource: (UIImage*) image
withRect: (CGRect) rect;
但这可能不是我想要的,因为我需要添加一个倒计时文本...有什么好主意吗?
captureOutput(_ output:AVCaptureOutput,didOutput sampleBuffer:CMSampleBuffer,from connection:AVCaptureConnection)
方法中使用CMSampleBuffer添加它们(仍然不确定是否是正确的方法),但现在有点卡住了...你能否将一些UIView添加到流中?如果可以,你是如何实现的? - Yuuu