@capacitor-community/text-to-speech 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,162 +1,186 @@
1
- package com.getcapacitor.community.tts;
2
-
3
- import android.content.Context;
4
- import android.content.Intent;
5
- import android.content.pm.PackageManager;
6
- import android.content.pm.ResolveInfo;
7
- import android.os.Build;
8
- import android.os.Bundle;
9
- import android.speech.tts.UtteranceProgressListener;
10
- import android.speech.tts.Voice;
11
- import android.util.Log;
12
- import com.getcapacitor.JSArray;
13
- import com.getcapacitor.JSObject;
14
- import java.util.ArrayList;
15
- import java.util.HashMap;
16
- import java.util.Locale;
17
- import java.util.Set;
18
-
19
- public class TextToSpeech implements android.speech.tts.TextToSpeech.OnInitListener {
20
-
21
- public static final String LOG_TAG = "TextToSpeech";
22
-
23
- private Context context;
24
- private android.speech.tts.TextToSpeech tts = null;
25
- private int initializationStatus;
26
- private JSObject[] supportedVoices = null;
27
-
28
- TextToSpeech(Context context) {
29
- this.context = context;
30
- try {
31
- tts = new android.speech.tts.TextToSpeech(context, this);
32
- } catch (Exception ex) {
33
- Log.d(LOG_TAG, ex.getLocalizedMessage());
34
- }
35
- }
36
-
37
- @Override
38
- public void onInit(int status) {
39
- this.initializationStatus = status;
40
- }
41
-
42
- public void speak(
43
- String text,
44
- String lang,
45
- float rate,
46
- float pitch,
47
- float volume,
48
- String callbackId,
49
- SpeakResultCallback resultCallback
50
- ) {
51
- tts.stop();
52
- tts.setOnUtteranceProgressListener(
53
- new UtteranceProgressListener() {
54
- @Override
55
- public void onStart(String utteranceId) {}
56
-
57
- @Override
58
- public void onDone(String utteranceId) {
59
- resultCallback.onDone();
60
- }
61
-
62
- @Override
63
- public void onError(String utteranceId) {
64
- resultCallback.onError();
65
- }
66
- }
67
- );
68
-
69
- Locale locale = Locale.forLanguageTag(lang);
70
-
71
- if (Build.VERSION.SDK_INT >= 21) {
72
- Bundle ttsParams = new Bundle();
73
- ttsParams.putSerializable(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackId);
74
- ttsParams.putSerializable(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_VOLUME, volume);
75
-
76
- tts.setLanguage(locale);
77
- tts.setSpeechRate(rate);
78
- tts.setPitch(pitch);
79
- tts.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, ttsParams, callbackId);
80
- } else {
81
- HashMap<String, String> ttsParams = new HashMap<>();
82
- ttsParams.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackId);
83
- ttsParams.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_VOLUME, Float.toString(volume));
84
-
85
- tts.setLanguage(locale);
86
- tts.setSpeechRate(rate);
87
- tts.setPitch(pitch);
88
- tts.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, ttsParams);
89
- }
90
- }
91
-
92
- public void stop() {
93
- tts.stop();
94
- }
95
-
96
- public JSArray getSupportedLanguages() {
97
- ArrayList<String> languages = new ArrayList<>();
98
- Set<Locale> supportedLocales = tts.getAvailableLanguages();
99
- for (Locale supportedLocale : supportedLocales) {
100
- String tag = supportedLocale.toLanguageTag();
101
- languages.add(tag);
102
- }
103
- JSArray result = JSArray.from(languages.toArray());
104
- return result;
105
- }
106
-
107
- public JSArray getSupportedVoices() {
108
- ArrayList<JSObject> voices = new ArrayList<>();
109
- Set<Voice> supportedVoices = tts.getVoices();
110
- for (Voice supportedVoice : supportedVoices) {
111
- JSObject obj = this.convertVoiceToJSObject(supportedVoice);
112
- voices.add(obj);
113
- }
114
- JSArray result = JSArray.from(voices.toArray());
115
- return result;
116
- }
117
-
118
- public void openInstall() {
119
- PackageManager packageManager = context.getPackageManager();
120
- Intent installIntent = new Intent();
121
- installIntent.setAction(android.speech.tts.TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
122
-
123
- ResolveInfo resolveInfo = packageManager.resolveActivity(installIntent, PackageManager.MATCH_DEFAULT_ONLY);
124
-
125
- if (resolveInfo != null) {
126
- installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
127
- context.startActivity(installIntent);
128
- }
129
- }
130
-
131
- public boolean isAvailable() {
132
- if (tts != null && initializationStatus == android.speech.tts.TextToSpeech.SUCCESS) {
133
- return true;
134
- }
135
- return false;
136
- }
137
-
138
- public boolean isLanguageSupported(String lang) {
139
- Locale locale = Locale.forLanguageTag(lang);
140
- int result = tts.isLanguageAvailable(locale);
141
- return result == tts.LANG_AVAILABLE || result == tts.LANG_COUNTRY_AVAILABLE || result == tts.LANG_COUNTRY_VAR_AVAILABLE;
142
- }
143
-
144
- public void onDestroy() {
145
- if (tts == null) {
146
- return;
147
- }
148
- tts.stop();
149
- tts.shutdown();
150
- }
151
-
152
- private JSObject convertVoiceToJSObject(Voice voice) {
153
- Locale locale = voice.getLocale();
154
- JSObject obj = new JSObject();
155
- obj.put("voiceURI", voice.getName());
156
- obj.put("name", locale.getDisplayLanguage() + " " + locale.getDisplayCountry());
157
- obj.put("lang", locale.toLanguageTag());
158
- obj.put("localService", !voice.isNetworkConnectionRequired());
159
- obj.put("default", false);
160
- return obj;
161
- }
162
- }
1
+ package com.getcapacitor.community.tts;
2
+
3
+ import android.content.Context;
4
+ import android.content.Intent;
5
+ import android.content.pm.PackageManager;
6
+ import android.content.pm.ResolveInfo;
7
+ import android.os.Build;
8
+ import android.os.Bundle;
9
+ import android.speech.tts.UtteranceProgressListener;
10
+ import android.speech.tts.Voice;
11
+ import android.util.Log;
12
+ import com.getcapacitor.JSArray;
13
+ import com.getcapacitor.JSObject;
14
+ import java.util.ArrayList;
15
+ import java.util.Comparator;
16
+ import java.util.HashMap;
17
+ import java.util.Locale;
18
+ import java.util.Set;
19
+
20
+ public class TextToSpeech implements android.speech.tts.TextToSpeech.OnInitListener {
21
+
22
+ public static final String LOG_TAG = "TextToSpeech";
23
+
24
+ private Context context;
25
+ private android.speech.tts.TextToSpeech tts = null;
26
+ private int initializationStatus;
27
+ private JSObject[] supportedVoices = null;
28
+
29
+ TextToSpeech(Context context) {
30
+ this.context = context;
31
+ try {
32
+ tts = new android.speech.tts.TextToSpeech(context, this);
33
+ } catch (Exception ex) {
34
+ Log.d(LOG_TAG, ex.getLocalizedMessage());
35
+ }
36
+ }
37
+
38
+ @Override
39
+ public void onInit(int status) {
40
+ this.initializationStatus = status;
41
+ }
42
+
43
+ public void speak(
44
+ String text,
45
+ String lang,
46
+ float rate,
47
+ float pitch,
48
+ float volume,
49
+ int voice,
50
+ String callbackId,
51
+ SpeakResultCallback resultCallback
52
+ ) {
53
+ tts.stop();
54
+ tts.setOnUtteranceProgressListener(
55
+ new UtteranceProgressListener() {
56
+ @Override
57
+ public void onStart(String utteranceId) {}
58
+
59
+ @Override
60
+ public void onDone(String utteranceId) {
61
+ resultCallback.onDone();
62
+ }
63
+
64
+ @Override
65
+ public void onError(String utteranceId) {
66
+ resultCallback.onError();
67
+ }
68
+ }
69
+ );
70
+
71
+ Locale locale = Locale.forLanguageTag(lang);
72
+
73
+ if (Build.VERSION.SDK_INT >= 21) {
74
+ Bundle ttsParams = new Bundle();
75
+ ttsParams.putSerializable(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackId);
76
+ ttsParams.putSerializable(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_VOLUME, volume);
77
+
78
+ tts.setLanguage(locale);
79
+ tts.setSpeechRate(rate);
80
+ tts.setPitch(pitch);
81
+
82
+ if (voice >= 0) {
83
+ ArrayList<Voice> supportedVoices = getSupportedVoicesOrdered();
84
+ if (voice < supportedVoices.size()) {
85
+ Voice newVoice = supportedVoices.get(voice);
86
+ int resultCode = tts.setVoice(newVoice);
87
+ }
88
+ }
89
+
90
+ tts.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, ttsParams, callbackId);
91
+ } else {
92
+ HashMap<String, String> ttsParams = new HashMap<>();
93
+ ttsParams.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackId);
94
+ ttsParams.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_VOLUME, Float.toString(volume));
95
+
96
+ tts.setLanguage(locale);
97
+ tts.setSpeechRate(rate);
98
+ tts.setPitch(pitch);
99
+ tts.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, ttsParams);
100
+ }
101
+ }
102
+
103
+ public void stop() {
104
+ tts.stop();
105
+ }
106
+
107
+ public JSArray getSupportedLanguages() {
108
+ ArrayList<String> languages = new ArrayList<>();
109
+ Set<Locale> supportedLocales = tts.getAvailableLanguages();
110
+ for (Locale supportedLocale : supportedLocales) {
111
+ String tag = supportedLocale.toLanguageTag();
112
+ languages.add(tag);
113
+ }
114
+ JSArray result = JSArray.from(languages.toArray());
115
+ return result;
116
+ }
117
+
118
+ public ArrayList<Voice> getSupportedVoicesOrdered() {
119
+ Set<Voice> supportedVoices = tts.getVoices();
120
+ ArrayList<Voice> orderedVoices = new ArrayList<Voice>();
121
+ for (Voice supportedVoice : supportedVoices) {
122
+ orderedVoices.add(supportedVoice);
123
+ }
124
+
125
+ Comparator<Voice> voiceComparator = Comparator.comparing(v -> v.hashCode());
126
+ orderedVoices.sort(voiceComparator);
127
+
128
+ return orderedVoices;
129
+ }
130
+
131
+ public JSArray getSupportedVoices() {
132
+ ArrayList<JSObject> voices = new ArrayList<>();
133
+ ArrayList<Voice> supportedVoices = getSupportedVoicesOrdered();
134
+ for (Voice supportedVoice : supportedVoices) {
135
+ JSObject obj = this.convertVoiceToJSObject(supportedVoice);
136
+ voices.add(obj);
137
+ }
138
+ JSArray result = JSArray.from(voices.toArray());
139
+ return result;
140
+ }
141
+
142
+ public void openInstall() {
143
+ PackageManager packageManager = context.getPackageManager();
144
+ Intent installIntent = new Intent();
145
+ installIntent.setAction(android.speech.tts.TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
146
+
147
+ ResolveInfo resolveInfo = packageManager.resolveActivity(installIntent, PackageManager.MATCH_DEFAULT_ONLY);
148
+
149
+ if (resolveInfo != null) {
150
+ installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
151
+ context.startActivity(installIntent);
152
+ }
153
+ }
154
+
155
+ public boolean isAvailable() {
156
+ if (tts != null && initializationStatus == android.speech.tts.TextToSpeech.SUCCESS) {
157
+ return true;
158
+ }
159
+ return false;
160
+ }
161
+
162
+ public boolean isLanguageSupported(String lang) {
163
+ Locale locale = Locale.forLanguageTag(lang);
164
+ int result = tts.isLanguageAvailable(locale);
165
+ return result == tts.LANG_AVAILABLE || result == tts.LANG_COUNTRY_AVAILABLE || result == tts.LANG_COUNTRY_VAR_AVAILABLE;
166
+ }
167
+
168
+ public void onDestroy() {
169
+ if (tts == null) {
170
+ return;
171
+ }
172
+ tts.stop();
173
+ tts.shutdown();
174
+ }
175
+
176
+ private JSObject convertVoiceToJSObject(Voice voice) {
177
+ Locale locale = voice.getLocale();
178
+ JSObject obj = new JSObject();
179
+ obj.put("voiceURI", voice.getName());
180
+ obj.put("name", locale.getDisplayLanguage() + " " + locale.getDisplayCountry());
181
+ obj.put("lang", locale.toLanguageTag());
182
+ obj.put("localService", !voice.isNetworkConnectionRequired());
183
+ obj.put("default", false);
184
+ return obj;
185
+ }
186
+ }
@@ -1,130 +1,131 @@
1
- package com.getcapacitor.community.tts;
2
-
3
- import com.getcapacitor.JSArray;
4
- import com.getcapacitor.JSObject;
5
- import com.getcapacitor.Plugin;
6
- import com.getcapacitor.PluginCall;
7
- import com.getcapacitor.PluginMethod;
8
- import com.getcapacitor.annotation.CapacitorPlugin;
9
-
10
- @CapacitorPlugin(name = "TextToSpeech")
11
- public class TextToSpeechPlugin extends Plugin {
12
-
13
- public static final String LOG_TAG = "TextToSpeechPlugin";
14
-
15
- public static final String ERROR_UTTERANCE = "Failed to read text.";
16
- public static final String ERROR_UNSUPPORTED_LANGUAGE = "This language is not supported.";
17
-
18
- private TextToSpeech implementation;
19
-
20
- @Override
21
- public void load() {
22
- implementation = new TextToSpeech(getContext());
23
- }
24
-
25
- @PluginMethod
26
- public void speak(PluginCall call) {
27
- boolean isAvailable = implementation.isAvailable();
28
- if (!isAvailable) {
29
- call.unavailable("Not yet initialized or not available on this device.");
30
- return;
31
- }
32
-
33
- String text = call.getString("text", "");
34
- String lang = call.getString("lang", "en-US");
35
- float rate = call.getFloat("rate", 1.0f);
36
- float pitch = call.getFloat("pitch", 1.0f);
37
- float volume = call.getFloat("volume", 1.0f);
38
-
39
- boolean isLanguageSupported = implementation.isLanguageSupported(lang);
40
- if (!isLanguageSupported) {
41
- call.reject(ERROR_UNSUPPORTED_LANGUAGE);
42
- return;
43
- }
44
-
45
- SpeakResultCallback resultCallback = new SpeakResultCallback() {
46
- @Override
47
- public void onDone() {
48
- call.resolve();
49
- }
50
-
51
- @Override
52
- public void onError() {
53
- call.reject(ERROR_UTTERANCE);
54
- }
55
- };
56
-
57
- try {
58
- implementation.speak(text, lang, rate, pitch, volume, call.getCallbackId(), resultCallback);
59
- } catch (Exception ex) {
60
- call.reject(ex.getLocalizedMessage());
61
- }
62
- }
63
-
64
- @PluginMethod
65
- public void stop(PluginCall call) {
66
- boolean isAvailable = implementation.isAvailable();
67
- if (!isAvailable) {
68
- call.unavailable("Not yet initialized or not available on this device.");
69
- return;
70
- }
71
- try {
72
- implementation.stop();
73
- call.resolve();
74
- } catch (Exception ex) {
75
- call.reject(ex.getLocalizedMessage());
76
- }
77
- }
78
-
79
- @PluginMethod
80
- public void getSupportedLanguages(PluginCall call) {
81
- try {
82
- JSArray languages = implementation.getSupportedLanguages();
83
- JSObject ret = new JSObject();
84
- ret.put("languages", languages);
85
- call.resolve(ret);
86
- } catch (Exception ex) {
87
- call.reject(ex.getLocalizedMessage());
88
- }
89
- }
90
-
91
- @PluginMethod
92
- public void getSupportedVoices(PluginCall call) {
93
- try {
94
- JSArray voices = implementation.getSupportedVoices();
95
- JSObject ret = new JSObject();
96
- ret.put("voices", voices);
97
- call.resolve(ret);
98
- } catch (Exception ex) {
99
- call.reject(ex.getLocalizedMessage());
100
- }
101
- }
102
-
103
- @PluginMethod
104
- public void isLanguageSupported(PluginCall call) {
105
- String lang = call.getString("lang", "");
106
- try {
107
- boolean isLanguageSupported = implementation.isLanguageSupported(lang);
108
- JSObject ret = new JSObject();
109
- ret.put("supported", isLanguageSupported);
110
- call.resolve(ret);
111
- } catch (Exception ex) {
112
- call.reject(ex.getLocalizedMessage());
113
- }
114
- }
115
-
116
- @PluginMethod
117
- public void openInstall(PluginCall call) {
118
- try {
119
- implementation.openInstall();
120
- call.resolve();
121
- } catch (Exception ex) {
122
- call.reject(ex.getLocalizedMessage());
123
- }
124
- }
125
-
126
- @Override
127
- protected void handleOnDestroy() {
128
- implementation.onDestroy();
129
- }
130
- }
1
+ package com.getcapacitor.community.tts;
2
+
3
+ import com.getcapacitor.JSArray;
4
+ import com.getcapacitor.JSObject;
5
+ import com.getcapacitor.Plugin;
6
+ import com.getcapacitor.PluginCall;
7
+ import com.getcapacitor.PluginMethod;
8
+ import com.getcapacitor.annotation.CapacitorPlugin;
9
+
10
+ @CapacitorPlugin(name = "TextToSpeech")
11
+ public class TextToSpeechPlugin extends Plugin {
12
+
13
+ public static final String LOG_TAG = "TextToSpeechPlugin";
14
+
15
+ public static final String ERROR_UTTERANCE = "Failed to read text.";
16
+ public static final String ERROR_UNSUPPORTED_LANGUAGE = "This language is not supported.";
17
+
18
+ private TextToSpeech implementation;
19
+
20
+ @Override
21
+ public void load() {
22
+ implementation = new TextToSpeech(getContext());
23
+ }
24
+
25
+ @PluginMethod
26
+ public void speak(PluginCall call) {
27
+ boolean isAvailable = implementation.isAvailable();
28
+ if (!isAvailable) {
29
+ call.unavailable("Not yet initialized or not available on this device.");
30
+ return;
31
+ }
32
+
33
+ String text = call.getString("text", "");
34
+ String lang = call.getString("lang", "en-US");
35
+ float rate = call.getFloat("rate", 1.0f);
36
+ float pitch = call.getFloat("pitch", 1.0f);
37
+ float volume = call.getFloat("volume", 1.0f);
38
+ int voice = call.getInt("voice", -1);
39
+
40
+ boolean isLanguageSupported = implementation.isLanguageSupported(lang);
41
+ if (!isLanguageSupported) {
42
+ call.reject(ERROR_UNSUPPORTED_LANGUAGE);
43
+ return;
44
+ }
45
+
46
+ SpeakResultCallback resultCallback = new SpeakResultCallback() {
47
+ @Override
48
+ public void onDone() {
49
+ call.resolve();
50
+ }
51
+
52
+ @Override
53
+ public void onError() {
54
+ call.reject(ERROR_UTTERANCE);
55
+ }
56
+ };
57
+
58
+ try {
59
+ implementation.speak(text, lang, rate, pitch, volume, voice, call.getCallbackId(), resultCallback);
60
+ } catch (Exception ex) {
61
+ call.reject(ex.getLocalizedMessage());
62
+ }
63
+ }
64
+
65
+ @PluginMethod
66
+ public void stop(PluginCall call) {
67
+ boolean isAvailable = implementation.isAvailable();
68
+ if (!isAvailable) {
69
+ call.unavailable("Not yet initialized or not available on this device.");
70
+ return;
71
+ }
72
+ try {
73
+ implementation.stop();
74
+ call.resolve();
75
+ } catch (Exception ex) {
76
+ call.reject(ex.getLocalizedMessage());
77
+ }
78
+ }
79
+
80
+ @PluginMethod
81
+ public void getSupportedLanguages(PluginCall call) {
82
+ try {
83
+ JSArray languages = implementation.getSupportedLanguages();
84
+ JSObject ret = new JSObject();
85
+ ret.put("languages", languages);
86
+ call.resolve(ret);
87
+ } catch (Exception ex) {
88
+ call.reject(ex.getLocalizedMessage());
89
+ }
90
+ }
91
+
92
+ @PluginMethod
93
+ public void getSupportedVoices(PluginCall call) {
94
+ try {
95
+ JSArray voices = implementation.getSupportedVoices();
96
+ JSObject ret = new JSObject();
97
+ ret.put("voices", voices);
98
+ call.resolve(ret);
99
+ } catch (Exception ex) {
100
+ call.reject(ex.getLocalizedMessage());
101
+ }
102
+ }
103
+
104
+ @PluginMethod
105
+ public void isLanguageSupported(PluginCall call) {
106
+ String lang = call.getString("lang", "");
107
+ try {
108
+ boolean isLanguageSupported = implementation.isLanguageSupported(lang);
109
+ JSObject ret = new JSObject();
110
+ ret.put("supported", isLanguageSupported);
111
+ call.resolve(ret);
112
+ } catch (Exception ex) {
113
+ call.reject(ex.getLocalizedMessage());
114
+ }
115
+ }
116
+
117
+ @PluginMethod
118
+ public void openInstall(PluginCall call) {
119
+ try {
120
+ implementation.openInstall();
121
+ call.resolve();
122
+ } catch (Exception ex) {
123
+ call.reject(ex.getLocalizedMessage());
124
+ }
125
+ }
126
+
127
+ @Override
128
+ protected void handleOnDestroy() {
129
+ implementation.onDestroy();
130
+ }
131
+ }