我正在我的应用程序中进行OpenTok视频通话集成。我在处理传入和传出调用方法时遇到问题。我收到来自OpenTok的呼叫请求,但无法处理该呼叫。我直接连接视频通话并显示用户界面。请帮助我找到处理传入和传出呼叫方法的方法。
下面是我尝试过的代码。
static NSString* kApiKey = @"xxxxxxx";
@interface CallDocViewController ()<OTSessionDelegate,OTPublisherDelegate,OTSubscriberDelegate,PKPushRegistryDelegate>{
PKPushRegistry *pushRegistry;
}
@property(nonatomic) OTSession *session;
@property(nonatomic) OTPublisher *publisher;
@property (nonatomic) OTSubscriber *subscriber;
@property (nonatomic) OTAudioDeviceRingtone *myAudioDevice;
@property (nonatomic) BOOL reconnectPlease;
@property (weak,nonatomic) IBOutlet UIButton *btnDisconnect;
- (IBaction)btnDisconnectClicked:(id)sender;
@end
@implementation CallDocViewController
@synthesize sessionId,tokenId;
@synthesize resultArray;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationController.navigationBar.hidden = YES;
[self connectToAnOpenTokSession];
pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
pushRegistry.delegate = self;
pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
}
#define PushKit Delegate Methods
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
if([credentials.token length] == 0) {
NSLog(@"voip token NULL");
return;
}
NSLog(@"PushCredentials: %@",credentials.token);
}
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type
{
NSLog(@"didReceiveIncomingPushWithPayload");
}
-(void)connectToAnOpenTokSession{
//kSessionId = sessionId;
_session = [[OTSession alloc] initWithApiKey:kApiKey sessionId:sessionId delegate:self];
NSError *error;
[_session connectWithToken:tokenId error:&error];
if (error) {
NSLog(@"%@",error);
}
}
# pragma mark - OTSession delegate callbacks
- (void)sessionDidConnect:(OTSession*)session
{
NSLog(@"The client connected to the OpenTok session.");
OTPublisherSettings *_publisherSettings = [[OTPublisherSettings alloc] init];
_publisherSettings.name = [UIDevice currentDevice].name;
_publisher = [[OTPublisher alloc]
initWithDelegate:self
settings:_publisherSettings];
OTError *error = nil;
[_session publish:_publisher error:&error];
if (error)
{
NSLog(@"Unable to publish (%@)",error.localizedDescription);
return;
}
[self.view addSubview:_publisher.view];
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGRect rect = CGRectMake(0,screenSize.width,screenSize.height);
[_publisher.view setframe:rect];
[self settingUpUI];
_publisher.publishAudio = YES;
_publisher.publishVideo = YES;
}
-(void)settingUpUI{
muteImage = [UIImage imageNamed:@"unmute"];
muteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[muteButton setImage:muteImage forState:UIControlStateNormal];
[muteButton addTarget:self
action:@selector(btnmuteclicked:)
forControlEvents:UIControlEventTouchUpInside];
//[muteButton setTitle:@"Disconnect" forState:UIControlStateNormal];
muteButton.frame = CGRectMake(65,585,70.0,70.0);
micStr = @"Enable";
[self.view addSubview:muteButton];
// Flip Camera Button
cameraImage = [UIImage imageNamed:@"camera"];
cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cameraButton setImage:cameraImage forState:UIControlStateNormal];
[cameraButton addTarget:self
action:@selector(btnCameraClicked:)
forControlEvents:UIControlEventTouchUpInside];
// [cameraButton setTitle:@"Disconnect" forState:UIControlStateNormal];
cameraButton.frame = CGRectMake(270,70.0);
videoStr = @"Enable";
[self.view addSubview:cameraButton];
// Disconnect Button
disconnectImage = [UIImage imageNamed:@"call"];
disconnectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[disconnectButton setImage:disconnectImage forState:UIControlStateNormal];
[disconnectButton addTarget:self
action:@selector(btnDisconnectClicked:)
forControlEvents:UIControlEventTouchUpInside];
// [button setTitle:@"Disconnect" forState:UIControlStateNormal];
disconnectButton.frame = CGRectMake(175,70.0);
[self.view addSubview:disconnectButton];
}
-(void)btnmuteclicked:(UIButton*)sender
{
if ([micStr isEqualToString:@"Enable"]) {
muteImage = [UIImage imageNamed:@"mute"];
[muteButton setImage:muteImage forState:UIControlStateNormal];
micStr = @"Disable";
_publisher.publishAudio = NO;
}
else
{
muteImage = [UIImage imageNamed:@"unmute"];
[muteButton setImage:muteImage forState:UIControlStateNormal];
micStr = @"Enable";
_publisher.publishAudio = YES;
}
}
-(void)btnCameraClicked:(UIButton*)sender
{
if ([micStr isEqualToString:@"Enable"]) {
_publisher.cameraPosition = AVCaptureDevicePositionBack;
micStr =@"Disable";
}
else
{
_publisher.cameraPosition = AVCaptureDevicePositionFront;
micStr =@"Enable";
}
}
-(void)btnDisconnectClicked:(UIButton*)sender
{
_session = [[OTSession alloc] initWithApiKey:kApiKey sessionId:sessionId delegate:self];
[self sessionDidDisconnect:_session];
}
- (void)sessionDidDisconnect:(OTSession*)session
{
NSLog(@"The client disconnected from the OpenTok session.");
[self.navigationController popviewcontrolleranimated:YES];
}
- (void) session:(OTSession*)session
didFailWithError:(OTError*)error
{
NSLog(@"The client failed to connect to the OpenTok session: (%@)",error);
}
- (void)session:(OTSession*)session
streamCreated:(OTStream *)stream
{
NSLog(@"A stream was created in the session.");
_subscriber = [[OTSubscriber alloc] initWithStream:stream
delegate:self];
OTError *error = nil;
[_session subscribe:_subscriber error:&error];
if (error)
{
NSLog(@"Unable to subscribe (%@)",error.localizedDescription);
return;
}
// [_subscriber.view setframe:[UIScreen mainScreen].bounds];
// [self.view insertSubview:_subscriber.view atIndex:0];
_publisher.publishAudio = YES;
_publisher.publishVideo = YES;
}
- (void)session:(OTSession*)session
streamDestroyed:(OTStream *)stream
{
NSLog(@"A stream was destroyed in the session.");
OTError* error = nil;
[session unsubscribe:_subscriber error:&error];
if (error) {
NSLog(@"unsubscribe failed with error: (%@)",error);
}
[_subscriber.view removeFromSuperview];
NSLog(@"session streamDestroyed (%@)",stream.streamId);
if ([_subscriber.stream.streamId isEqualToString:stream.streamId])
{
[_subscriber.view removeFromSuperview];
_subscriber = nil;
}
_session = [[OTSession alloc] initWithApiKey:kApiKey sessionId:sessionId delegate:self];
[self sessionDidDisconnect:_session];
}
# pragma mark - OTPublisher delegate callbacks
- (void)publisher:(OTPublisherKit*)publisher
didFailWithError:(OTError*) error
{
NSLog(@"The publisher failed: %@",error);
}
# pragma mark - OTSubscriber delegate callbacks
- (void)subscriberDidConnectToStream:(OTSubscriberKit *)subscriber {
NSLog(@"The subscirber: %@ did connect to the stream",subscriber);
[_subscriber.view setframe:CGRectMake(0,self.view.frame.size.width,self.view.frame.size.height)];
[_publisher.view setframe:CGRectMake(self.view.frame.size.width-180,self.view.frame.size.height-400,140,220)];
_publisher.publishAudio = YES;
_publisher.publishVideo = YES;
[self.view addSubview:_subscriber.view];
[self.view addSubview:_publisher.view];
[self.view addSubview:muteButton];
[self.view addSubview:cameraButton];
[self.view addSubview:disconnectButton];
}
- (void)subscriber:(OTSubscriberKit*)subscriber
didFailWithError:(OTError*)error
{
NSLog(@"subscriber %@ didFailWithError %@",subscriber.stream.streamId,error);
}