我正在尝试在我正在为iOS开发的游戏上设置语音聊天,它成功创建了匹配,但是当我尝试设置语音聊天时它什么也没做,我做错了什么?它运行时没有抛出错误。这是我用来进行语音聊天的代码。
- (void)establishVoice
{
if (![GKVoiceChat isVoIPAllowed])
return;
if (![self establishPlayAndRecordAudioSession])
return;
NSLog(@"Did stablish voice chat");
chat = [match voiceChatWithName:@"GeneralChat"];
[chat start]; // stop with [chat end];
chat.active = YES; // disable mic by setting to NO
chat.volume = 1.0f; // adjust as needed.
chat.playerStateUpdateHandler = ^(NSString *playerID, GKVoiceChatPlayerState state) {
switch (state)
{
case GKVoiceChatPlayerSpeaking:
// Highlight player's picture
NSLog(@"Speaking");
break;
case GKVoiceChatPlayerSilent:
// Dim player's picture
NSLog(@"Silent");
break;
case GKVoiceChatPlayerConnected:
// Show player name/picture
NSLog(@"Voice connected");
break;
case GKVoiceChatPlayerDisconnected:
// Hide player name/picture
NSLog(@"Voice disconnected");
break;
} };
}
其中establishPlayAndRecordAudioSession为:
- (BOOL) establishPlayAndRecordAudioSession
{
NSLog(@"Establishing Audio Session");
NSError *error;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if (!success)
{
NSLog(@"Error setting session category: %@", error.localizedFailureReason);
return NO;
}
else
{
success = [audioSession setActive: YES error: &error];
if (success)
{
NSLog(@"Audio session is active (play and record)");
return YES;
}
else
{
NSLog(@"Error activating audio session: %@", error.localizedFailureReason);
return NO;
}
}
return NO;
}
代码成功记录“确定语音聊天”,因此它确实运行了代码,但是当我开始说话时,它似乎没有得到语音也没有发送它。我究竟做错了什么?我错过了什么吗?附:我没有让GKVoiceChatPlayerConnected被解雇。
答案 0 :(得分:0)
确保两台设备都打开了wifi。
(我正在敲打这个问题好几个小时,终于意识到我在我的两个测试设备之一上意外地关闭了wifi)