@capacitor-community/text-to-speech 1.1.1 → 1.1.3-dev.690a781.1659193180
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.
- package/CapacitorCommunityTextToSpeech.podspec +17 -17
- package/LICENSE +21 -21
- package/README.md +102 -102
- package/android/build.gradle +57 -58
- package/android/src/main/AndroidManifest.xml +3 -3
- package/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java +162 -162
- package/android/src/main/java/com/getcapacitor/community/tts/TextToSpeechPlugin.java +130 -130
- package/dist/docs.json +10 -10
- package/dist/esm/definitions.d.ts +112 -112
- package/dist/esm/definitions.js +1 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +4 -4
- package/dist/esm/index.js +10 -10
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +25 -25
- package/dist/esm/web.js +91 -88
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +91 -88
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +92 -89
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/Info.plist +24 -24
- package/ios/Plugin/TextToSpeech.swift +81 -81
- package/ios/Plugin/TextToSpeechPlugin.h +10 -10
- package/ios/Plugin/TextToSpeechPlugin.m +13 -13
- package/ios/Plugin/TextToSpeechPlugin.swift +64 -64
- package/package.json +81 -80
- package/CHANGELOG.md +0 -83
|
@@ -1,162 +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
|
-
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.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,130 +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 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
|
+
|
|
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
|
+
}
|
package/dist/docs.json
CHANGED
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
"parameters": [],
|
|
78
78
|
"returns": "Promise<void>",
|
|
79
79
|
"tags": [],
|
|
80
|
-
"docs": "Verifies proper installation and availability of resource files on the system.\
|
|
80
|
+
"docs": "Verifies proper installation and availability of resource files on the system.\n\nOnly available for Android.",
|
|
81
81
|
"complexTypes": [],
|
|
82
82
|
"slug": "openinstall"
|
|
83
83
|
}
|
|
@@ -102,42 +102,42 @@
|
|
|
102
102
|
{
|
|
103
103
|
"name": "lang",
|
|
104
104
|
"tags": [],
|
|
105
|
-
"docs": "The language of the utterance.\
|
|
105
|
+
"docs": "The language of the utterance.\nPossible languages can be queried using `getSupportedLanguages`.\n\nDefault: `en-US`.",
|
|
106
106
|
"complexTypes": [],
|
|
107
107
|
"type": "string | undefined"
|
|
108
108
|
},
|
|
109
109
|
{
|
|
110
110
|
"name": "rate",
|
|
111
111
|
"tags": [],
|
|
112
|
-
"docs": "The speed at which the utterance will be spoken at.\
|
|
112
|
+
"docs": "The speed at which the utterance will be spoken at.\n\nDefault: `1.0`.",
|
|
113
113
|
"complexTypes": [],
|
|
114
114
|
"type": "number | undefined"
|
|
115
115
|
},
|
|
116
116
|
{
|
|
117
117
|
"name": "pitch",
|
|
118
118
|
"tags": [],
|
|
119
|
-
"docs": "The pitch at which the utterance will be spoken at.\
|
|
119
|
+
"docs": "The pitch at which the utterance will be spoken at.\n\nDefault: `1.0`.",
|
|
120
120
|
"complexTypes": [],
|
|
121
121
|
"type": "number | undefined"
|
|
122
122
|
},
|
|
123
123
|
{
|
|
124
124
|
"name": "volume",
|
|
125
125
|
"tags": [],
|
|
126
|
-
"docs": "The volume that the utterance will be spoken at.\
|
|
126
|
+
"docs": "The volume that the utterance will be spoken at.\n\nDefault: `1.0`.",
|
|
127
127
|
"complexTypes": [],
|
|
128
128
|
"type": "number | undefined"
|
|
129
129
|
},
|
|
130
130
|
{
|
|
131
131
|
"name": "voice",
|
|
132
132
|
"tags": [],
|
|
133
|
-
"docs": "The index of the selected voice that will be used to speak the utterance.\
|
|
133
|
+
"docs": "The index of the selected voice that will be used to speak the utterance.\nPossible voices can be queried using `getSupportedVoices`.\n\nOnly available for Web.",
|
|
134
134
|
"complexTypes": [],
|
|
135
135
|
"type": "number | undefined"
|
|
136
136
|
},
|
|
137
137
|
{
|
|
138
138
|
"name": "category",
|
|
139
139
|
"tags": [],
|
|
140
|
-
"docs": "Select the iOS Audio session category.\
|
|
140
|
+
"docs": "Select the iOS Audio session category.\nPossible values: `ambient` and `playback`.\nUse `playback` to play audio even when the app is in the background.\n\nOnly available for iOS.\n\nDefault: `ambient`.",
|
|
141
141
|
"complexTypes": [],
|
|
142
142
|
"type": "string | undefined"
|
|
143
143
|
}
|
|
@@ -160,7 +160,7 @@
|
|
|
160
160
|
{
|
|
161
161
|
"name": "lang",
|
|
162
162
|
"tags": [],
|
|
163
|
-
"docs": "BCP 47 language tag indicating the language of the voice.\
|
|
163
|
+
"docs": "BCP 47 language tag indicating the language of the voice.\nExample: `en-US`.",
|
|
164
164
|
"complexTypes": [],
|
|
165
165
|
"type": "string"
|
|
166
166
|
},
|
|
@@ -174,14 +174,14 @@
|
|
|
174
174
|
{
|
|
175
175
|
"name": "name",
|
|
176
176
|
"tags": [],
|
|
177
|
-
"docs": "Human-readable name that represents the voice.\
|
|
177
|
+
"docs": "Human-readable name that represents the voice.\nExample: `Microsoft Zira Desktop - English (United States)`.",
|
|
178
178
|
"complexTypes": [],
|
|
179
179
|
"type": "string"
|
|
180
180
|
},
|
|
181
181
|
{
|
|
182
182
|
"name": "voiceURI",
|
|
183
183
|
"tags": [],
|
|
184
|
-
"docs": "Type of URI and location of the speech synthesis service for this voice.\
|
|
184
|
+
"docs": "Type of URI and location of the speech synthesis service for this voice.\nExample: `urn:moz-tts:sapi:Microsoft Zira Desktop - English (United States)?en-US`.",
|
|
185
185
|
"complexTypes": [],
|
|
186
186
|
"type": "string"
|
|
187
187
|
}
|