为什么GKVoiceChat没有在iOS上传输任何audio?

我的游戏有两个UIViewControllers 。 第一个与matchmakerViewController(didFindMatch:)方法find一个匹配。 一旦find了比赛,球员被带到第二个。

GKVoiceChat应该在第二个激活。 虽然,我目前在第一个UIViewController设置它。 这是正确的方法吗? 我找不到任何方法在第二个UIViewController ,因为我无法创build一个GKMatchvariables,无论我想要的。

要添加一个GKVoiceChat ,我使用下面的代码:

 func matchmakerViewController(viewController: GKMatchmakerViewController!, didFindMatch match: GKMatch!) { println("I found a match.") // Checking if the device has a microphone if !GKVoiceChat.isVoIPAllowed() { return } // Creation of an audio session var audioSession:AVAudioSession = AVAudioSession.sharedInstance() audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil) audioSession.setActive(true, error: nil) // Setting up a voice channel let allChannel = match.voiceChatWithName("allChannel") allChannel.active = true allChannel.volume = 1.0 allChannel.start() // Redirect the player to a new UIViewController let storyboard = UIStoryboard(name: "Universal", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController") as UIViewController self.dismissViewControllerAnimated(true, completion: nil) self.presentViewController(vc, animated: true, completion: nil) } 

什么工作

  • 游戏成功请求使用麦克风的权限;
  • 代码编译得很好。 没有崩溃或错误发生。
  • 游戏成功authentication用户,并将他/她redirect到第二个UIViewController

什么不是

  • 没有语音传输我没有听到任何声音。 为什么会这样呢?

正如在注释中提到的,这看起来像你的allChannel对象被释放在方法的末尾。 我不知道你的第二个视图控制器的目的是什么,但我会UIViewController并创build一个你设置的属性,然后检查.eg:

 class VoiceViewController: UIViewController { var voiceChat: GKVoiceChat? override func viewDidLoad() { super.viewDidLoad() if let voiceChat = self.voiceChat { // Do something with your voiceChat property } } } 

然后,更新你的方法是:

 func matchmakerViewController(viewController: GKMatchmakerViewController!, didFindMatch match: GKMatch!) { println("I found a match.") // Checking if the device has a microphone if !GKVoiceChat.isVoIPAllowed() { return } // Creation of an audio session var error: NSError? var audioSession:AVAudioSession = AVAudioSession.sharedInstance() audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: &error) if let error = error { // Do something with the error, such as tell the user return } audioSession.setActive(true, error: &error) if let error = error { // Do something with the error, such as tell the user return } // Setting up a voice channel let allChannel = match.voiceChatWithName("allChannel") allChannel.active = true allChannel.volume = 1.0 allChannel.start() // Redirect the player to a new UIViewController let storyboard = UIStoryboard(name: "Universal", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController") as VoiceViewController vc.voiceChat = allChannel self.dismissViewControllerAnimated(true, completion: nil) self.presentViewController(vc, animated: true, completion: nil) } 

你的AVAudioSession也有问题,所以我也增加了错误检查。

如果您select通过视图控制器pipe理您的原始方法进行语音聊天,那么可以这样做:

 var voiceChat: GKVoiceChat? func matchmakerViewController(viewController: GKMatchmakerViewController!, didFindMatch match: GKMatch!) { println("I found a match.") // Checking if the device has a microphone if !GKVoiceChat.isVoIPAllowed() { return } // Creation of an audio session var error: NSError? var audioSession:AVAudioSession = AVAudioSession.sharedInstance() audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: &error) if let error = error { // Do something with the error, such as tell the user return } audioSession.setActive(true, error: &error) if let error = error { // Do something with the error, such as tell the user return } // Setting up a voice channel let allChannel = match.voiceChatWithName("allChannel") allChannel.active = true allChannel.volume = 1.0 allChannel.start() self.voiceChannel = allChannel // Redirect the player to a new UIViewController let storyboard = UIStoryboard(name: "Universal", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController") as UIViewController self.dismissViewControllerAnimated(true, completion: nil) self.presentViewController(vc, animated: true, completion: nil) }