我正在尝试在我为 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;
} };
}
其中建立播放和记录音频会话是:
- (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;
}
该代码成功记录了“Did stablish voice chat”,所以它确实运行了代码,但是当我开始说话时,它似乎既没有得到声音也没有发送它。我究竟做错了什么?我错过了什么吗?PS 我没有让 GKVoiceChatPlayerConnected 被解雇。