如何通过Delphi FireMonkey实现iOS应用的文本转语音功能?
在Delphi FireMonkey iOS中实现文本转语音(拼读单词/短语)
嘿,我之前刚好在Delphi FireMonkey的iOS项目里做过专门用来拼读单词、短语的文本转语音功能,给你分享下实测有效的方案!
核心思路
iOS原生自带的AVSpeechSynthesizer框架就是做文本转语音的最佳选择,我们可以通过Delphi的iOS Objective-C桥接直接调用这个原生API,稳定性和兼容性都比第三方组件靠谱得多。
步骤1:配置项目依赖
首先得确保项目引用了iOS的AVFoundation框架:
- 打开Delphi项目,进入Project Options
- 切换到iOS Platform下的Frameworks and Libraries
- 添加
AVFoundation.framework到引用列表(如果已经存在就跳过)
步骤2:实现TTS核心代码
下面是封装好的可复用代码,包含整句朗读和逐个字母拼读两种场景:
unit SpeechHelper; interface uses System.SysUtils, iOSapi.Foundation, iOSapi.AVFoundation, Macapi.ObjectiveC; // 朗读整句/单词 procedure SpeakText(const AText: string; ALanguage: string = 'en-US'; ARate: Single = 0.3); // 逐个字母拼读单词 procedure SpeakLetterByLetter(const AWord: string; ALanguage: string = 'en-US'; ARate: Single = 0.2; APauseDuration: Single = 0.5); implementation procedure SpeakText(const AText: string; ALanguage: string = 'en-US'; ARate: Single = 0.3); var Synthesizer: AVSpeechSynthesizer; Utterance: AVSpeechUtterance; Voice: AVSpeechSynthesisVoice; NSText, NSLang: NSString; begin if AText.IsEmpty then Exit; // 创建语音合成器实例 Synthesizer := TAVSpeechSynthesizer.Create; try // 转换Delphi字符串为iOS原生NSString NSText := StrToNSStr(AText); NSLang := StrToNSStr(ALanguage); // 创建承载朗读文本的Utterance对象 Utterance := TAVSpeechUtterance.speechUtteranceWithString(NSText); try // 设置语速:范围0.0(最慢)到1.0(最快),拼读场景建议0.3左右 Utterance.setRate(ARate); // 设置发音语言,比如英语'en-US'、中文普通话'zh-CN' Voice := TAVSpeechSynthesisVoice.voiceWithLanguage(NSLang); if Assigned(Voice) then Utterance.setVoice(Voice); // 启动朗读 Synthesizer.speakUtterance(Utterance); finally Utterance.release; // 手动释放OC对象,避免内存泄漏 end; finally Synthesizer.release; end; end; procedure SpeakLetterByLetter(const AWord: string; ALanguage: string = 'en-US'; ARate: Single = 0.2; APauseDuration: Single = 0.5); var I: Integer; Synthesizer: AVSpeechSynthesizer; Utterance: AVSpeechUtterance; NSChar, NSLang: NSString; begin if AWord.IsEmpty then Exit; Synthesizer := TAVSpeechSynthesizer.Create; try NSLang := StrToNSStr(ALanguage); for I := 1 to Length(AWord) do begin NSChar := StrToNSStr(AWord[I]); Utterance := TAVSpeechUtterance.speechUtteranceWithString(NSChar); try Utterance.setRate(ARate); // 更慢的语速,确保每个字母清晰可辨 Utterance.setPauseDuration(APauseDuration); // 字母之间的停顿间隔 Utterance.setVoice(TAVSpeechSynthesisVoice.voiceWithLanguage(NSLang)); Synthesizer.speakUtterance(Utterance); finally Utterance.release; end; end; finally Synthesizer.release; end; end; end.
步骤3:调用示例
在你的FireMonkey界面事件里直接调用就行,比如按钮点击:
procedure TMainForm.btnSpeakPhraseClick(Sender: TObject); begin // 朗读短语,比如"Good morning" SpeakText('Good morning', 'en-US', 0.3); end; procedure TMainForm.btnSpellWordClick(Sender: TObject); begin // 逐个字母拼读"Banana" SpeakLetterByLetter('Banana', 'en-US', 0.2, 0.5); end;
注意事项
- 优先真机测试:iOS模拟器的语音功能偶尔会出现无声或卡顿的情况,用真实iOS设备测试更准确
- 语言代码规范:确保传入的语言代码是iOS支持的,比如英式英语
en-GB、粤语zh-HK等 - 内存管理:因为调用了Objective-C对象,一定要记得手动调用
release释放,避免内存泄漏 - Delphi版本要求:建议使用XE8及以上版本,这些版本对iOS的Objective-C桥接支持更完善
内容的提问来源于stack exchange,提问作者Haizhou




