@capacitor-community/text-to-speech 1.0.0 → 1.1.2-dev.aaa3396.1649088226

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,154 +1,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.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
- if (Build.VERSION.SDK_INT >= 21) {
70
- Bundle ttsParams = new Bundle();
71
- ttsParams.putSerializable(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackId);
72
- ttsParams.putSerializable(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_VOLUME, volume);
73
-
74
- tts.setLanguage(new Locale(lang));
75
- tts.setSpeechRate(rate);
76
- tts.setPitch(pitch);
77
- tts.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, ttsParams, callbackId);
78
- } else {
79
- HashMap<String, String> ttsParams = new HashMap<>();
80
- ttsParams.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackId);
81
- ttsParams.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_VOLUME, Float.toString(volume));
82
-
83
- tts.setLanguage(new Locale(lang));
84
- tts.setSpeechRate(rate);
85
- tts.setPitch(pitch);
86
- tts.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, ttsParams);
87
- }
88
- }
89
-
90
- public void stop() {
91
- tts.stop();
92
- }
93
-
94
- public JSArray getSupportedLanguages() {
95
- ArrayList<String> languages = new ArrayList<>();
96
- Set<Locale> supportedLocales = tts.getAvailableLanguages();
97
- for (Locale supportedLocale : supportedLocales) {
98
- String tag = supportedLocale.toLanguageTag();
99
- languages.add(tag);
100
- }
101
- JSArray result = JSArray.from(languages.toArray());
102
- return result;
103
- }
104
-
105
- public JSArray getSupportedVoices() {
106
- ArrayList<JSObject> voices = new ArrayList<>();
107
- Set<Voice> supportedVoices = tts.getVoices();
108
- for (Voice supportedVoice : supportedVoices) {
109
- JSObject obj = this.convertVoiceToJSObject(supportedVoice);
110
- voices.add(obj);
111
- }
112
- JSArray result = JSArray.from(voices.toArray());
113
- return result;
114
- }
115
-
116
- public void openInstall() {
117
- PackageManager packageManager = context.getPackageManager();
118
- Intent installIntent = new Intent();
119
- installIntent.setAction(android.speech.tts.TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
120
-
121
- ResolveInfo resolveInfo = packageManager.resolveActivity(installIntent, PackageManager.MATCH_DEFAULT_ONLY);
122
-
123
- if (resolveInfo != null) {
124
- installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
125
- context.startActivity(installIntent);
126
- }
127
- }
128
-
129
- public boolean isAvailable() {
130
- if (tts != null && initializationStatus == android.speech.tts.TextToSpeech.SUCCESS) {
131
- return true;
132
- }
133
- return false;
134
- }
135
-
136
- public boolean isLanguageSupported(String lang) {
137
- Set<Locale> supportedLocales = tts.getAvailableLanguages();
138
- if (supportedLocales.contains(Locale.forLanguageTag(lang))) {
139
- return true;
140
- }
141
- return false;
142
- }
143
-
144
- private JSObject convertVoiceToJSObject(Voice voice) {
145
- Locale locale = voice.getLocale();
146
- JSObject obj = new JSObject();
147
- obj.put("voiceURI", voice.getName());
148
- obj.put("name", locale.getDisplayLanguage() + " " + locale.getDisplayCountry());
149
- obj.put("lang", locale.toLanguageTag());
150
- obj.put("localService", !voice.isNetworkConnectionRequired());
151
- obj.put("default", false);
152
- return obj;
153
- }
154
- }
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,112 +1,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
-
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 openInstall(PluginCall call) {
105
- try {
106
- implementation.openInstall();
107
- call.resolve();
108
- } catch (Exception ex) {
109
- call.reject(ex.getLocalizedMessage());
110
- }
111
- }
112
- }
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
+ }