IOS GKVoiceChat iphone

时间:2014-02-15 16:09:34

标签: ios iphone core-audio game-center

我在iphone上遇到了GKVoiceChat的问题。当我用iphone和ipad测试语音聊天时,来自ipad的输出声音通过扬声器响亮而清晰,但是当它出现在iphone输出声音时,我必须把它放到耳边才能听到声音。

我可以选择音频输出,这样我就能听到扬声器发出的聊天声吗?

- (void) establishChat
{
    audioChat = YES;
    chat = [activeMatch voiceChatWithName:@"com.fazan.chat"];
    if (!chat)
    {
        NSLog(@"Error establishing chat!");
        return;
    }

    chat.playerStateUpdateHandler = ^(NSString *playerID, GKVoiceChatPlayerState state) {
        switch (state)
        {
            case GKVoiceChatPlayerSpeaking:
                NSLog(@"***Speaking: %@", playerID);
                // Highlight player's picture
                break;
            case GKVoiceChatPlayerSilent:
                NSLog(@"***Silent: %@", playerID);
                // Dim player's picture
                break;
            case GKVoiceChatPlayerConnected:
                NSLog(@"***Connected: %@", playerID);
                // Show player name/picture
                break;
            case GKVoiceChatPlayerDisconnected:
                NSLog(@"***Disconnected: %@", playerID);
                // Hide player name/picture
                break;
        } };


    chat.active = NO; // disable mic by setting to NO
    chat.volume = 1.0f; // adjust as needed.

    NSLog(@"Connecting to channel");
    [chat start]; // stop with [chat end];

    NSLog(@"Chat: %@", chat.description);
    NSLog(@"Participants: %@", chat.playerIDs);

    // Establishing team chats and direct chat
    /*
     e.g. directChat = [match voiceChatWithName:@"Private Channel 1"];
     Each unique name specifies the chat
     */

    // muting: [chat setMute:YES/NO forPlayer: playerID];
}

1 个答案:

答案 0 :(得分:0)

原因是iPhone默认将音频路由到“接收器”扬声器(靠你的耳机),而不是当你也启用输入时底部的内置扬声器(如双向聊天)在游戏中)。 iPad没有“接收器”扬声器,所以它只是内置扬声器。

您可以使用AudioSession API覆盖此默认值。将会话类别设置为PlayAndRecord后立即执行此操作:

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

尽管有一个警告 - 在扬声器中播放聊天会产生一些反馈,因为扬声器发出的音频会被麦克风拾取。最好使用内置麦克风的耳机来做这种事情......