Pourquoi GKVoiceChat ne transmet-il pas d'audio dans iOS?

Mon jeu a deux UIViewControllers . Le premier trouve une correspondance avec la méthode matchmakerViewController(didFindMatch:) . Une fois le match trouvé, le joueur est pris au second.

Le GKVoiceChat devrait être actif dans le second. Cependant, je suis en train de le configurer dans le premier UIViewController . Est-ce la bonne approche? Je ne trouve aucun moyen de le faire dans le second UIViewController , car je ne peux pas créer une variable GKMatch où je veux.

Pour append un GKVoiceChat , j'utilise le code suivant:

 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) } 

Ce qui fonctionne

  • Le jeu request avec succès l'autorisation d'utiliser le microphone;
  • Le code comstack bien. Aucun incident ou erreur ne se produit.
  • Le jeu authentifie l'user et le redirige vers le second UIViewController

Qu'est-ce qui ne

  • Aucune voix n'est transmise Je n'entends aucun son. Pourquoi cela se passe-t-il?

Comme mentionné dans les commentaires, il semble que votre object allChannel soit désalloué à la fin de la méthode. Je ne suis pas sûr quel est le but de votre deuxième controller de vue, mais je sous-class UIViewController et créer une propriété que vous définissez, puis vérifier, .eg:

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

Ensuite, mettez à jour votre méthode pour être:

 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) } 

Il y a aussi une chance que votre problème soit avec votre AVAudioSession , j'ai donc ajouté une vérification d'erreur pour cela.

Si vous choisissez de faire en sorte que le chat vocal soit géré par le controller de vue, votre méthode d'origine est activée. Ainsi, vous pouvez:

 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) }