使用OpenTok iOS SDK在iOS App中实施预呼叫测试

问题描述 投票:-1回答:1
import UIKit

class CircularViewController: UIViewController, URLSessionDownloadDelegate {

    let progressLayer = CAShapeLayer()

    var pulsatingLayer: CAShapeLayer!

    let percentageLabel: UILabel = {
        let label = UILabel()
        label.text = "Start"
        label.textAlignment = .center
        label.font = UIFont.boldSystemFont(ofSize: 32)
        return label
    }()
    let completionLabel: UILabel = {
        let compLabel = UILabel()
        compLabel.text = ""
        compLabel.textAlignment = .center
        compLabel.font = UIFont.boldSystemFont(ofSize: 32)
        return compLabel
    }()

    @IBOutlet weak var imageStat:UIImageView!
    @IBOutlet weak var button:UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
      //  self.imageStat.image = #imageLiteral(resourceName: "Image-Camera")
        //let center = view.center
        print("test")
        let trackLayer = CAShapeLayer()

        let circularPath = UIBezierPath(arcCenter: .zero, radius: 100, startAngle: 0, endAngle: CGFloat.pi  * 2, clockwise: true)

        trackLayer.path = circularPath.cgPath

        trackLayer.strokeColor = UIColor.lightGray.cgColor
        trackLayer.lineWidth = 1
        trackLayer.fillColor = nil
        trackLayer.lineCap = .round
        trackLayer.position = view.center
        view.layer.addSublayer(trackLayer)

        pulsatingLayer = CAShapeLayer()
        pulsatingLayer.path = circularPath.cgPath
        pulsatingLayer.strokeColor = UIColor.clear.cgColor
        pulsatingLayer.lineWidth = 1
        pulsatingLayer.fillColor = nil
        pulsatingLayer.lineCap = .round
        pulsatingLayer.position = view.center
        view.layer.addSublayer(pulsatingLayer)


        progressLayer.path = circularPath.cgPath
        progressLayer.strokeColor = #colorLiteral(red: 0.3568627451, green: 0.1254901961, blue: 0.3098039216, alpha: 1)
        progressLayer.lineWidth = 8
        progressLayer.fillColor = nil
        progressLayer.lineCap = .square
        progressLayer.strokeEnd = 0
        progressLayer.position = view.center
        view.layer.addSublayer(progressLayer)

//        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
        button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
//        loginButton.addTarget(self, action: #selector(ViewController.login), forControlEvents: .TouchUpInside)


        view.addSubview(percentageLabel)
        view.addSubview(imageStat)
        view.addSubview(completionLabel)
        percentageLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        percentageLabel.center = view.center
        imageStat.center = view.center


    }

    let urlString = "https://firebasestorage.googleapis.com/v0/b/firestorechat-e64ac.appspot.com/o/intermediate_training_rec.mp4?alt=media&token=e20261d0-7219-49d2-b32d-367e1606500c"

    private func beginDownloadingFile() {

        progressLayer.strokeEnd = 0

        let configuration = URLSessionConfiguration.default
        let operationQueue = OperationQueue()
        let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue)

        guard let url = URL(string: urlString) else {return}
        let downloadTask = urlSession.downloadTask(with: url)
        downloadTask.resume()
    }

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        let percentage = CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite)

        DispatchQueue.main.async {
            self.percentageLabel.text = "\(Int(percentage * 100))%"
            self.progressLayer.strokeEnd = percentage

        }

//        if (percentage == 1){
//                            self.imageStat.image = #imageLiteral(resourceName: "Image-Camera")
//        }
        print(percentage)
    }


    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        print("Finished downloading file")
     //         self.imageStat.image = #imageLiteral(resourceName: "Image-Camera")
    }

    fileprivate func animateCircle() {
        let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")

        basicAnimation.toValue = 1
        basicAnimation.duration = 2
        basicAnimation.fillMode = .forwards
        basicAnimation.isRemovedOnCompletion = false

        progressLayer.add(basicAnimation, forKey: "urSoBassic")
    }

    @objc private func handleTap() {

        beginDownloadingFile()

      //  animateCircle()

    }


}
ios swift webrtc opentok tokbox
1个回答
1
投票

所以这就是我的工作方式:

  • 我添加了OpenTok SDK,并将其导入到进度条控制器中
  • 我创建了一个函数func connectToAnOpenTokSession()
  • 并在点击按钮时与进度栏功能public func setProgressNetwork(to progressConstant: Double, withAnimation: Bool)一起调用。
  • 对于预呼叫,您必须选择其他会话ID。

    //ProgressViewController
    func connectToAnOpenTokSession() {
    session = OTSession(apiKey: kApiKey, sessionId: kSessionId, delegate: self as OTSessionDelegate)
    var error: OTError?
    session?.connect(withToken: kToken, error: &error)
    if error != nil {
        print(error!)}
    }
    
    //TestViewController
    @objc func handleTapNetwork() {
    progressBar.connectToAnOpenTokSession()
    progressBar.safePercent = 100
    progressBar.setProgressNetwork(to: 1, withAnimation: true)}
    
    //TestViewController
    @IBAction func networkButtonAction(_ sender: UIButton) {        
        networkButton.addTarget(self, action: #selector(handleTapNetwork), for: 
       .primaryActionTriggered) }
    
© www.soinside.com 2019 - 2024. All rights reserved.