一 AlivcLiveVideo簡介
視頻直播服務(ApsaraVideo Live)是基于領先的內容接入與分發網絡和大規模分布式實時轉碼技術打造的音視頻直播平臺,提供便捷接入、高清流暢、低延遲、高并發的音視頻直播服務。
相關鏈接
二 sdk集成
獲取推流SDK:下載地址
其中SDK包含以下文件:
demo: 調用 SDK 的示例工程,開發者可參考demo代碼進行開發集成
lib: SDK開發包,包含framework文件,需要在工程中進行引用
doc: 相關接入文檔。
拖拽 sdk 文件夾到自己的 Xcode 項目中,勾選 Copy items if needed,點擊 Finish。
三 相關配置
打開項目的 app target,在 Build Phases 中的 Link Binary With Libraries 添加以下依賴庫:
- libz.tbd
- VideoToolbox.framework
- AudioToolbox.framework
- libstdc++.tbd
- SystemConfiguration.framework
- CoreTelephony.framework
四 代碼示例
func creatSession() {
//初始化 config 配置類
configuration = AlivcLConfiguration.init()
//設置推流地址
configuration.url = "rtmp://push.live.t.zmengzhu.com/mz/3673?vhost=live.t.zmengzhu.com&auth_key=1502804297-0-0-1c4d3a9714198f09e7780936fc688414";
//設置最大碼率
configuration.videoMaxBitRate = 1500 * 1000;
//設置當前視頻碼率
configuration.videoBitRate = 600 * 1000;
//設置最小碼率
configuration.videoMinBitRate = 400 * 1000;
//設置視頻幀率
configuration.fps = 25;
//設置音頻碼率
configuration.audioBitRate = 64 * 1000;
//設置直播分辨率
configuration.videoSize = CGSize.init(width: 360, height: 640)
//設置橫豎屏
configuration.screenOrientation = .AlivcLiveScreenVertical;
//設置攝像頭采集質量
configuration.preset = AVCaptureSessionPresetiFrame1280x720;
//設置前置攝像頭或后置攝像頭
configuration.position = .back;
//設置水印圖片
configuration.waterMaskImage = UIImage.init(named: "訂閱選中")
//設置水印位置
configuration.waterMaskLocation = .AlivcLiveWaterMaskLocationLeftTop
//設置水印相對x邊框距離
configuration.waterMaskMarginX = 10;
//設置水印相對y邊框距離
configuration.waterMaskMarginY = 70;
//設置重連超時時長
configuration.reconnectTimeout = 5;
let captureSession = AVCaptureSession.init()
if captureSession.canSetSessionPreset(AVCaptureSessionPreset1280x720) {
configuration.preset = AVCaptureSessionPreset1280x720
}else if captureSession.canSetSessionPreset(AVCaptureSessionPresetiFrame960x540)
{
configuration.preset = AVCaptureSessionPresetiFrame960x540
}else
{
configuration.preset = AVCaptureSessionPreset640x480
}
liveSession = AlivcLiveSession.init(configuration: configuration)
liveSession.delegate = self
liveSession.alivcLiveVideoStartPreview()
view.insertSubview(self.liveSession.previewView(), at: 0)
self.liveSession.previewView().snp.makeConstraints { (make) in
make.top.equalTo(view).offset(80)
make.bottom.equalTo(view).offset(-50)
make.left.right.equalTo(view)
}
let button = UIButton.init(type: .custom)
button.setTitle(NSLocalizedString("Welcome", comment: "Welcome"), for: .normal)
button.setTitleColor(UIColor.red, for: .normal)
button.addTarget(self, action: #selector(buttonclick), for: .touchUpInside)
view.addSubview(button)
button.snp.makeConstraints { (make) in
make.centerX.centerY.equalTo(view)
}
}
func buttonclick()
{
self.liveSession.alivcLiveVideoConnectServer()
}
// MARK: - AlivcLiveSessionDelegate
//required
func alivcLiveVideoLiveSession(_ session: AlivcLiveSession!, error: Error!) {
//推流錯誤
}
func alivcLiveVideoLiveSessionNetworkSlow(_ session: AlivcLiveSession!) {
//網絡很慢 不建議直播
}
//optional
func alivcLiveVideoLiveSessionConnectSuccess(_ session: AlivcLiveSession!) {
//推流鏈接成功
}
func alivcLiveVideoOpenAudioSuccess(_ session: AlivcLiveSession!) {
//麥克風獲取成功
}
func alivcLiveVideoOpenVideoSuccess(_ session: AlivcLiveSession!) {
//攝像頭獲取成功
}
func alivcLiveVideoLiveSession(_ session: AlivcLiveSession!, openAudioError error: Error!) {
//音頻打開失敗
}
func alivcLiveVideoLiveSession(_ session: AlivcLiveSession!, openVideoError error: Error!) {
//視頻設備打開失敗
}
func alivcLiveVideoLiveSession(_ session: AlivcLiveSession!, encodeAudioError error: Error!) {
//音頻轉碼失敗
}
func alivcLiveVideoLiveSession(_ session: AlivcLiveSession!, encodeVideoError error: Error!) {
//視頻轉碼失敗
}
func alivcLiveVideoReconnectTimeout(_ session: AlivcLiveSession!, error: Error!) {
//重連超時
}