我正在使用PJSIP设计一个用于VOIP呼叫的iOS应用程序,
我在扬声器上开始通话,按下按钮我可以在普通麦克风上拨打电话,但按下扬声器按钮后我无法再在扬声器上进行通话。
我使用以下代码来实现该功能: -
if sender.titleLabel?.text == "Speaker"{
do {
try audioSession.overrideOutputAudioPort(.none)
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setMode(AVAudioSessionModeVoiceChat)
try audioSession.setActive(true)
} catch {
print("AVAudioSession cannot be set: \(error)")
}
}else{
do {
try audioSession.overrideOutputAudioPort(.speaker)
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setMode(AVAudioSessionModeVoiceChat)
try audioSession.setActive(true)
} catch {
print("AVAudioSession cannot be set: \(error)")
}
}
答案 0 :(得分:0)
我不确定,但我认为您的代码在使用NSNotificationCenter时可以正常工作。在if语句中为发言者发送通知,并为该通知发送一个通知。
var speaker = false
按下按钮操作:
if speaker == false {
NSNotificationCenter.defaultCenter().postNotificationName("speakerON", object: nil)
} else {
NSNotificationCenter.defaultCenter().postNotificationName("speakerOFF", object: nil)
}
在viewDidLoad上:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"speakerON", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"speakerOFF", object: nil)
你的职能:
func speakerON() {
try audioSession.overrideOutputAudioPort(.none)
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setMode(AVAudioSessionModeVoiceChat)
try audioSession.setActive(true)
}
func speakerOFF() {
try audioSession.overrideOutputAudioPort(.speaker)
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setMode(AVAudioSessionModeVoiceChat)
try audioSession.setActive(true)
}
如果您想了解有关NSNotificationCenter的更多信息,请参阅此链接:
https://stackoverflow.com/questions/24049020/nsnotificationcenter-addobserver-in-swift