您链接的项目 (OpenTok-iOS-Hello-World) 仅用于订阅一个流。就像概念证明一样,您只需修改一些方法并在ViewController.m
创建一个跟踪订阅者数量的变量:
@implementation ViewController {
OTSession* _session;
OTPublisher* _publisher;
OTSubscriber* _subscriber;
int _numSubscribers; // **NEW**
}
在初始化方法中初始化变量:
- (void)viewDidLoad
{
[super viewDidLoad];
_session = [[OTSession alloc] initWithSessionId:kSessionId
delegate:self];
_numSubscribers = 0; // **NEW**
[self doConnect];
}
确保我们没有订阅我们自己的流:
static bool subscribeToSelf = NO;
修改不再关心此会话委托方法中是否已有订阅者:
- (void)session:(OTSession*)mySession didReceiveStream:(OTStream*)stream
{
NSLog(@"session didReceiveStream (%@)", stream.streamId);
// See the declaration of subscribeToSelf above.
if ( (subscribeToSelf && [stream.connection.connectionId isEqualToString: _session.connection.connectionId])
||
(!subscribeToSelf && ![stream.connection.connectionId isEqualToString: _session.connection.connectionId])
) {
// ** Changing if statement **
if (_numSubscribers < 2) {
_subscriber = [[OTSubscriber alloc] initWithStream:stream delegate:self];
_numSubscribers++;
}
}
}
将订阅者并排放置,占用更少的宽度:
- (void)subscriberDidConnectToStream:(OTSubscriber*)subscriber
{
NSLog(@"subscriberDidConnectToStream (%@)", subscriber.stream.connection.connectionId);
// ** Calculate the frame **
CGRect subFrame = CGRectMake(0, widgetHeight, widgetWidth / 2, widgetHeight)
if (_numSubscribers == 2) subFrame = CGRectOffset(subFrame, widgetWidth / 2, 0);
[subscriber.view setFrame:subFrame];
[self.view addSubview:subscriber.view];
}
注意:此解决方案不会产生稳定的应用程序。只要您不断开中间的任何 iPad,它应该可以让您看到两个订阅者。要完成此操作,您需要将创建的 OTSubscribers 存储在session:didRecieveStream:
像 NSArray 这样的集合中,处理删除正确的订阅者并减少_numSubscribers
in session:didDropStream:
,并考虑您希望该updateSubscriber
方法如何工作。