我正在尝试使用GKMatch对象在两个连接的玩家中实现VoiceChat。 我的玩家已通过身份验证,我也可以使用GKMatchmakerViewController创建匹配。
问题是当我通过委托回调matchmakerViewController:didFindMatch:
收到GKMatch对象时,我设置了AudioSession和一个VoiceChat对象。但是在这个方法返回后不久我就会在GKMatch的委托match:player:didChangeState:
以下是我在didFindMatch回调中创建AudioSession和VoiceChat的方法:
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match {
[viewController dismissViewControllerAnimated:YES completion:nil];
self.match = match;
match.delegate = self;
if (!_matchStarted && match.expectedPlayerCount == 0)
{
NSError *err = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
[audioSession setActive: YES error:&err];
if (err)
{
NSLog(@"%@",err.localizedDescription);
}
self.teamChannel = [[match voiceChatWithName:@"redTeam"] retain];
_teamChannel.volume = 1.0f;
_teamChannel.active = YES;
[_teamChannel start];
_teamChannel.playerStateUpdateHandler = ^(NSString *playerID, GKVoiceChatPlayerState state) {
switch (state)
{
case GKVoiceChatPlayerSpeaking:
NSLog(@"Speaking...");
break;
case GKVoiceChatPlayerSilent:
break;
case GKVoiceChatPlayerConnected:
NSLog(@"Connected.");
break;
case GKVoiceChatPlayerConnecting:
NSLog(@"Connecting..");
break;
case GKVoiceChatPlayerDisconnected:
NSLog(@"Disconnected.");
break;
}
};
}
}
我从未接到playerStateUpdateHandler
的电话。我在以下函数中断开调用:
` - (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {
if(_match!= match)return;
switch (state) {
case GKPlayerStateConnected:
NSLog(@"Player connected!");
break;
case GKPlayerStateDisconnected:
NSLog(@"Player disconnected!");
_matchStarted = NO;
break;
case GKPlayerStateUnknown:
NSLog(@"Player stage Unknown.");
break;
}
}`
问题: -
我无法听到任何声音,我错过了什么? 我已经尝试了3天了,并且(作为一个附带问题)我不知道如何处理我的第二个玩家。因为,当有匹配时,我在其中一个设备上获得了didFindMatch,并且在另一个设备上没有回叫。我是否需要在其他设备上发送消息?关于比赛?
非常感谢快速帮助。
答案 0 :(得分:0)
听起来你邀请一个玩家而不是自动匹配玩家,如果是这种情况didFindMatch
只在托管设备上调用。客户在inviteHandler
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite)
{
if (acceptedInvite)
{
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite];
mmvc.matchmakerDelegate = self;
// Do client connection stuff here
}
else if (playersToInvite)
{
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request];
mmvc.matchmakerDelegate = self;
[pViewController presentViewController:mmvc animated:YES completion:nil];
}
};
如果您自动匹配游戏,则两个玩家都会在[[GKMatchmaker sharedMatchmaker] findMatchForRequest:request withCompletionHandler:^(GKMatch *match, NSError *error)
[[GKMatchmaker sharedMatchmaker] findMatchForRequest:request withCompletionHandler:^(GKMatch *match, NSError *error)
{
if (error != nil)
{
NSLog(@"MULTIPLAYER MATCH REQUEST FAILED: %@", error.localizedDescription);
}
else if (match != nil)
{
self.m_pMatchObject = [match retain];
}
}];