ios - 使用 CallKit 切换扬声器和从扬声器切换

标签 ios swift xcode avaudiosession callkit

我正在创建一个仅执行出站调用的 CallKit 应用程序。 我们正在做自己的界面,而不是使用内置的 UI。 我需要在屏幕上放置一个扬声器按钮,以便从扬声器/无扬声器模式切换。

我遵循了 Apple 的指南和示例 CallKit 代码,并且 它看起来运行得足够好。

我找不到太多有关如何切换扬声器模式和从扬声器模式切换的信息。我正在使用以下函数来更改音频路线。 有人有这样做的经验吗?这能可靠地工作吗?

func speaker(on: Bool) {
    let audioSession = AVAudioSession.sharedInstance()
    do {
        // not sure I need this ...
        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
        if on {
            // does this trigger a change to the audio route?
            try audioSession.overrideOutputAudioPort(.speaker)
        } else {
            // is this how to switch back to the phone speaker?
            try audioSession.overrideOutputAudioPort(.none)
        }
    } catch {
        // Audio session change failure
        print("failed to change speaker phone")
    }
}

最佳答案

您需要在 do block 末尾添加以下内容:audioSession.setActive(true)

关于ios - 使用 CallKit 切换扬声器和从扬声器切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44420009/

相关文章:

ios - Spritekit 平行滚动

ios - 重新加载 Collection View ?

ios - 如何暂停我的应用程序足够长的时间以使我的屏幕显示为 "catch up"

iOS cordova 允许位置访问 'Always'

使用 MVVM 模式显示数组中的值的 SwiftUI 问题

ios - ImageView 不遵守约束

ios - 将 UIImage 转换为 CGImage 以获得像素颜色

ios - 标签栏 Controller 中多个标签的一个导航 Controller

ios - Segue 没有将数据传递给第二个 VC

ios - 如何像android一样在iOS NavigationController中创建菜单?