質問編集履歴
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1 +1,497 @@
|
|
1
1
|
今までFirebaseなどAndroidStudioでの導入の方法を解説したドキュメントがありましたが、Cloud Text to Speechにはなく導入方法が全くわかりません。教えていただけますと助かります。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
---追記---
|
6
|
+
|
7
|
+
教えていただいた方法で行いました。
|
8
|
+
|
9
|
+
https://github.com/changemyminds/Google-Cloud-TTS-Android
|
10
|
+
|
11
|
+
こちらのサンプルを以下のように取り込み以下のファイルのみ編集しました。
|
12
|
+
|
13
|
+
AlarmNotification.javaは無関係です。
|
14
|
+
|
15
|
+
![イメージ説明](e0a360b8b7b3da5870fb0aac85a900de.png)
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
```java
|
20
|
+
|
21
|
+
package darren.gcptts.model.presenter;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
import android.content.Context;
|
26
|
+
|
27
|
+
import android.content.Intent;
|
28
|
+
|
29
|
+
import android.speech.tts.TextToSpeech;
|
30
|
+
|
31
|
+
import android.util.Log;
|
32
|
+
|
33
|
+
import com.google.gson.JsonArray;
|
34
|
+
|
35
|
+
import com.google.gson.JsonElement;
|
36
|
+
|
37
|
+
import com.google.gson.JsonObject;
|
38
|
+
|
39
|
+
import com.google.gson.JsonParser;
|
40
|
+
|
41
|
+
import java.util.Locale;
|
42
|
+
|
43
|
+
import darren.gcptts.model.AndroidTTSAdapter;
|
44
|
+
|
45
|
+
import darren.gcptts.model.GCPTTSAdapter;
|
46
|
+
|
47
|
+
import darren.gcptts.model.SpeechManager;
|
48
|
+
|
49
|
+
import darren.gcptts.model.android.AndroidVoice;
|
50
|
+
|
51
|
+
import darren.gcptts.model.gcp.AudioConfig;
|
52
|
+
|
53
|
+
import darren.gcptts.model.gcp.EAudioEncoding;
|
54
|
+
|
55
|
+
import darren.gcptts.model.gcp.ESSMLlVoiceGender;
|
56
|
+
|
57
|
+
import darren.gcptts.model.gcp.GCPVoice;
|
58
|
+
|
59
|
+
import darren.gcptts.model.gcp.VoiceCollection;
|
60
|
+
|
61
|
+
import darren.gcptts.model.gcp.VoiceList;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
public class MainActivityPresenter implements VoiceList.IVoiceListener {
|
68
|
+
|
69
|
+
private static final String TAG = "MainActivityPresenter";
|
70
|
+
|
71
|
+
private static final int TEXT_TO_SPEECH_CODE = 0x100;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
private static final long WAIT_TIME = 2000L;
|
76
|
+
|
77
|
+
private long TOUCH_TIME = 0;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
private VoiceList mVoiceList;
|
82
|
+
|
83
|
+
private VoiceCollection mVoiceCollection;
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
private SpeechManager mSpeechManager;
|
88
|
+
|
89
|
+
private GCPTTSAdapter mGCPTTSAdapter;
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
public MainActivityPresenter() {
|
94
|
+
|
95
|
+
mVoiceList = new VoiceList();
|
96
|
+
|
97
|
+
mVoiceList.addVoiceListener(this);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
mSpeechManager = new SpeechManager();
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
// init GCPTTSAdapter and set default
|
106
|
+
|
107
|
+
mGCPTTSAdapter = new GCPTTSAdapter();
|
108
|
+
|
109
|
+
mSpeechManager.setSpeech(mGCPTTSAdapter);
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
public void exitApp() {
|
116
|
+
|
117
|
+
if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
|
118
|
+
|
119
|
+
// exit app
|
120
|
+
|
121
|
+
android.os.Process.killProcess(android.os.Process.myPid());
|
122
|
+
|
123
|
+
System.exit(0);
|
124
|
+
|
125
|
+
} else {
|
126
|
+
|
127
|
+
TOUCH_TIME = System.currentTimeMillis();
|
128
|
+
|
129
|
+
//mView.makeToast("Press back again to Exit", false);
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
public void onTextToSpeechResult(Context context, int requestCode, int resultCode) {
|
138
|
+
|
139
|
+
if (requestCode == TEXT_TO_SPEECH_CODE) {
|
140
|
+
|
141
|
+
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
|
142
|
+
|
143
|
+
AndroidTTSAdapter androidTTSAdapter = new AndroidTTSAdapter(context);
|
144
|
+
|
145
|
+
AndroidVoice androidVoice = new AndroidVoice.Builder()
|
146
|
+
|
147
|
+
.addLanguage(Locale.ENGLISH)
|
148
|
+
|
149
|
+
.addPitch(1.0f)
|
150
|
+
|
151
|
+
.addSpeakingRate(1.0f)
|
152
|
+
|
153
|
+
.build();
|
154
|
+
|
155
|
+
androidTTSAdapter.setAndroidVoice(androidVoice);
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
// set the next handler
|
160
|
+
|
161
|
+
SpeechManager androidTTSManager = new SpeechManager();
|
162
|
+
|
163
|
+
androidTTSManager.setSpeech(androidTTSAdapter);
|
164
|
+
|
165
|
+
mSpeechManager.setSupervisor(androidTTSManager);
|
166
|
+
|
167
|
+
} else {
|
168
|
+
|
169
|
+
// mView.makeToast("You do not have the text to speech file you have to install", true);
|
170
|
+
|
171
|
+
Intent installIntent = new Intent();
|
172
|
+
|
173
|
+
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
|
174
|
+
|
175
|
+
// mView.startActivity(installIntent);
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
public void initGCPTTSSettings() {
|
186
|
+
|
187
|
+
mVoiceList.start();
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
public void initAndroidTTSSetting() {
|
194
|
+
|
195
|
+
Intent checkIntent = new Intent();
|
196
|
+
|
197
|
+
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
|
198
|
+
|
199
|
+
// mView.startActivityForResult(checkIntent, TEXT_TO_SPEECH_CODE);
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
private void initGCPTTSVoice() {
|
206
|
+
|
207
|
+
if (mGCPTTSAdapter == null) return;
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
Log.d("debug-a", "nullc");
|
212
|
+
|
213
|
+
String languageCode = "en=US";
|
214
|
+
|
215
|
+
String name = "en-US-Wavenet-A";
|
216
|
+
|
217
|
+
float pitch = (float) 0.0;
|
218
|
+
|
219
|
+
float speakRate = (float) 1.0;
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
GCPVoice gcpVoice = new GCPVoice(languageCode, name);
|
224
|
+
|
225
|
+
AudioConfig audioConfig = new AudioConfig.Builder()
|
226
|
+
|
227
|
+
.addAudioEncoding(EAudioEncoding.MP3)
|
228
|
+
|
229
|
+
.addSpeakingRate(speakRate)
|
230
|
+
|
231
|
+
.addPitch(pitch)
|
232
|
+
|
233
|
+
.build();
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
mGCPTTSAdapter.setGCPVoice(gcpVoice);
|
238
|
+
|
239
|
+
mGCPTTSAdapter.setAudioConfig(audioConfig);
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
@Override
|
246
|
+
|
247
|
+
public void onResponse(String text) {
|
248
|
+
|
249
|
+
JsonElement jsonElement = new JsonParser().parse(text);
|
250
|
+
|
251
|
+
if (jsonElement == null || jsonElement.getAsJsonObject() == null ||
|
252
|
+
|
253
|
+
jsonElement.getAsJsonObject().get("voices").getAsJsonArray() == null) {
|
254
|
+
|
255
|
+
Log.e(TAG, "get error json");
|
256
|
+
|
257
|
+
return;
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
264
|
+
|
265
|
+
JsonArray jsonArray = jsonObject.get("voices").getAsJsonArray();
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
mVoiceCollection = new VoiceCollection();
|
270
|
+
|
271
|
+
for (int i = 0; i < jsonArray.size(); i++) {
|
272
|
+
|
273
|
+
JsonArray jsonArrayLanguage = jsonArray.get(i)
|
274
|
+
|
275
|
+
.getAsJsonObject().get("languageCodes")
|
276
|
+
|
277
|
+
.getAsJsonArray();
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
if (jsonArrayLanguage.get(0) != null) {
|
282
|
+
|
283
|
+
String language = jsonArrayLanguage.get(0).toString().replace("\"", "");
|
284
|
+
|
285
|
+
String name = jsonArray.get(i).getAsJsonObject().get("name").toString().replace("\"", "");
|
286
|
+
|
287
|
+
String ssmlGender = jsonArray.get(i).getAsJsonObject().get("ssmlGender").toString().replace("\"", "");
|
288
|
+
|
289
|
+
ESSMLlVoiceGender essmLlVoiceGender = ESSMLlVoiceGender.convert(ssmlGender);
|
290
|
+
|
291
|
+
int naturalSampleRateHertz = jsonArray.get(i).getAsJsonObject().get("naturalSampleRateHertz").getAsInt();
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
GCPVoice gcpVoice = new GCPVoice(language, name, essmLlVoiceGender, naturalSampleRateHertz);
|
296
|
+
|
297
|
+
mVoiceCollection.add(language, gcpVoice);
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
@Override
|
308
|
+
|
309
|
+
public void onFailure(String error) {
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
public void startSpeak(String text) {
|
318
|
+
|
319
|
+
mSpeechManager.stopSpeak();
|
320
|
+
|
321
|
+
if (mVoiceCollection == null || mVoiceCollection.size() == 0) {
|
322
|
+
|
323
|
+
// mView.makeToast("Loading Voice Error, please check network or API_KEY.", true);
|
324
|
+
|
325
|
+
Log.d("debug-a", "nullb");
|
326
|
+
|
327
|
+
} else {
|
328
|
+
|
329
|
+
initGCPTTSVoice();
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
mSpeechManager.startSpeak(text);
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
public void stopSpeak() {
|
342
|
+
|
343
|
+
mSpeechManager.stopSpeak();
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
public void resumeSpeak() {
|
350
|
+
|
351
|
+
mSpeechManager.resume();
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
public void pauseSpeak() {
|
358
|
+
|
359
|
+
mSpeechManager.pause();
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
public void disposeSpeak() {
|
366
|
+
|
367
|
+
mSpeechManager.dispose();
|
368
|
+
|
369
|
+
mSpeechManager = null;
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
```
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
```Java
|
380
|
+
|
381
|
+
package com.XXX.XXX;
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
import android.support.v7.app.AppCompatActivity;
|
386
|
+
|
387
|
+
import android.os.Bundle;
|
388
|
+
|
389
|
+
import android.app.AlarmManager;
|
390
|
+
|
391
|
+
import android.app.PendingIntent;
|
392
|
+
|
393
|
+
import android.content.Intent;
|
394
|
+
|
395
|
+
import android.util.Log;
|
396
|
+
|
397
|
+
import android.view.View;
|
398
|
+
|
399
|
+
import android.widget.Button;
|
400
|
+
|
401
|
+
import android.widget.Toast;
|
402
|
+
|
403
|
+
import java.util.Calendar;
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
import darren.gcptts.model.presenter.MainActivityPresenter;
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
public class MainActivity extends AppCompatActivity {
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
private MainActivityPresenter mPresenter;
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
private AlarmManager am;
|
420
|
+
|
421
|
+
private PendingIntent pending;
|
422
|
+
|
423
|
+
private int requestCode = 1;
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
@Override
|
428
|
+
|
429
|
+
protected void onCreate(Bundle savedInstanceState) {
|
430
|
+
|
431
|
+
super.onCreate(savedInstanceState);
|
432
|
+
|
433
|
+
setContentView(R.layout.activity_main);
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
mPresenter = new MainActivityPresenter();
|
438
|
+
|
439
|
+
mPresenter.initGCPTTSSettings();
|
440
|
+
|
441
|
+
mPresenter.initAndroidTTSSetting();
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
Button buttonStart = this.findViewById(R.id.button_start);
|
448
|
+
|
449
|
+
buttonStart.setOnClickListener(new View.OnClickListener() {
|
450
|
+
|
451
|
+
@Override
|
452
|
+
|
453
|
+
public void onClick(View v) {
|
454
|
+
|
455
|
+
abc();
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
});
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
}
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
public void abc(){
|
468
|
+
|
469
|
+
mPresenter.startSpeak("Hello.World.");
|
470
|
+
|
471
|
+
}
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
@Override
|
476
|
+
|
477
|
+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
478
|
+
|
479
|
+
super.onActivityResult(requestCode, resultCode, data);
|
480
|
+
|
481
|
+
mPresenter.onTextToSpeechResult(this, requestCode, resultCode);
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
public void onBackPressed() {
|
488
|
+
|
489
|
+
mPresenter.exitApp();
|
490
|
+
|
491
|
+
}
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
}
|
496
|
+
|
497
|
+
```
|