检测流的尺寸变化的新方法是什么?

时间:2014-09-16 06:43:56

标签: ios delegates frame opentok valuechangelistener

旧版本的 iOS OpenTok 框架具有以下委托方法来检测维度或<用户流的em> frame 更改。

- (void)stream:(OTStream*)stream didChangeVideoDimensions:(CGSize)dimensions;

框架的新版本没有类似的方法。

检测订阅者流的维度更改的 新方法 是什么?

或者 iOS 中有哪种方法可以将监听器附加到视频流的维度上?

1 个答案:

答案 0 :(得分:2)

OTStream对象的videoDimensions属性符合键值编码,因此您可以在值更改时使用Key Value Observing to receive a notification

这是一个例子(我自己没有这样做):

(在OTSessionDelegate内部,OTSubscriberDelegate实现)

- (void)session:(OTSession *)session streamCreated:(OTStream *)stream
{
    // Assuming there is only one subscriber and its a property of self
    self.subscriber = [[OTSubscriber alloc] initWithStream:stream delegate:self];
    OTError *subscribeError;
    [session subscribe:self.subscriber error:&subscribeError];
    // TODO: check error
    // TODO: Add self.subscriber.view to self.view
}

- (void)session:(OTSession *)session streamDestroyed:(OTStream *)stream
{
    if ([stream.streamId isEqualToString:self.subscriber.stream.streamId]) {
        OTError *unsubscribeError;
        [session unsubscribe:self.subscriber error:unsubscribeError];
        // TODO: check error
        // Unregister for updates to video dimensions
        [self.subscriber.stream removeObserver:self forKeyPath:@"videoDimensions"];
        // TODO: remove self.subscriber.view from self.view
    }
}

- (void)subscriberVideoDataReceived:(OTSubscriber *)subscriber
{
    // Read initial video dimensions
    CGSize videoDimensions = subscriber.stream.videoDimensions;
    // Register for updates to video dimensions
    [subscriber.stream addObserver:self
                        forKeyPath:@"videoDimensions"
                           options:(NSKeyValueObservingOptionNew |
                                      NSKeyValueObservingOptionOld)
                           context:NULL];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {

    if ([keyPath isEqual:@"videoDimensions"]) {
        // Read new value for video dimensions
        CGSize newVideoDimensions = [change objectForKey:NSKeyValueChangeNewKey];
    }
}