为什么GKVoiceChat在iOS中传输任何音频?

时间:2015-03-08 20:04:35

标签: ios swift

我的游戏有两个UIViewControllers。第一个与matchmakerViewController(didFindMatch:)方法匹配。一旦找到匹配,玩家将被带到第二个。

GKVoiceChat应该在第二个中有效。但是,我目前正在第一个UIViewController进行设置。这是正确的方法吗?我无法在第二个UIViewController找到任何方法,因为我无法在任何地方创建GKMatch变量。

要添加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)
}

什么有效

  • 游戏成功请求使用麦克风;
  • 代码编译得很好。不会发生崩溃或错误。
  • 游戏成功验证用户身份,并将他/她重定向到第二个UIViewController

什么不

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

1 个答案:

答案 0 :(得分:2)

正如评论中所提到的,这看起来像是在方法结束时释放了allChannel对象。我不确定你的第二个视图控制器的用途是什么,但是我将子类UIViewController创建并创建一个你设置的属性然后检查,.e.g:

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也可能存在问题,因此我也为此添加了错误检查。

如果您选择让视图控制器管理您的原始方法,则可以这样做:

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