识别和合成自然语言语音。
Spezi Speech 组件提供了一种简单便捷的方式来识别(语音转文本)和合成(文本转语音)自然语言内容,从而促进与应用程序的无缝交互。它构建于 Apple 的 Speech 和 AVFoundation 框架之上。
您需要将 Spezi Speech Swift 包添加到 Xcode 中的应用程序 或 Swift 包。
重要提示
如果您的应用程序尚未配置为使用 Spezi,请按照Spezi 设置文章设置 Spezi 核心基础架构。
SpeechRecognizer
和 SpeechSynthesizer
模块需要在基于 Spezi 的应用程序中使用 configuration
在 SpeziAppDelegate
中注册。
class ExampleAppDelegate: SpeziAppDelegate {
override var configuration: Configuration {
Configuration {
SpeechRecognizer()
SpeechSynthesizer()
// ...
}
}
}
注意
您可以在 Spezi 文档中了解有关 Module
的更多信息。
为了确保您的应用程序具有麦克风访问和语音识别的必要权限,请按照以下步骤配置 Xcode 项目中的目标属性
Custom iOS Target Properties
(即 Info.plist
文件) 以提供应用程序需要这些权限的原因的说明Privacy - Microphone Usage Description
的键,并提供一个字符串值,描述您的应用程序为何需要访问麦克风。 当应用程序首次请求麦克风访问权限时,此描述将显示给用户。Privacy - Speech Recognition Usage Description
的键,并提供一个字符串值,解释您的应用程序为何需要语音识别功能。 当应用程序首次尝试执行语音识别时,此描述将呈现给用户。这些条目对于使用麦克风和语音识别功能的应用程序是强制性的。 如果未能提供这些条目,将导致您的应用程序无法访问这些功能。
SpeechTestView
演示了 Spezi Speech 的功能。 它展示了与 SpeechRecognizer
的交互,以提供语音转文本功能,以及与 SpeechSynthesizer
的交互,以从文本生成语音。
struct SpeechTestView: View {
// Get the `SpeechRecognizer` and `SpeechSynthesizer` from the SwiftUI `Environment`.
@Environment(SpeechRecognizer.self) private var speechRecognizer
@Environment(SpeechSynthesizer.self) private var speechSynthesizer
// The transcribed message from the user's voice input.
@State private var message = ""
var body: some View {
VStack {
// Button used to start and stop recording by triggering the `microphoneButtonPressed()` function.
Button("Record") {
microphoneButtonPressed()
}
.padding(.bottom)
// Button used to start and stop playback of the transcribed message by triggering the `playbackButtonPressed()` function.
Button("Playback") {
playbackButtonPressed()
}
.padding(.bottom)
Text(message)
}
}
private func microphoneButtonPressed() {
if speechRecognizer.isRecording {
// If speech is currently recognized, stop the transcribing.
speechRecognizer.stop()
} else {
// If the recognizer is idle, start a new recording.
Task {
do {
// The `speechRecognizer.start()` function returns an `AsyncThrowingStream` that yields the transcribed text.
for try await result in speechRecognizer.start() {
// Access the string-based result of the transcribed result.
message = result.bestTranscription.formattedString
}
}
}
}
}
private func playbackButtonPressed() {
if speechSynthesizer.isSpeaking {
// If speech is currently synthezized, pause the playback.
speechSynthesizer.pause()
} else {
// If synthesizer is idle, start with the text-to-speech functionality.
speechSynthesizer.speak(message)
}
}
}
SpeziSpeech 还支持选择声音,包括个人声音。
以下示例显示了如何让用户在他们当前的语言环境中选择声音,并且所选的声音可以用于合成语音。
struct SpeechVoiceSelectionExample: View {
@Environment(SpeechSynthesizer.self) private var speechSynthesizer
@State private var selectedVoiceIndex = 0
@State private var message = ""
var body: some View {
VStack {
TextField("Enter text to be spoken", text: $message)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Picker("Voice", selection: $selectedVoiceIndex) {
ForEach(speechSynthesizer.voices.indices, id: \.self) { index in
Text(speechSynthesizer.voices[index].name)
.tag(index)
}
}
.pickerStyle(.inline)
.accessibilityIdentifier("voicePicker")
.padding()
Button("Speak") {
speechSynthesizer.speak(
message,
voice: speechSynthesizer.voices[selectedVoiceIndex]
)
}
}
.padding()
}
}
iOS 17 及更高版本支持个人声音。 用户必须首先创建个人声音。 使用个人声音还需要获得用户的授权。 要请求访问任何可用的个人声音,您可以使用 SpeechSynthesizer
的 getPersonalVoices()
方法。 然后,个人声音将与系统声音一起提供。
有关更多信息,请参阅API 文档。
欢迎对此项目做出贡献。 请务必先阅读贡献指南和贡献者盟约行为准则。
本项目根据 MIT 许可证获得许可。 有关更多信息,请参阅许可证。