Я интегрировал свое приложение с CallKit
, чтобы принимать входящие звонки от другого пользователя, используя VoIP
. Теперь у меня возникла проблема со звуком, которую нельзя активировать при ответе на звонок.
Я проверял этот урок Этот , и я сравнил ProviderDelegate
, что очень похоже.
Вот так выглядит мой ProviderDelegate
класс
class ProviderDelegate: NSObject {
// 1.
fileprivate let callKitManager: CallKitCallInit
fileprivate let provider: CXProvider
init(callKitManager: CallKitCallInit) {
self.callKitManager = callKitManager
// 2.
provider = CXProvider(configuration: type(of: self).providerConfiguration)
super.init()
// 3.
provider.setDelegate(self, queue: nil)
}
// 4.
static var providerConfiguration: CXProviderConfiguration {
let providerConfiguration = CXProviderConfiguration(localizedName: "vKclub dev2")
providerConfiguration.supportsVideo = false
providerConfiguration.maximumCallsPerCallGroup = 1
providerConfiguration.supportedHandleTypes = [.phoneNumber]
return providerConfiguration
}
func reportIncomingCall(uuid: UUID, handle: String, hasVideo: Bool = false, completion: ((NSError?) -> Void)?) {
// 1.
print("This is UUID === ", uuid)
configureAudioSession()
let update = CXCallUpdate()
update.remoteHandle = CXHandle(type: .phoneNumber, value: handle)
update.hasVideo = hasVideo
provider.reportNewIncomingCall(with: uuid, update: update) { error in
if error == nil {
self.configureAudioSession()
let call = CallKitCallInit(uuid: uuid, handle: handle)
self.callKitManager.add(call: call)
lastCallUUID = uuid
print("UUID === ", uuid)
} else {
}
completion?(error as NSError?)
}
}
}
Вот как я настроил AVAudioSession
extension ProviderDelegate: CXProviderDelegate {
func providerDidReset(_ provider: CXProvider) {
print("Stop Audio ==STOP-AUDIO==")
for call in callKitManager.calls {
call.end(uuid: UUID())
}
callKitManager.removeAllCalls()
}
func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
// 1.
guard let call = callKitManager.callWithUUID(uuid: action.callUUID) else {
action.fail()
return
}
configureAudioSession()
}
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
// 1.
guard let call = callKitManager.callWithUUID(uuid: action.callUUID) else {
action.fail()
return
}
// 2.
configureAudioSession()
// 3.
call.answer()
// 4.
if #available(iOS 11, *) {
print ("vKclub")
} else {
action.fulfill()
}
}
func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
// 1.
guard let call = callKitManager.callWithUUID(uuid: action.callUUID) else {
action.fail()
return
}
// 2.
print("Stop audio ==STOP-AUDIO==")
configureAudioSession()
// 3.
call.end(uuid: action.callUUID)
// 4.
if #available(iOS 11, *) {
print("Our vKclube")
} else {
action.fulfill()
}
// 5.
callKitManager.remove(call: call)
}
// 5.
func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
print("Starting audio ==STARTING-AUDIO==")
}
func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
print("Received \(#function)")
}
func configureAudioSession() {
let session = AVAudioSession.sharedInstance()
do{
try session.setCategory(AVAudioSessionCategoryPlayAndRecord,
mode: AVAudioSessionModeVoiceChat,
options: [])
} catch {
print("========== Error in setting category \(error.localizedDescription)")
}
do {
try session.setPreferredSampleRate(44100.0)
} catch {
print("======== Error setting rate \(error.localizedDescription)")
}
do {
try session.setPreferredIOBufferDuration(0.005)
} catch {
print("======== Error IOBufferDuration \(error.localizedDescription)")
}
do {
try session.setActive(true)
} catch {
print("========== Error starting session \(error.localizedDescription)")
}
}
}
Я сталкивался с этой проблемой уже несколько дней, но до сих пор не могу понять.
Я использую XCode 10.1.