Vonage Video框架解析(二)—— 应用中添加一个实时视

2021-05-19  本文已影响0人  刀客传奇

版本记录

版本号 时间
V1.0 2021.05.19 星期三

前言

Vonage Video框架是提供直播流服务的一个平台,接下来这几篇就主要对这个框架进行介绍和讲解。感兴趣的可以看下面几篇文章。
1. Vonage Video框架解析(一)—— 应用中添加一个实时视频流的简单实现(一)

源码

1. Swift

首先看下工程组织结构

下面就是源码了

1. AppDelegate.swift
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { }
2. SceneDelegate.swift
import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  var window: UIWindow?
}
3. TeaPartyViewController.swift
import UIKit

final class TeaPartyViewController: UIViewController {
  @IBOutlet private var imageView: UIImageView!

  override func viewDidLoad() {
    super.viewDidLoad()
    // swiftlint:disable:next force_unwrapping
    let image = UIImage(named: "tea-background")!
    imageView.image = image.resizableImage(withCapInsets: .zero, resizingMode: .tile)
  }
}
4. StreamingViewController.swift
import OpenTok
import Foundation

final class StreamingViewController: UIViewController {
  private let apiKey = "your_api_key"
  private let sessionId = "your_session_id"
  // swiftlint:disable:next line_length
  private let token = "your_token"
  private var subscriberView: UIView?
  private var session: OTSession?

  @IBOutlet private var leaveButton: UIButton!

  override func viewDidLoad() {
    super.viewDidLoad()
    leaveButton.layer.cornerRadius = leaveButton.frame.size.height / 2
    navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    connectToSession()
  }

  @IBAction private func leave() {
    var error: OTError?
    session?.disconnect(&error)

    if let error = error {
      print("An error occurred disconnecting from the session", error)
    } else {
      navigationController?.popViewController(animated: true)
    }
  }

  private func connectToSession() {
    session = OTSession(
      apiKey: apiKey,
      sessionId: sessionId,
      delegate: self
    )

    var error: OTError?
    session?.connect(withToken: token, error: &error)

    if let error = error {
      print("An error occurred connecting to the session", error)
    }
  }

  private func publishCamera() {
    guard let publisher = OTPublisher(delegate: nil) else {
      return
    }

    var error: OTError?
    session?.publish(publisher, error: &error)

    if let error = error {
      print("An error occurred when trying to publish", error)
      return
    }

    guard let publisherView = publisher.view else {
      return
    }

    let screenBounds = UIScreen.main.bounds
    let viewWidth: CGFloat = 150
    let viewHeight: CGFloat = 267
    let margin: CGFloat = 20

    publisherView.frame = CGRect(
      x: screenBounds.width - viewWidth - margin,
      y: screenBounds.height - viewHeight - margin,
      width: viewWidth,
      height: viewHeight
    )
    view.addSubview(publisherView)
  }

  private func subscribe(to stream: OTStream) {
    guard let subscriber = OTSubscriber(stream: stream, delegate: nil) else {
      return
    }

    var error: OTError?
    session?.subscribe(subscriber, error: &error)

    if let error = error {
      print("An error occurred when subscribing to the stream", error)
      return
    }

    guard let subscriberView = subscriber.view else {
      return
    }

    self.subscriberView = subscriberView
    subscriberView.frame = UIScreen.main.bounds
    view.insertSubview(subscriberView, at: 0)
  }
}

// MARK: - OTSessionDelegate
extension StreamingViewController: OTSessionDelegate {
  func sessionDidConnect(_ session: OTSession) {
    print("The client connected to the session.")
    publishCamera()
  }

  func sessionDidDisconnect(_ session: OTSession) {
    print("The client disconnected from the session.")
  }

  func session(_ session: OTSession, didFailWithError error: OTError) {
    print("The client failed to connect to the session: \(error).")
  }

  func session(_ session: OTSession, streamCreated stream: OTStream) {
    print("A stream was created in the session.")
    subscribe(to: stream)
  }

  func session(_ session: OTSession, streamDestroyed stream: OTStream) {
    print("A stream was destroyed in the session.")
    subscriberView?.removeFromSuperview()
  }
}

后记

本篇主要讲述了基于Vonage Video的应用中添加一个实时视频流的简单实现,感兴趣的给个赞或者关注~~~

上一篇下一篇

猜你喜欢

热点阅读