質問するログイン新規登録

質問編集履歴

1

コードの追加

2019/10/11 11:25

投稿

uhsi
uhsi

スコア57

title CHANGED
File without changes
body CHANGED
@@ -1,1 +1,249 @@
1
- 今までFirebaseなどAndroidStudioでの導入の方法を解説したドキュメントがありましたが、Cloud Text to Speechにはなく導入方法が全くわかりません。教えていただけますと助かります。
1
+ 今までFirebaseなどAndroidStudioでの導入の方法を解説したドキュメントがありましたが、Cloud Text to Speechにはなく導入方法が全くわかりません。教えていただけますと助かります。
2
+
3
+ ---追記---
4
+ 教えていただいた方法で行いました。
5
+ https://github.com/changemyminds/Google-Cloud-TTS-Android
6
+ こちらのサンプルを以下のように取り込み以下のファイルのみ編集しました。
7
+ AlarmNotification.javaは無関係です。
8
+ ![イメージ説明](e0a360b8b7b3da5870fb0aac85a900de.png)
9
+
10
+ ```java
11
+ package darren.gcptts.model.presenter;
12
+
13
+ import android.content.Context;
14
+ import android.content.Intent;
15
+ import android.speech.tts.TextToSpeech;
16
+ import android.util.Log;
17
+ import com.google.gson.JsonArray;
18
+ import com.google.gson.JsonElement;
19
+ import com.google.gson.JsonObject;
20
+ import com.google.gson.JsonParser;
21
+ import java.util.Locale;
22
+ import darren.gcptts.model.AndroidTTSAdapter;
23
+ import darren.gcptts.model.GCPTTSAdapter;
24
+ import darren.gcptts.model.SpeechManager;
25
+ import darren.gcptts.model.android.AndroidVoice;
26
+ import darren.gcptts.model.gcp.AudioConfig;
27
+ import darren.gcptts.model.gcp.EAudioEncoding;
28
+ import darren.gcptts.model.gcp.ESSMLlVoiceGender;
29
+ import darren.gcptts.model.gcp.GCPVoice;
30
+ import darren.gcptts.model.gcp.VoiceCollection;
31
+ import darren.gcptts.model.gcp.VoiceList;
32
+
33
+
34
+ public class MainActivityPresenter implements VoiceList.IVoiceListener {
35
+ private static final String TAG = "MainActivityPresenter";
36
+ private static final int TEXT_TO_SPEECH_CODE = 0x100;
37
+
38
+ private static final long WAIT_TIME = 2000L;
39
+ private long TOUCH_TIME = 0;
40
+
41
+ private VoiceList mVoiceList;
42
+ private VoiceCollection mVoiceCollection;
43
+
44
+ private SpeechManager mSpeechManager;
45
+ private GCPTTSAdapter mGCPTTSAdapter;
46
+
47
+ public MainActivityPresenter() {
48
+ mVoiceList = new VoiceList();
49
+ mVoiceList.addVoiceListener(this);
50
+
51
+ mSpeechManager = new SpeechManager();
52
+
53
+ // init GCPTTSAdapter and set default
54
+ mGCPTTSAdapter = new GCPTTSAdapter();
55
+ mSpeechManager.setSpeech(mGCPTTSAdapter);
56
+ }
57
+
58
+ public void exitApp() {
59
+ if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
60
+ // exit app
61
+ android.os.Process.killProcess(android.os.Process.myPid());
62
+ System.exit(0);
63
+ } else {
64
+ TOUCH_TIME = System.currentTimeMillis();
65
+ //mView.makeToast("Press back again to Exit", false);
66
+ }
67
+ }
68
+
69
+ public void onTextToSpeechResult(Context context, int requestCode, int resultCode) {
70
+ if (requestCode == TEXT_TO_SPEECH_CODE) {
71
+ if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
72
+ AndroidTTSAdapter androidTTSAdapter = new AndroidTTSAdapter(context);
73
+ AndroidVoice androidVoice = new AndroidVoice.Builder()
74
+ .addLanguage(Locale.ENGLISH)
75
+ .addPitch(1.0f)
76
+ .addSpeakingRate(1.0f)
77
+ .build();
78
+ androidTTSAdapter.setAndroidVoice(androidVoice);
79
+
80
+ // set the next handler
81
+ SpeechManager androidTTSManager = new SpeechManager();
82
+ androidTTSManager.setSpeech(androidTTSAdapter);
83
+ mSpeechManager.setSupervisor(androidTTSManager);
84
+ } else {
85
+ // mView.makeToast("You do not have the text to speech file you have to install", true);
86
+ Intent installIntent = new Intent();
87
+ installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
88
+ // mView.startActivity(installIntent);
89
+ }
90
+ }
91
+ }
92
+
93
+ public void initGCPTTSSettings() {
94
+ mVoiceList.start();
95
+ }
96
+
97
+ public void initAndroidTTSSetting() {
98
+ Intent checkIntent = new Intent();
99
+ checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
100
+ // mView.startActivityForResult(checkIntent, TEXT_TO_SPEECH_CODE);
101
+ }
102
+
103
+ private void initGCPTTSVoice() {
104
+ if (mGCPTTSAdapter == null) return;
105
+
106
+ Log.d("debug-a", "nullc");
107
+ String languageCode = "en=US";
108
+ String name = "en-US-Wavenet-A";
109
+ float pitch = (float) 0.0;
110
+ float speakRate = (float) 1.0;
111
+
112
+ GCPVoice gcpVoice = new GCPVoice(languageCode, name);
113
+ AudioConfig audioConfig = new AudioConfig.Builder()
114
+ .addAudioEncoding(EAudioEncoding.MP3)
115
+ .addSpeakingRate(speakRate)
116
+ .addPitch(pitch)
117
+ .build();
118
+
119
+ mGCPTTSAdapter.setGCPVoice(gcpVoice);
120
+ mGCPTTSAdapter.setAudioConfig(audioConfig);
121
+ }
122
+
123
+ @Override
124
+ public void onResponse(String text) {
125
+ JsonElement jsonElement = new JsonParser().parse(text);
126
+ if (jsonElement == null || jsonElement.getAsJsonObject() == null ||
127
+ jsonElement.getAsJsonObject().get("voices").getAsJsonArray() == null) {
128
+ Log.e(TAG, "get error json");
129
+ return;
130
+ }
131
+
132
+ JsonObject jsonObject = jsonElement.getAsJsonObject();
133
+ JsonArray jsonArray = jsonObject.get("voices").getAsJsonArray();
134
+
135
+ mVoiceCollection = new VoiceCollection();
136
+ for (int i = 0; i < jsonArray.size(); i++) {
137
+ JsonArray jsonArrayLanguage = jsonArray.get(i)
138
+ .getAsJsonObject().get("languageCodes")
139
+ .getAsJsonArray();
140
+
141
+ if (jsonArrayLanguage.get(0) != null) {
142
+ String language = jsonArrayLanguage.get(0).toString().replace("\"", "");
143
+ String name = jsonArray.get(i).getAsJsonObject().get("name").toString().replace("\"", "");
144
+ String ssmlGender = jsonArray.get(i).getAsJsonObject().get("ssmlGender").toString().replace("\"", "");
145
+ ESSMLlVoiceGender essmLlVoiceGender = ESSMLlVoiceGender.convert(ssmlGender);
146
+ int naturalSampleRateHertz = jsonArray.get(i).getAsJsonObject().get("naturalSampleRateHertz").getAsInt();
147
+
148
+ GCPVoice gcpVoice = new GCPVoice(language, name, essmLlVoiceGender, naturalSampleRateHertz);
149
+ mVoiceCollection.add(language, gcpVoice);
150
+ }
151
+ }
152
+ }
153
+
154
+ @Override
155
+ public void onFailure(String error) {
156
+
157
+ }
158
+
159
+ public void startSpeak(String text) {
160
+ mSpeechManager.stopSpeak();
161
+ if (mVoiceCollection == null || mVoiceCollection.size() == 0) {
162
+ // mView.makeToast("Loading Voice Error, please check network or API_KEY.", true);
163
+ Log.d("debug-a", "nullb");
164
+ } else {
165
+ initGCPTTSVoice();
166
+ }
167
+
168
+ mSpeechManager.startSpeak(text);
169
+ }
170
+
171
+ public void stopSpeak() {
172
+ mSpeechManager.stopSpeak();
173
+ }
174
+
175
+ public void resumeSpeak() {
176
+ mSpeechManager.resume();
177
+ }
178
+
179
+ public void pauseSpeak() {
180
+ mSpeechManager.pause();
181
+ }
182
+
183
+ public void disposeSpeak() {
184
+ mSpeechManager.dispose();
185
+ mSpeechManager = null;
186
+ }
187
+ }
188
+ ```
189
+
190
+ ```Java
191
+ package com.XXX.XXX;
192
+
193
+ import android.support.v7.app.AppCompatActivity;
194
+ import android.os.Bundle;
195
+ import android.app.AlarmManager;
196
+ import android.app.PendingIntent;
197
+ import android.content.Intent;
198
+ import android.util.Log;
199
+ import android.view.View;
200
+ import android.widget.Button;
201
+ import android.widget.Toast;
202
+ import java.util.Calendar;
203
+
204
+ import darren.gcptts.model.presenter.MainActivityPresenter;
205
+
206
+ public class MainActivity extends AppCompatActivity {
207
+
208
+ private MainActivityPresenter mPresenter;
209
+
210
+ private AlarmManager am;
211
+ private PendingIntent pending;
212
+ private int requestCode = 1;
213
+
214
+ @Override
215
+ protected void onCreate(Bundle savedInstanceState) {
216
+ super.onCreate(savedInstanceState);
217
+ setContentView(R.layout.activity_main);
218
+
219
+ mPresenter = new MainActivityPresenter();
220
+ mPresenter.initGCPTTSSettings();
221
+ mPresenter.initAndroidTTSSetting();
222
+
223
+
224
+ Button buttonStart = this.findViewById(R.id.button_start);
225
+ buttonStart.setOnClickListener(new View.OnClickListener() {
226
+ @Override
227
+ public void onClick(View v) {
228
+ abc();
229
+ }
230
+ });
231
+
232
+ }
233
+
234
+ public void abc(){
235
+ mPresenter.startSpeak("Hello.World.");
236
+ }
237
+
238
+ @Override
239
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
240
+ super.onActivityResult(requestCode, resultCode, data);
241
+ mPresenter.onTextToSpeechResult(this, requestCode, resultCode);
242
+ }
243
+
244
+ public void onBackPressed() {
245
+ mPresenter.exitApp();
246
+ }
247
+
248
+ }
249
+ ```