【问题标题】:What is the new way of detecting the change in dimensions of a stream?检测流尺寸变化的新方法是什么?
【发布时间】:2014-11-09 19:34:34
【问题描述】:
旧版本的 iOS OpenTok 框架 具有以下 delegate 方法来检测 维度 或 frame 订阅流的变化。
- (void)stream:(OTStream*)stream didChangeVideoDimensions:(CGSize)dimensions;
框架的新版本没有类似的方法。
检测订阅流维度变化的新方法是什么?
或者在 iOS 中有一种方法可以将 listener 附加到视频流的维度上吗?
【问题讨论】:
标签:
ios
delegates
frame
opentok
valuechangelistener
【解决方案1】:
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];
}
}