Android实例-调用GOOGLE的TTS实现文字转语音(XE7+小米2)(XE10.1+小米5)

 

 

 

相关资料:

注意:在手机上必须选安装文字转语音引擎“google Text To Speech”
地址:http://www.shouji56.com/soft/GoogleWenZiZhuanYuYinYinQingGoogleTexttoSpeech_3864/

 

结果总结:

XE10.1 + 小米手机5 = 通过测试
首先手机上必须安装语音包: 
1.“google Text To Speech” (不知为什么我的只发英文) 
2.“度秘语音引擎3.0” (中英都可以发音) 
3.“科大讯飞语音引擎3.0”(中英都可以发音) 
4.APK发音使用的是当前语音包 
5.小米手机5-设置-更多设置-语言和输入法-文字转语音(TTS)输出 


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; 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) 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) 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) 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) 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) 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) 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) 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; 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) 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) end;
264 
265 implementation
266 
267 end.
View Code

 

2.实例代码 

  1 {********************************************************}
  2 {    XE10.1 + 小米手机5 = 通过测试                                                    }
  3 {    首先手机上必须安装语音包                            }
  4 {    语音包有                                            }
  5 {    1.“google Text To Speech” (不知为什么我的只发英文)  }
  6 {    2.“度秘语音引擎3.0” (中英都可以发音)                }
  7 {    3.“科大讯飞语音引擎3.0”(中英都可以发音)             }
  8 {    4.APK发音使用的是当前语音包                         }
  9 {    5.设置-更多设置-语言和输入法-文字转语音(TTS)输出  }
 10 {********************************************************}
 11 
 12 unit Unit1;
 13 
 14 interface
 15 
 16 uses
 17   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
 18   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.ScrollBox,
 19   FMX.Memo, FMX.Controls.Presentation, FMX.StdCtrls,
 20   //需要引入的单元
 21   FMX.Helpers.Android,//
 22   Androidapi.Helpers,//引入
 23   Androidapi.JNIBridge,//引入
 24   Androidapi.Jni.JavaTypes,//引入
 25   Androidapi.JNI.TTS;//引入
 26 
 27 type
 28   TForm1 = class(TForm)
 29     Button1: TButton;//发音
 30     Button2: TButton;//初始化
 31     Memo1: TMemo;//内容,可以是英语,可以是中文
 32     procedure Button1Click(Sender: TObject);
 33     procedure Button2Click(Sender: TObject);
 34   private
 35     type
 36       //倾听者
 37       TttsOnInitListener = class(TJavaLocal, JTextToSpeech_OnInitListener)
 38       private
 39         FParent: TForm1;
 40       public
 41         constructor Create(AParent: TForm1);
 42         procedure onInit(status: Integer); cdecl;
 43       end;
 44   private
 45     { Private declarations }
 46     ttsListener: TttsOnInitListener;//倾听者私有对象
 47     TTS: JTextToSpeech;//文字TO言语
 48     procedure SpeakOut;//说话
 49   public
 50     { Public declarations }
 51     constructor Create(AOwner: TComponent); override;
 52     destructor Destroy; override;
 53   end;
 54 
 55 var
 56   Form1: TForm1;
 57 
 58 implementation
 59 
 60 {$R *.fmx}
 61 {$R *.LgXhdpiPh.fmx ANDROID}
 62 //发音
 63 procedure TForm1.Button1Click(Sender: TObject);
 64 begin
 65   SpeakOut;
 66 end;
 67 //初始化
 68 procedure TForm1.Button2Click(Sender: TObject);
 69 begin
 70   TTS := TJTextToSpeech.JavaClass.init(SharedActivityContext, ttsListener);
 71 end;
 72 //建立私有对象
 73 constructor TForm1.Create(AOwner: TComponent);
 74 begin
 75   inherited;
 76   ttsListener := TttsOnInitListener.Create(self);
 77 end;
 78 //释放对象
 79 destructor TForm1.Destroy;
 80 begin
 81   if Assigned(TTS) then
 82   begin
 83     TTS.stop;
 84     TTS.shutdown;
 85     TTS := nil;
 86   end;
 87   ttsListener := nil;
 88   inherited;
 89 end;
 90 //把要朗读的文字指定给 TTS 引擎
 91 procedure TForm1.SpeakOut;
 92 var
 93   text: JString;
 94 begin
 95   text := StringToJString(Memo1.Lines.text);
 96   TTS.speak(text, TJTextToSpeech.JavaClass.QUEUE_FLUSH, nil);
 97 end;
 98 
 99 { TForm1.TttsOnInitListener }
100 
101 constructor TForm1.TttsOnInitListener.Create(AParent: TForm1);
102 begin
103   inherited Create;
104   FParent := AParent;
105 end;
106 
107 procedure TForm1.TttsOnInitListener.onInit(status: Integer);
108 var
109   Result: Integer;
110 begin
111   if (status = TJTextToSpeech.JavaClass.SUCCESS) then
112   begin
113     Result := FParent.TTS.setLanguage(TJLocale.JavaClass.US); // 这是指定这美语
114     if (Result = TJTextToSpeech.JavaClass.LANG_MISSING_DATA) or
115       (Result = TJTextToSpeech.JavaClass.LANG_NOT_SUPPORTED) then
116       //这里在XE10.1中提示“只能在主线程中使用此提示”
117       //ShowMessage('不支持这种语言!')
118     else
119     begin
120       FParent.Button1.Enabled := True;
121       FParent.Button2.Enabled := False;
122     end;
123   end
124   else
125   begin
126     //ShowMessage('初始化失败!');
127   end;
128 end;
129 
130 end.
View Code

 

 

你可能感兴趣的:(Android实例-调用GOOGLE的TTS实现文字转语音(XE7+小米2)(XE10.1+小米5))