标签:
注意:在手机上必须选安装文字转语音引擎“google Text To Speech”
地址:http://www.shouji56.com/soft/GoogleWenZiZhuanYuYinYinQingGoogleTexttoSpeech_3864/
1. 新建一个单元Androidapi.JNI.TTS
1 {*******************************************************} 2 { } 3 { CodeGear Delphi Runtime Library } 4 { Copyright(c) 2013 Jeff Overcash } 5 { } 6 {*******************************************************} 7 8 { Delphi trnslation for TTS Android Java classes from } 9 { http://developer.android.com/reference/android/speech/tts/package-summary.html } 10 11 unit Androidapi.JNI.TTS; 12 13 interface 14 15 uses Androidapi.JNIBridge, 16 Androidapi.JNI.JavaTypes, 17 Androidapi.JNI.Os, 18 Androidapi.JNI.App, 19 Androidapi.JNI.GraphicsContentViewText; 20 21 type 22 23 {Forward declarations} 24 JSynthesisCallback = interface; // android.speech.tts.SynthesisCallback 25 JSynthesisRequest = interface; // android.speech.tts.SynthesisRequest 26 JTextToSpeech = interface; // android.speech.tts.TextToSpeech 27 JTextToSpeech_Engine = interface; // android.speech.tts.TextToSpeech$Engine 28 JTextToSpeech_EngineInfo = interface; //android.speech.tts.TextToSpeech$EngineInfo 29 JTextToSpeech_OnInitListener = interface; // android.speech.tts.TextToSpeech$OnInitListener 30 JTextToSpeech_OnUtteranceCompletedListener = interface; // android.speech.tts.TextToSpeech$OnUtteranceCompletedListener 31 JTextToSpeechService = interface; // android.speech.tts.TextToSpeechService 32 JUtteranceProgressListener = interface; // android.speech.tts.UtteranceProgressListener 33 34 JSynthesisCallbackClass = interface(IJavaClass) 35 [‘{3B30766E-A43B-4005-BE92-472CD075F325}‘] 36 end; 37 38 [JavaSignature(‘android/speech/tts/SynthesisCallback‘)] 39 JSynthesisCallback = interface(IJavaInstance) 40 [‘{190C13DE-BC7E-44CE-94F6-4AF1A84A4612}‘] 41 function audioAvailable(buffer: TJavaArray<byte>; offset: Integer; length: Integer): Integer; cdecl; 42 function done: Integer; cdecl; 43 procedure error; cdecl; 44 function getMaxBufferSize: Integer; cdecl; 45 function start(sampleRateInHz: Integer; audioFormat: Integer; channelCount: Integer): Integer; cdecl; 46 end; 47 TJSynthesisCallback = class(TJavaGenericImport<JSynthesisCallbackClass, JSynthesisCallback>) end; 48 49 JSynthesisRequestClass = interface(JObjectClass) 50 [‘{AE41459D-42C6-4E66-B174-F6FA5216A1DA}‘] 51 { Method } 52 function init(text: JString; params: JBundle): JSynthesisRequest; 53 end; 54 55 [JavaSignature(‘android/speech/tts/SynthesisRequest‘)] 56 JSynthesisRequest = interface(JObject) 57 [‘{1963DAD8-C58F-4868-BF8A-B43AE7A14145}‘] 58 function getCountry: JString; cdecl; 59 function getLanguage: JString; cdecl; 60 function getParams: JBundle; cdecl; 61 function getPitch: Integer; cdecl; 62 function getSpeechRate: Integer; cdecl; 63 function getText: JString; cdecl; 64 function getVariant: JString; cdecl; 65 end; 66 TJSynthesisRequest = class(TJavaGenericImport<JSynthesisRequestClass, JSynthesisRequest>) end; 67 68 JTextToSpeechClass = interface(JObjectClass) 69 [‘{0E2C5E49-95BE-4F19-BCCD-21960D03E957}‘] 70 { Property Methods } 71 function _GetACTION_TTS_QUEUE_PROCESSING_COMPLETED: JString; 72 function _GetERROR: Integer; 73 function _GetLANG_AVAILABLE: Integer; 74 function _GetLANG_COUNTRY_AVAILABLE: Integer; 75 function _GetLANG_COUNTRY_VAR_AVAILABLE: Integer; 76 function _GetLANG_MISSING_DATA: Integer; 77 function _GetLANG_NOT_SUPPORTED: Integer; 78 function _GetQUEUE_ADD: Integer; 79 function _GetQUEUE_FLUSH: Integer; 80 function _GetSUCCESS: Integer; 81 { Methods } 82 function init(contect: JContext; listener: JTextToSpeech_OnInitListener) : JTextToSpeech; cdecl; overload; 83 function init(context: JContext; listener: JTextToSpeech_OnInitListener; engine: JString): JTextToSpeech; overload; 84 { Properties } 85 property ACTION_TTS_QUEUE_PROCESSING_COMPLETED: JString read _GetACTION_TTS_QUEUE_PROCESSING_COMPLETED; 86 property ERROR: Integer read _GetERROR; 87 property LANG_AVAILABLE: Integer read _GetLANG_AVAILABLE; 88 property LANG_COUNTRY_AVAILABLE: Integer read _GetLANG_COUNTRY_AVAILABLE; 89 property LANG_COUNTRY_VAR_AVAILABLE: Integer read _GetLANG_COUNTRY_VAR_AVAILABLE; 90 property LANG_MISSING_DATA: Integer read _GetLANG_MISSING_DATA; 91 property LANG_NOT_SUPPORTED: Integer read _GetLANG_NOT_SUPPORTED; 92 property QUEUE_ADD: Integer read _GetQUEUE_ADD; 93 property QUEUE_FLUSH: Integer read _GetQUEUE_FLUSH; 94 property SUCCESS: Integer read _GetSUCCESS; 95 end; 96 97 [JavaSignature(‘android/speech/tts/TextToSpeech‘)] 98 JTextToSpeech = interface(JObject) 99 [‘{E1D06364-F967-4381-B178-EEDD42C203C7}‘] 100 function addEarcon(earcon: JString; filename: JString): Integer; cdecl; overload; 101 function addEarcon(earcon: JString; packagename: JString; resourceID: Integer): Integer; cdecl; overload; 102 function addSpeech(text: JString; filename: JString): Integer; cdecl; overload; 103 function addSpeech(text: JString; packagename: JString; resourceID: Integer) : Integer; cdecl; overload; 104 function areDefaultsEnforced: Boolean; cdecl; 105 function getDefaultEngine: JString; cdecl; 106 function getEngines: JList; cdecl; 107 function getFeatures(locale: JLocale): JSet; cdecl; 108 function getLanguage: JLocale; cdecl; 109 function isLanguageAvailable(loc: JLocale): Integer; cdecl; 110 function isSpeaking: Boolean; cdecl; 111 function playEarcon(earcon: JString; queueMode: Integer; params: JHashMap) : Integer; cdecl; 112 function playSilence(durationInMs: Int64; queueMode: Integer; params: JHashMap): Integer; cdecl; 113 function setEngineByPackageName(enginePackageName: JString): Integer; cdecl; 114 function setLanguage(loc: JLocale): Integer; cdecl; 115 function setOnUtteranceCompletedListener(listener: JTextToSpeech_OnUtteranceCompletedListener): Integer; cdecl; 116 function setOnUtteranceProgressListener(listener: JUtteranceProgressListener): Integer; cdecl; 117 function setPitch(pitch: Single): Integer; cdecl; 118 function setSpeechRate(speechRate: Single): Integer; cdecl; 119 procedure shutdown; cdecl; 120 function speak(text: JString; queueMode: Integer; params: JHashMap) : Integer; cdecl; 121 function stop: Integer; cdecl; 122 function synthesizeToFile(text: JString; params: JHashMap; filename: String) : Integer; cdecl; 123 end; 124 TJTextToSpeech = class(TJavaGenericImport<JTextToSpeechClass, JTextToSpeech>) end; 125 126 JTextToSpeech_EngineClass = interface(JObjectClass) 127 [‘{8516EA75-A410-4EEE-8281-9ABCE1577F46}‘] 128 { Property Methods } 129 function _GeCHECK_VOICE_DATA_PASS: Integer; 130 function _GetACTION_CHECK_TTS_DATA: JString; 131 function _GetACTION_INSTALL_TTS_DATA: JString; 132 function _GetACTION_TTS_DATA_INSTALLED: JString; 133 function _GetCHECK_VOICE_DATA_BAD_DATA: Integer; 134 function _GetCHECK_VOICE_DATA_FAIL: Integer; 135 function _GetCHECK_VOICE_DATA_MISSING_DATA: Integer; 136 function _GetCHECK_VOICE_DATA_MISSING_VOLUME: Integer; 137 function _GetDEFAULT_STREAM: Integer; 138 function _GetEXTRA_AVAILABLE_VOICES: JString; 139 function _GetEXTRA_CHECK_VOICE_DATA_FOR: JString; 140 function _GetEXTRA_TTS_DATA_INSTALLED: JString; 141 function _GetEXTRA_UNAVAILABLE_VOICES: JString; 142 function _GetEXTRA_VOICE_DATA_FILES: JString; 143 function _GetEXTRA_VOICE_DATA_FILES_INFO: JString; 144 function _GetEXTRA_VOICE_DATA_ROOT_DIRECTORY: JString; 145 function _GetINTENT_ACTION_TTS_SERVICE: JString; 146 function _GetKEY_FEATURE_EMBEDDED_SYNTHESIS: JString; 147 function _GetKEY_FEATURE_NETWORK_SYNTHESIS: JString; 148 function _GetKEY_PARAM_PAN: JString; 149 function _GetKEY_PARAM_STREAM: JString; 150 function _GetKEY_PARAM_UTTERANCE_ID: JString; 151 function _GetKEY_PARAM_VOLUME: JString; 152 function _GetSERVICE_META_DATA: JString; 153 { Methods } 154 function init: JTextToSpeech_Engine; 155 { Properties } 156 property ACTION_CHECK_TTS_DATA: JString read _GetACTION_CHECK_TTS_DATA; 157 property ACTION_INSTALL_TTS_DATA: JString read _GetACTION_INSTALL_TTS_DATA; 158 property ACTION_TTS_DATA_INSTALLED: JString read _GetACTION_TTS_DATA_INSTALLED; 159 property CHECK_VOICE_DATA_BAD_DATA: Integer read _GetCHECK_VOICE_DATA_BAD_DATA; 160 property CHECK_VOICE_DATA_FAIL: Integer read _GetCHECK_VOICE_DATA_FAIL; 161 property CHECK_VOICE_DATA_MISSING_DATA: Integer read _GetCHECK_VOICE_DATA_MISSING_DATA; 162 property CHECK_VOICE_DATA_MISSING_VOLUME: Integer read _GetCHECK_VOICE_DATA_MISSING_VOLUME; 163 property CHECK_VOICE_DATA_PASS: Integer read _GeCHECK_VOICE_DATA_PASS; 164 property DEFAULT_STREAM: Integer read _GetDEFAULT_STREAM; 165 property EXTRA_AVAILABLE_VOICES: JString read _GetEXTRA_AVAILABLE_VOICES; 166 property EXTRA_CHECK_VOICE_DATA_FOR: JString read _GetEXTRA_CHECK_VOICE_DATA_FOR; 167 property EXTRA_TTS_DATA_INSTALLED: JString read _GetEXTRA_TTS_DATA_INSTALLED; 168 property EXTRA_UNAVAILABLE_VOICES: JString read _GetEXTRA_UNAVAILABLE_VOICES; 169 property EXTRA_VOICE_DATA_FILES: JString read _GetEXTRA_VOICE_DATA_FILES; 170 property EXTRA_VOICE_DATA_FILES_INFO: JString read _GetEXTRA_VOICE_DATA_FILES_INFO; 171 property EXTRA_VOICE_DATA_ROOT_DIRECTORY: JString read _GetEXTRA_VOICE_DATA_ROOT_DIRECTORY; 172 property INTENT_ACTION_TTS_SERVICE: JString read _GetINTENT_ACTION_TTS_SERVICE; 173 property KEY_FEATURE_EMBEDDED_SYNTHESIS: JString read _GetKEY_FEATURE_EMBEDDED_SYNTHESIS; 174 property KEY_FEATURE_NETWORK_SYNTHESIS: JString read _GetKEY_FEATURE_NETWORK_SYNTHESIS; 175 property KEY_PARAM_PAN: JString read _GetKEY_PARAM_PAN; 176 property KEY_PARAM_STREAM: JString read _GetKEY_PARAM_STREAM; 177 property KEY_PARAM_UTTERANCE_ID: JString read _GetKEY_PARAM_UTTERANCE_ID; 178 property KEY_PARAM_VOLUME: JString read _GetKEY_PARAM_VOLUME; 179 property SERVICE_META_DATA: JString read _GetSERVICE_META_DATA; 180 end; 181 182 JTextToSpeech_Engine = interface(JObject) 183 [‘{5BAC3048-CB0C-4DC4-AF62-D0D9AE4394CF}‘] 184 end; 185 TJTextToSpeech_Engine = class(TJavaGenericImport<JTextToSpeech_EngineClass, JTextToSpeech_Engine>) end; 186 187 188 JTextToSpeech_EngineInfoClass = interface(JObjectClass) 189 [‘{8297AD59-5A6F-4867-A185-CA09BAD90159}‘] 190 {Methods} 191 function init : JTextToSpeech_EngineInfo; 192 end; 193 194 JTextToSpeech_EngineInfo = interface(JObject) 195 [‘{204B30D8-6E25-4531-847D-18588D48D9BF}‘] 196 {property Methods} 197 function _Geticon: integer; 198 function _Getlabel: JString; 199 function _Getname: JString; 200 procedure _Seticon(const Value: integer); 201 procedure _Setlabel(const Value: JString); 202 procedure _Setname(const Value: JString); 203 {Properties} 204 property icon : integer read _Geticon write _Seticon; 205 property _label : JString read _Getlabel write _Setlabel; 206 property name : JString read _Getname write _Setname; 207 end; 208 TJTextToSpeech_EngineInfo = class(TJavaGenericImport<JTextToSpeech_EngineInfoClass, JTextToSpeech_EngineInfo>) end; 209 210 JTextToSpeech_OnInitListenerClass = interface(IJavaClass) 211 [‘{58D32EFB-6528-4EC6-BA4F-28B22FE8E573}‘] 212 end; 213 214 [JavaSignature(‘android/speech/tts/TextToSpeech$OnInitListener‘)] 215 JTextToSpeech_OnInitListener = interface(IJavaInstance) 216 [‘{027DA109-680A-4A69-905D-F62E2BD1282F}‘] 217 procedure onInit(status: Integer); cdecl; 218 end; 219 TJTextToSpeech_OnInitListener = class(TJavaGenericImport<JTextToSpeech_OnInitListenerClass,JTextToSpeech_OnInitListener>) end; 220 221 JTextToSpeech_OnUtteranceCompletedListenerClass = interface(IJavaClass) 222 [‘{0A608BB9-A6BF-4746-8419-9317AD625DFA}‘] 223 end; 224 225 [JavaSignature(‘android/speech/tts/TextToSpeech$OnUtteranceCompletedListener‘)] 226 JTextToSpeech_OnUtteranceCompletedListener = interface(IJavaInstance) 227 [‘{00439F2B-E73C-4B93-A9E3-832AE3CC1D5F}‘] 228 procedure onUtteranceCompleted(utteranceID: JString); cdecl; 229 end; 230 TJTextToSpeech_OnUtteranceCompletedListener = class(TJavaGenericImport<JTextToSpeech_OnUtteranceCompletedListenerClass, JTextToSpeech_OnUtteranceCompletedListener>) end; 231 232 JTextToSpeechServiceClass = interface(JServiceClass) 233 [‘{6BCC6ADC-CBCB-4515-A5C3-E33F9787EEC2}‘] 234 function init : JTextToSpeechService; 235 end; 236 237 [JavaSignature(‘android/speech/tts/TextToSpeechService‘)] 238 JTextToSpeechService = interface(JService) 239 [‘{EE3943B6-88DC-488B-9E10-E0FA9914708D}‘] 240 function onGetFeaturesForLanguage(lang : JString; country : JString; variant : JString) : JSet; cdecl; 241 function onGetLanguage : TJavaArray<JString>; cdecl; 242 function onIsLanguageAvailable(lang : JString; country : JString; variant : JString) : Integer; cdecl; 243 function onLoadLanguage(lang : JString; country : JString; variant : JString) : Integer; cdecl; 244 procedure onStop; cdecl; 245 procedure onSynthesizeText(request : JSynthesisRequest; callback : JSynthesisCallback); cdecl; 246 end; 247 TJTextToSpeechService = class(TJavaGenericImport<JTextToSpeechServiceClass, JTextToSpeechService>) end; 248 249 JUtteranceProgressListenerClass = interface(JObjectClass) 250 [‘{0870532F-6FD7-4B1F-9DEF-B53F0095C98A}‘] 251 { Method } 252 function init: JUtteranceProgressListener; 253 end; 254 255 [JavaSignature(‘android/speech/tts/UtteranceProgressListener‘)] 256 JUtteranceProgressListener = interface(JObject) 257 [‘{8B03499D-0B26-4F36-90E8-F724BD78DB0C}‘] 258 procedure onDone(utteranceID: JString); cdecl; 259 procedure onError(utteranceID: JString); cdecl; 260 procedure onStart(utteranceID: JString); cdecl; 261 end; 262 263 TJUtteranceProgressListener = class(TJavaGenericImport<JUtteranceProgressListenerClass, JUtteranceProgressListener>) end; 264 265 implementation 266 267 end.
2.实例窗体
1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, 7 System.Variants, Androidapi.Helpers, 8 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, 9 FMX.StdCtrls, FMX.Layouts, FMX.Memo, Androidapi.JNIBridge, 10 FMX.Controls.Presentation, FMX.Edit, 11 Androidapi.Jni.JavaTypes, 12 Androidapi.JNI.TTS; 13 14 type 15 TForm1 = class(TForm) 16 Memo1: TMemo; 17 Button1: TButton; 18 Button2: TButton; 19 procedure Button1Click(Sender: TObject); 20 procedure Button2Click(Sender: TObject); 21 private 22 type 23 TttsOnInitListener = class(TJavaLocal, JTextToSpeech_OnInitListener) 24 private 25 FParent: TForm1; 26 public 27 constructor Create(AParent: TForm1); 28 procedure onInit(status: Integer); cdecl; 29 end; 30 private 31 { Private declarations } 32 ttsListener: TttsOnInitListener; 33 TTS: JTextToSpeech; 34 procedure SpeakOut; 35 public 36 { Public declarations } 37 constructor Create(AOwner: TComponent); override; 38 destructor Destroy; override; 39 end; 40 41 var 42 Form1: TForm1; 43 44 implementation 45 46 {$R *.fmx} 47 {$R *.NmXhdpiPh.fmx ANDROID} 48 49 uses FMX.Helpers.Android; 50 51 procedure TForm1.Button1Click(Sender: TObject); 52 begin 53 SpeakOut; 54 end; 55 56 procedure TForm1.Button2Click(Sender: TObject); 57 begin 58 TTS := TJTextToSpeech.JavaClass.init(SharedActivityContext, ttsListener); 59 end; 60 61 constructor TForm1.Create(AOwner: TComponent); 62 begin 63 inherited; 64 ttsListener := TttsOnInitListener.Create(self); 65 end; 66 67 destructor TForm1.Destroy; 68 begin 69 if Assigned(TTS) then 70 begin 71 TTS.stop; 72 TTS.shutdown; 73 TTS := nil; 74 end; 75 76 ttsListener := nil; 77 inherited; 78 end; 79 80 procedure TForm1.SpeakOut; // 把要朗读的文字指定给 TTS 引擎 81 var 82 text: JString; 83 begin 84 text := StringToJString(Memo1.Lines.text); 85 TTS.speak(text, TJTextToSpeech.JavaClass.QUEUE_FLUSH, nil); 86 end; 87 88 // 以下照抄即可 89 { TForm1.TttsOnInitListener } 90 constructor TForm1.TttsOnInitListener.Create(AParent: TForm1); 91 begin 92 inherited Create; 93 FParent := AParent; 94 ShowMessage(‘A!‘); 95 end; 96 97 procedure TForm1.TttsOnInitListener.onInit(status: Integer); 98 var 99 Result: Integer; 100 begin 101 ShowMessage(‘B!‘); 102 if (status = TJTextToSpeech.JavaClass.SUCCESS) then 103 begin 104 ShowMessage(‘c!‘); 105 Result := FParent.TTS.setLanguage(TJLocale.JavaClass.US); // 这是指定这美语 106 if (Result = TJTextToSpeech.JavaClass.LANG_MISSING_DATA) or 107 (Result = TJTextToSpeech.JavaClass.LANG_NOT_SUPPORTED) then 108 ShowMessage(‘不支持这种语言!‘) 109 else 110 begin 111 FParent.Button1.Enabled := true; 112 FParent.Button2.Enabled := false; 113 end; 114 end 115 else 116 ShowMessage(‘初始化失败!‘); 117 end; 118 end.
标签:
原文地址:http://www.cnblogs.com/FKdelphi/p/4633779.html