我的游戏有两个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
答案 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)
}