1

我正在尝试使用 GKMatch 对象在两个连接的玩家之间实现 VoiceChat。我的球员已经过身份验证,我还可以使用 GKMatchmakerViewController 创建比赛。

问题是当我通过委托回调收到 GKMatch 对象时matchmakerViewController:didFindMatch:,我设置了 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 并且在另一台设备上没有回调。我需要在其他设备上发送消息吗?关于比赛?

快速帮助将不胜感激。

4

1 回答 1

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];
    }
}];
于 2015-06-26T05:11:09.417 回答