概览
语音叠加帮助您将用户的语音转换为文本,同时提供精美的用户体验,并为您处理必要的权限。
它内部使用了原生的 SFSpeechRecognizer
来执行语音转文本转换。
示例
您可以通过执行 pod install
命令来克隆并运行示例项目,然后运行项目
安装
CocoaPods
InstantSearchVoiceOverlay
通过 CocoaPods 可用。要安装它,请将以下行添加到您的 Podfile 中:
pod 'InstantSearchVoiceOverlay', '~> 1.0.0-beta'
Carthage
Carthage 是用于 Cocoa 的简单、去中心化的依赖管理器。
要安装 InstantSearchVoiceOverlay,请将以下行添加到您的 Cartfile 中:
github "algolia/voice-overlay-ios" ~> 1.0.0-beta
使用方法
- 在
Info.plist
中,添加这些 2 个字符串属性以及描述:
Privacy - Microphone Usage Description
,例如描述为:需要麦克风进行语音转文本
Privacy - Speech Recognition Usage Description
,例如描述为:需要语音识别功能以搜索标签
- 启动语音覆盖层并收听文本输出
import InstantSearchVoiceOverlay
class ViewController: UIViewController {
let voiceOverlayController = VoiceOverlayController()
@objc func voiceButtonTapped() {
voiceOverlayController.start(on: self, textHandler: { (text, final) in
print("voice output: \(String(describing: text))")
print("voice output: is it final? \(String(describing: final))")
}, errorHandler: { (error) in
print("voice output: error \(String(describing: error))")
})
}
自定义
您可以通过修改 voiceOverlayController 的 settings
属性来自定义语音覆盖层。
/// Specifies whether the overlay directly starts recording (true),
/// or if it requires the user to click the mic (false). Defaults to true.
voiceOverlayController.settings.autoStart = true
/// Specifies whether the overlay stops recording after the user stops talking for `autoStopTimeout`
/// seconds (true), or if it requires the user to click the mic (false). Defaults to true.
voiceOverlayController.settings.autoStop = true
/// When autoStop is set to true, autoStopTimeout determines the amount of
/// silence time of the user that causes the recording to stop. Defaults to 2.
voiceOverlayController.settings.autoStopTimeout = 2
/// The layout and style of all screens of the voice overlay.
voiceOverlayController.settings.layout.<someScreen>.<someConstant>
// Use XCode autocomplete to see all possible screens and constants that are customisable.
// Examples:
/// The voice suggestions that appear in bullet points
voiceOverlayController.settings.layout.inputScreen.subtitleBulletList = ["Suggestion1", "Sug2"]
/// Change the title of the input screen when the recording is ongoing.
voiceOverlayController.settings.layout.inputScreen.titleListening = "my custom title"
/// Change the background color of the permission screen.
voiceOverlayController.settings.layout.permissionScreen.backgroundColor = UIColor.red
/// And many more...
更改区域设置或 SpeechController
初始化 voiceOverlayController 时,您可以如此更改区域设置或 SpeechController
lazy var voiceOverlayController: VoiceOverlayController = {
let recordableHandler = {
return SpeechController(locale: Locale(identifier: "en_US"))
}
return VoiceOverlayController(speechControllerHandler: recordableHandler)
}()
您可以通过实现 Recordable
协议来创建自己的自定义 SpeechController 类。
注意,在 Swift 4 中,您可以使用 Locale.current.languageCode
来获取当前区域设置。
委托
可选地,为了监听文本和错误事件,您可以遵守 VoiceOverlayDelegate
协议的方法。
// Second way to listen to recording through delegate
func recording(text: String?, final: Bool?, error: Error?) {
if let error = error {
print("delegate: error \(error)")
}
if error == nil {
print("delegate: text \(text)")
}
}
处理权限缺失时的操作
当存在缺失的权限时,语音叠加功能将引导用户到设置应用的正确部分。
结果屏幕(Beta)
当 showResultScreen
设置为 true 时,将显示结果屏幕。
/// Whether or not to show a result screen after the recording is finished.
voiceOverlayController.settings.showResultScreen = true
/// Timeout for showing the result screen in case no resultScreenText is provided on time.
voiceOverlayController.settings.showResultScreenTimeout = 2
/// Time for showing the result screen with the provided resultScreenText.
voiceOverlayController.settings.showResultScreenTime = 4
/// The processed result screen text that should be appear in the result screen.
voiceOverlayController.settings.resultScreenText = NSAttributedString(string: myString, attributes: myAttributes)
当结果屏幕被关闭(前提是未点击“重新开始”按钮)时,小部件提供了一个 resultScreenHandler
。该处理器提供了事先在 resultScreenText
中设置的文字。
voiceOverlayController.start(on: self, textHandler: { (text, final) in
print("getting \(String(describing: text))")
print("is it final? \(String(describing: final))")
if final {
// Process the result to post in the result screen.
// The timer here simulates a network processing call that took 1.5 seconds.
Timer.scheduledTimer(withTimeInterval: 1.5, repeats: false, block: { (_) in
let myString = text
let myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.red ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
self.voiceOverlayController.settings.resultScreenText = myAttrString
})
}
}, errorHandler: { (error) in
print("error \(String(describing: error))")
}, resultScreenHandler: { (text) in
print("Result Screen: \(text)")
})
获取帮助
- 需要帮助吗?请在 Algolia 社区 或 Stack Overflow 上提问。
- 发现了一个错误吗?您可以在 GitHub 问题上报告。
- 对 Algolia 有疑问?您可以在我们网站的 常见问题解答中搜索。
参与进来
- 如果您想 贡献力量,请随时 提交拉取请求。
- 如果您有 功能请求,请 打开一个问题。
- 如果您在您的应用程序中使用 InstantSearch,我们很乐意听听您的反馈!请在我们 论坛 或 Twitter 上联系我们。
许可证
InstantSearchVoiceOverlay 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。