Twilio的videoViewDelegate没有被调用

时间:2018-10-15 06:52:44

标签: ios swift twilio

我正在使用twilio框架开发视频交流。根据Twilio的视频快速入门,我修改了呈现TwilioVideoView的某种方式。

我正在使用一个继承UIView类的类,称为“ myView”,该类具有TVIVideoView的实例属性。然后,我使“ myView”符合TVIVideoViewDelegate的协议,希望获得一些回调,但我失败了 获取回调,即使我可以成功在myView上显示远程对等方的视频。

根据TVIVideoView的评论说:

  

delegate一个实现TVIVideoViewDelegate协议的对象(通常是UIViewController)。

这是否意味着我无法使自定义类符合TVIVideoViewDelegate?

这是myView类:

open class myView: UIView {
    private var tviVideoView: TVIVideoView?
    public var myViewDelegate: MyViewDelegate?

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public init(frame: CGRect) {
        super.init(frame: frame)
    }

    public init(frame: CGRect, myViewDelegate: MyViewDelegate? = nil) {
        super.init(frame: frame)
        self.tviVideoView = TVIVideoView.init(frame: self.frame, delegate: self)
        self.myViewDelegate = myViewDelegate
        self.tviVideoView?.translatesAutoresizingMaskIntoConstraints = false
        tviVideoView?.contentMode = .scaleAspectFill
        self.addSubview(self.tviVideoView!)
        self.backgroundColor = UIColor.blue
    }
}

extension myView: TVIVideoViewDelegate {
    func videoView(_ view: TVIVideoView, videoDimensionsDidChange dimensions: CMVideoDimensions) {
        DispatchQueue.main.async {
            print("dimensions: \(dimensions.width), \(dimensions.height)")
            self.myViewDelegate?.videoView(self, videoDimensionDidChange: dimensions)
        }
    }
}

public protocol MyViewDelegate {
    func videoView(_ view: UIView, videoDimensionDidChange dimensions: CMVideoDimensions)
}

1 个答案:

答案 0 :(得分:0)

我找到了一种解决方法: 我创建一个继承TVIVideoViewDelegate的类,并将其分配为TVIVideoView的委托。而且有效!但是我还是不知道为什么?

我的代码如下:

open class myView: UIView {
    private var tviVideoView: TVIVideoView?
    public var myViewDelegate: MyViewDelegate? {
        didSet {
            self.delegatorOfTVIView?.myViewDelegate = myViewDelegate
        }
    }

    var delegatorOfTVIView: DelegatorOfTVIVideoViewDelegate?

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public init(frame: CGRect) {
        super.init(frame: frame)
    }

    public init(frame: CGRect, myViewDelegate: MyViewDelegate? = nil) {
        super.init(frame: frame)
        self.delegatorOfTVIView =  DelegatorOfTVIVideoViewDelegate(myDelegate: myViewDelegate)
        self.myViewDelegate = myViewDelegate
        self.tviVideoView = TVIVideoView.init(frame: self.frame, delegate: delegatorOfTVIView)

        self.tviVideoView?.translatesAutoresizingMaskIntoConstraints = false
        tviVideoView?.contentMode = .scaleAspectFill
        self.addSubview(self.tviVideoView!)
        self.backgroundColor = UIColor.blue
    }
}


public protocol MyViewDelegate {
    func videoView(_ view: UIView, videoDimensionDidChange dimensions: CMVideoDimensions)
}

class DelegatorOfTVIVideoViewDelegate: NSObject, TVIVideoViewDelegate {
    var myViewDelegate: MyViewDelegate?

    init(myDelegate: MyViewDelegate? = nil) {
        super.init()
        self.myViewDelegate = myDelegate
    }

    func videoView(_ view: TVIVideoView, videoDimensionsDidChange dimensions: CMVideoDimensions) {
        print("TVIVideoDelegate call dimension change")
        self.myViewDelegate?.videoView(view, videoDimensionDidChange: dimensions)
    }
}