@capacitor-community/text-to-speech 0.2.3 → 1.1.2
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 +1 -1
- package/LICENSE +1 -1
- package/README.md +169 -113
- package/android/build.gradle +14 -8
- package/android/src/main/AndroidManifest.xml +3 -5
- package/android/src/main/java/com/getcapacitor/community/tts/SpeakResultCallback.java +6 -0
- package/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java +98 -180
- package/android/src/main/java/com/getcapacitor/community/tts/TextToSpeechPlugin.java +130 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +194 -0
- package/dist/esm/definitions.d.ts +70 -17
- package/dist/esm/definitions.js +1 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +9 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +7 -9
- package/dist/esm/web.js +48 -76
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +57 -75
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +59 -75
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/Info.plist +1 -1
- package/ios/Plugin/TextToSpeech.swift +81 -0
- package/ios/Plugin/{Plugin.h → TextToSpeechPlugin.h} +0 -0
- package/ios/Plugin/{Plugin.m → TextToSpeechPlugin.m} +2 -3
- package/ios/Plugin/TextToSpeechPlugin.swift +64 -0
- package/package.json +36 -21
- package/CHANGELOG.md +0 -46
- package/android/src/main/java/com/getcapacitor/community/tts/Constant.java +0 -8
- package/android/src/main/res/layout/bridge_layout_main.xml +0 -15
- package/android/src/main/res/values/colors.xml +0 -3
- package/android/src/main/res/values/strings.xml +0 -3
- package/android/src/main/res/values/styles.xml +0 -3
- package/ios/Plugin/Plugin.swift +0 -119
package/dist/plugin.js
CHANGED
|
@@ -1,100 +1,77 @@
|
|
|
1
1
|
var capacitorTextToSpeech = (function (exports, core) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
9
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
10
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
11
|
-
});
|
|
12
|
-
};
|
|
4
|
+
const TextToSpeech = core.registerPlugin('TextToSpeech', {
|
|
5
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.TextToSpeechWeb()),
|
|
6
|
+
});
|
|
7
|
+
|
|
13
8
|
class TextToSpeechWeb extends core.WebPlugin {
|
|
14
9
|
constructor() {
|
|
15
|
-
super(
|
|
16
|
-
name: 'TextToSpeech',
|
|
17
|
-
platforms: ['web'],
|
|
18
|
-
});
|
|
10
|
+
super();
|
|
19
11
|
this.speechSynthesis = null;
|
|
20
12
|
if ('speechSynthesis' in window) {
|
|
21
13
|
this.speechSynthesis = window.speechSynthesis;
|
|
22
14
|
}
|
|
23
15
|
}
|
|
24
|
-
speak(options) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
speechSynthesis.speak(utterance);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
stop() {
|
|
44
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
if (!this.speechSynthesis) {
|
|
46
|
-
this.throwUnsupportedError();
|
|
47
|
-
}
|
|
48
|
-
this.speechSynthesis.cancel();
|
|
16
|
+
async speak(options) {
|
|
17
|
+
if (!this.speechSynthesis) {
|
|
18
|
+
this.throwUnsupportedError();
|
|
19
|
+
}
|
|
20
|
+
await this.stop();
|
|
21
|
+
const speechSynthesis = this.speechSynthesis;
|
|
22
|
+
const utterance = this.createSpeechSynthesisUtterance(options);
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
utterance.onend = () => {
|
|
25
|
+
resolve();
|
|
26
|
+
};
|
|
27
|
+
utterance.onerror = (event) => {
|
|
28
|
+
reject(event);
|
|
29
|
+
};
|
|
30
|
+
speechSynthesis.speak(utterance);
|
|
49
31
|
});
|
|
50
32
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return { languages: filteredLanguages };
|
|
57
|
-
});
|
|
33
|
+
async stop() {
|
|
34
|
+
if (!this.speechSynthesis) {
|
|
35
|
+
this.throwUnsupportedError();
|
|
36
|
+
}
|
|
37
|
+
this.speechSynthesis.cancel();
|
|
58
38
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
39
|
+
async getSupportedLanguages() {
|
|
40
|
+
const voices = this.getSpeechSynthesisVoices();
|
|
41
|
+
const languages = voices.map(voice => voice.lang);
|
|
42
|
+
const filteredLanguages = languages.filter((v, i, a) => a.indexOf(v) == i);
|
|
43
|
+
return { languages: filteredLanguages };
|
|
64
44
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
});
|
|
45
|
+
async getSupportedVoices() {
|
|
46
|
+
const voices = this.getSpeechSynthesisVoices();
|
|
47
|
+
return { voices };
|
|
69
48
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
49
|
+
async isLanguageSupported(options) {
|
|
50
|
+
const result = await this.getSupportedLanguages();
|
|
51
|
+
const isLanguageSupported = result.languages.includes(options.lang);
|
|
52
|
+
return { supported: isLanguageSupported };
|
|
74
53
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.throwUnimplementedError();
|
|
78
|
-
});
|
|
54
|
+
async openInstall() {
|
|
55
|
+
this.throwUnimplementedError();
|
|
79
56
|
}
|
|
80
57
|
createSpeechSynthesisUtterance(options) {
|
|
81
58
|
const voices = this.getSpeechSynthesisVoices();
|
|
82
59
|
const utterance = new SpeechSynthesisUtterance();
|
|
83
|
-
const { text,
|
|
60
|
+
const { text, lang, rate, pitch, volume, voice } = options;
|
|
84
61
|
if (voice) {
|
|
85
62
|
utterance.voice = voices[voice];
|
|
86
63
|
}
|
|
87
64
|
if (volume) {
|
|
88
65
|
utterance.volume = volume >= 0 && volume <= 1 ? volume : 1;
|
|
89
66
|
}
|
|
90
|
-
if (
|
|
91
|
-
utterance.rate =
|
|
67
|
+
if (rate) {
|
|
68
|
+
utterance.rate = rate >= 0.1 && rate <= 10 ? rate : 1;
|
|
92
69
|
}
|
|
93
|
-
if (
|
|
94
|
-
utterance.pitch =
|
|
70
|
+
if (pitch) {
|
|
71
|
+
utterance.pitch = pitch >= 0 && pitch <= 2 ? pitch : 2;
|
|
95
72
|
}
|
|
96
|
-
if (
|
|
97
|
-
utterance.lang =
|
|
73
|
+
if (lang) {
|
|
74
|
+
utterance.lang = lang;
|
|
98
75
|
}
|
|
99
76
|
utterance.text = text;
|
|
100
77
|
return utterance;
|
|
@@ -103,20 +80,27 @@ var capacitorTextToSpeech = (function (exports, core) {
|
|
|
103
80
|
if (!this.speechSynthesis) {
|
|
104
81
|
this.throwUnsupportedError();
|
|
105
82
|
}
|
|
106
|
-
|
|
83
|
+
if (!this.supportedVoices || this.supportedVoices.length < 1) {
|
|
84
|
+
this.supportedVoices = this.speechSynthesis.getVoices();
|
|
85
|
+
}
|
|
86
|
+
return this.supportedVoices;
|
|
107
87
|
}
|
|
108
88
|
throwUnsupportedError() {
|
|
109
|
-
throw
|
|
89
|
+
throw this.unavailable('SpeechSynthesis API not available in this browser.');
|
|
110
90
|
}
|
|
111
91
|
throwUnimplementedError() {
|
|
112
|
-
throw
|
|
92
|
+
throw this.unimplemented('Not implemented on web.');
|
|
113
93
|
}
|
|
114
94
|
}
|
|
115
|
-
|
|
116
|
-
|
|
95
|
+
|
|
96
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
97
|
+
__proto__: null,
|
|
98
|
+
TextToSpeechWeb: TextToSpeechWeb
|
|
99
|
+
});
|
|
117
100
|
|
|
118
101
|
exports.TextToSpeech = TextToSpeech;
|
|
119
|
-
|
|
102
|
+
|
|
103
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
120
104
|
|
|
121
105
|
return exports;
|
|
122
106
|
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["esm/web.js"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst TextToSpeech = registerPlugin('TextToSpeech', {\n web: () => import('./web').then(m => new m.TextToSpeechWeb()),\n});\n// Warm up\nif ('speechSynthesis' in window) {\n window.speechSynthesis;\n}\nexport * from './definitions';\nexport { TextToSpeech };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class TextToSpeechWeb extends WebPlugin {\n constructor() {\n super();\n this.speechSynthesis = null;\n if ('speechSynthesis' in window) {\n this.speechSynthesis = window.speechSynthesis;\n }\n }\n async speak(options) {\n if (!this.speechSynthesis) {\n this.throwUnsupportedError();\n }\n await this.stop();\n const speechSynthesis = this.speechSynthesis;\n const utterance = this.createSpeechSynthesisUtterance(options);\n return new Promise((resolve, reject) => {\n utterance.onend = () => {\n resolve();\n };\n utterance.onerror = (event) => {\n reject(event);\n };\n speechSynthesis.speak(utterance);\n });\n }\n async stop() {\n if (!this.speechSynthesis) {\n this.throwUnsupportedError();\n }\n this.speechSynthesis.cancel();\n }\n async getSupportedLanguages() {\n const voices = this.getSpeechSynthesisVoices();\n const languages = voices.map(voice => voice.lang);\n const filteredLanguages = languages.filter((v, i, a) => a.indexOf(v) == i);\n return { languages: filteredLanguages };\n }\n async getSupportedVoices() {\n const voices = this.getSpeechSynthesisVoices();\n return { voices };\n }\n async isLanguageSupported(options) {\n const result = await this.getSupportedLanguages();\n const isLanguageSupported = result.languages.includes(options.lang);\n return { supported: isLanguageSupported };\n }\n async openInstall() {\n this.throwUnimplementedError();\n }\n createSpeechSynthesisUtterance(options) {\n const voices = this.getSpeechSynthesisVoices();\n const utterance = new SpeechSynthesisUtterance();\n const { text, lang, rate, pitch, volume, voice } = options;\n if (voice) {\n utterance.voice = voices[voice];\n }\n if (volume) {\n utterance.volume = volume >= 0 && volume <= 1 ? volume : 1;\n }\n if (rate) {\n utterance.rate = rate >= 0.1 && rate <= 10 ? rate : 1;\n }\n if (pitch) {\n utterance.pitch = pitch >= 0 && pitch <= 2 ? pitch : 2;\n }\n if (lang) {\n utterance.lang = lang;\n }\n utterance.text = text;\n return utterance;\n }\n getSpeechSynthesisVoices() {\n if (!this.speechSynthesis) {\n this.throwUnsupportedError();\n }\n if (!this.supportedVoices || this.supportedVoices.length < 1) {\n this.supportedVoices = this.speechSynthesis.getVoices();\n }\n return this.supportedVoices;\n }\n throwUnsupportedError() {\n throw this.unavailable('SpeechSynthesis API not available in this browser.');\n }\n throwUnimplementedError() {\n throw this.unimplemented('Not implemented on web.');\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,YAAY,GAAGA,mBAAc,CAAC,cAAc,EAAE;IACpD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,eAAe,EAAE,CAAC;IACjE,CAAC;;ICFM,MAAM,eAAe,SAASC,cAAS,CAAC;IAC/C,IAAI,WAAW,GAAG;IAClB,QAAQ,KAAK,EAAE,CAAC;IAChB,QAAQ,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;IACpC,QAAQ,IAAI,iBAAiB,IAAI,MAAM,EAAE;IACzC,YAAY,IAAI,CAAC,eAAe,GA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string>1.0</string>
|
|
19
19
|
<key>CFBundleVersion</key>
|
|
20
20
|
<string>$(CURRENT_PROJECT_VERSION)</string>
|
|
21
21
|
<key>NSPrincipalClass</key>
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class TextToSpeech: NSObject, AVSpeechSynthesizerDelegate {
|
|
5
|
+
let synthesizer = AVSpeechSynthesizer()
|
|
6
|
+
var calls: [CAPPluginCall] = []
|
|
7
|
+
|
|
8
|
+
override init() {
|
|
9
|
+
super.init()
|
|
10
|
+
self.synthesizer.delegate = self
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
|
|
14
|
+
self.resolveCurrentCall()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
|
|
18
|
+
self.resolveCurrentCall()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@objc public func speak(_ text: String, _ lang: String, _ rate: Float, _ pitch: Float, _ category: String, _ volume: Float, _ call: CAPPluginCall) throws {
|
|
22
|
+
self.synthesizer.stopSpeaking(at: .immediate)
|
|
23
|
+
|
|
24
|
+
var avAudioSessionCategory = AVAudioSession.Category.ambient
|
|
25
|
+
if category != "ambient" {
|
|
26
|
+
avAudioSessionCategory = AVAudioSession.Category.playback
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
try AVAudioSession.sharedInstance().setCategory(avAudioSessionCategory, mode: .default, options: AVAudioSession.CategoryOptions.duckOthers)
|
|
30
|
+
try AVAudioSession.sharedInstance().setActive(true)
|
|
31
|
+
|
|
32
|
+
self.calls.append(call)
|
|
33
|
+
|
|
34
|
+
let utterance = AVSpeechUtterance(string: text)
|
|
35
|
+
utterance.voice = AVSpeechSynthesisVoice(language: lang)
|
|
36
|
+
utterance.rate = adjustRate(rate)
|
|
37
|
+
utterance.pitchMultiplier = pitch
|
|
38
|
+
utterance.volume = volume
|
|
39
|
+
synthesizer.speak(utterance)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@objc public func stop() {
|
|
43
|
+
synthesizer.stopSpeaking(at: .immediate)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@objc public func getSupportedLanguages() -> [String] {
|
|
47
|
+
return Array(AVSpeechSynthesisVoice.speechVoices().map {
|
|
48
|
+
return $0.language
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@objc public func isLanguageSupported(_ lang: String) -> Bool {
|
|
53
|
+
let voice = AVSpeechSynthesisVoice(language: lang)
|
|
54
|
+
return voice != nil
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Adjust rate for a closer match to other platform.
|
|
58
|
+
@objc private func adjustRate(_ rate: Float) -> Float {
|
|
59
|
+
let baseRate = AVSpeechUtteranceDefaultSpeechRate
|
|
60
|
+
if rate == 1 {
|
|
61
|
+
return baseRate
|
|
62
|
+
}
|
|
63
|
+
if rate > baseRate {
|
|
64
|
+
return baseRate + (rate * 0.025)
|
|
65
|
+
}
|
|
66
|
+
return rate / 2
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@objc private func resolveCurrentCall() {
|
|
70
|
+
do {
|
|
71
|
+
try AVAudioSession.sharedInstance().setActive(false)
|
|
72
|
+
} catch {
|
|
73
|
+
CAPLog.print(error.localizedDescription)
|
|
74
|
+
}
|
|
75
|
+
guard let call = calls.first else {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
call.resolve()
|
|
79
|
+
calls.removeFirst()
|
|
80
|
+
}
|
|
81
|
+
}
|
|
File without changes
|
|
@@ -3,12 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
// Define the plugin using the CAP_PLUGIN Macro, and
|
|
5
5
|
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
|
|
6
|
-
CAP_PLUGIN(
|
|
6
|
+
CAP_PLUGIN(TextToSpeechPlugin, "TextToSpeech",
|
|
7
7
|
CAP_PLUGIN_METHOD(speak, CAPPluginReturnPromise);
|
|
8
8
|
CAP_PLUGIN_METHOD(stop, CAPPluginReturnPromise);
|
|
9
|
-
CAP_PLUGIN_METHOD(setSpeechRate, CAPPluginReturnPromise);
|
|
10
|
-
CAP_PLUGIN_METHOD(setPitchRate, CAPPluginReturnPromise);
|
|
11
9
|
CAP_PLUGIN_METHOD(openInstall, CAPPluginReturnPromise);
|
|
12
10
|
CAP_PLUGIN_METHOD(getSupportedLanguages, CAPPluginReturnPromise);
|
|
13
11
|
CAP_PLUGIN_METHOD(getSupportedVoices, CAPPluginReturnPromise);
|
|
12
|
+
CAP_PLUGIN_METHOD(isLanguageSupported, CAPPluginReturnPromise);
|
|
14
13
|
)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Please read the Capacitor iOS Plugin Development Guide
|
|
6
|
+
* here: https://capacitorjs.com/docs/plugins/ios
|
|
7
|
+
*/
|
|
8
|
+
@objc(TextToSpeechPlugin)
|
|
9
|
+
public class TextToSpeechPlugin: CAPPlugin {
|
|
10
|
+
private static let errorUnsupportedLanguage = "This language is not supported."
|
|
11
|
+
|
|
12
|
+
private let implementation = TextToSpeech()
|
|
13
|
+
|
|
14
|
+
@objc public func speak(_ call: CAPPluginCall) {
|
|
15
|
+
let text = call.getString("text") ?? ""
|
|
16
|
+
let lang = call.getString("lang") ?? "en-US"
|
|
17
|
+
let rate = call.getFloat("rate") ?? 1.0
|
|
18
|
+
let pitch = call.getFloat("pitch") ?? 1.0
|
|
19
|
+
let volume = call.getFloat("volume") ?? 1.0
|
|
20
|
+
let category = call.getString("category") ?? "ambient"
|
|
21
|
+
|
|
22
|
+
let isLanguageSupported = implementation.isLanguageSupported(lang)
|
|
23
|
+
guard isLanguageSupported else {
|
|
24
|
+
call.reject(TextToSpeechPlugin.errorUnsupportedLanguage)
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
do {
|
|
29
|
+
try implementation.speak(text, lang, rate, pitch, category, volume, call)
|
|
30
|
+
} catch {
|
|
31
|
+
call.reject(error.localizedDescription)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@objc public func stop(_ call: CAPPluginCall) {
|
|
36
|
+
implementation.stop()
|
|
37
|
+
call.resolve()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@objc public func openInstall(_ call: CAPPluginCall) {
|
|
41
|
+
call.resolve()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@objc func getSupportedLanguages(_ call: CAPPluginCall) {
|
|
45
|
+
let languages = self.implementation.getSupportedLanguages()
|
|
46
|
+
call.resolve([
|
|
47
|
+
"languages": languages
|
|
48
|
+
])
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@objc func getSupportedVoices(_ call: CAPPluginCall) {
|
|
52
|
+
call.resolve([
|
|
53
|
+
"voices": []
|
|
54
|
+
])
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@objc func isLanguageSupported(_ call: CAPPluginCall) {
|
|
58
|
+
let lang = call.getString("lang") ?? ""
|
|
59
|
+
let isLanguageSupported = self.implementation.isLanguageSupported(lang)
|
|
60
|
+
call.resolve([
|
|
61
|
+
"supported": isLanguageSupported
|
|
62
|
+
])
|
|
63
|
+
}
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capacitor-community/text-to-speech",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Capacitor plugin for synthesizing speech from text.",
|
|
5
|
-
"main": "dist/plugin.js",
|
|
5
|
+
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
7
7
|
"types": "dist/esm/index.d.ts",
|
|
8
|
+
"unpkg": "dist/plugin.js",
|
|
8
9
|
"scripts": {
|
|
9
|
-
"
|
|
10
|
+
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
|
|
11
|
+
"verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin && cd ..",
|
|
12
|
+
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
|
13
|
+
"verify:web": "npm run build",
|
|
14
|
+
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
|
|
15
|
+
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
|
|
16
|
+
"eslint": "eslint . --ext ts",
|
|
10
17
|
"prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
|
|
11
18
|
"swiftlint": "node-swiftlint",
|
|
12
|
-
"
|
|
19
|
+
"docgen": "docgen --api TextToSpeechPlugin --output-readme README.md --output-json dist/docs.json",
|
|
20
|
+
"build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.js",
|
|
13
21
|
"clean": "rimraf ./dist",
|
|
14
22
|
"watch": "tsc --watch",
|
|
15
23
|
"prepublishOnly": "npm run build",
|
|
@@ -18,21 +26,25 @@
|
|
|
18
26
|
"author": "Robin Genz <mail@robingenz.dev>",
|
|
19
27
|
"license": "MIT",
|
|
20
28
|
"devDependencies": {
|
|
21
|
-
"@capacitor/android": "
|
|
22
|
-
"@capacitor/
|
|
23
|
-
"@capacitor/
|
|
29
|
+
"@capacitor/android": "3.4.0",
|
|
30
|
+
"@capacitor/cli": "3.4.0",
|
|
31
|
+
"@capacitor/core": "3.4.0",
|
|
32
|
+
"@capacitor/docgen": "0.0.18",
|
|
33
|
+
"@capacitor/ios": "3.4.0",
|
|
34
|
+
"@ionic/eslint-config": "0.3.0",
|
|
24
35
|
"@ionic/prettier-config": "1.0.1",
|
|
25
36
|
"@ionic/swiftlint-config": "1.1.2",
|
|
26
|
-
"
|
|
27
|
-
"prettier
|
|
37
|
+
"eslint": "7.11.0",
|
|
38
|
+
"prettier": "2.2.0",
|
|
39
|
+
"prettier-plugin-java": "1.0.0",
|
|
28
40
|
"rimraf": "3.0.2",
|
|
29
|
-
"rollup": "2.
|
|
41
|
+
"rollup": "2.32.0",
|
|
30
42
|
"standard-version": "9.1.0",
|
|
31
43
|
"swiftlint": "1.0.1",
|
|
32
|
-
"typescript": "
|
|
44
|
+
"typescript": "4.0.3"
|
|
33
45
|
},
|
|
34
46
|
"peerDependencies": {
|
|
35
|
-
"@capacitor/core": "^
|
|
47
|
+
"@capacitor/core": "^3.0.0"
|
|
36
48
|
},
|
|
37
49
|
"files": [
|
|
38
50
|
"android/src/main/",
|
|
@@ -41,11 +53,23 @@
|
|
|
41
53
|
"ios/Plugin/",
|
|
42
54
|
"CapacitorCommunityTextToSpeech.podspec"
|
|
43
55
|
],
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "git+https://github.com/capacitor-community/text-to-speech.git"
|
|
59
|
+
},
|
|
60
|
+
"bugs": {
|
|
61
|
+
"url": "https://github.com/capacitor-community/text-to-speech/issues"
|
|
62
|
+
},
|
|
44
63
|
"keywords": [
|
|
45
64
|
"capacitor",
|
|
46
65
|
"plugin",
|
|
47
66
|
"native"
|
|
48
67
|
],
|
|
68
|
+
"prettier": "@ionic/prettier-config",
|
|
69
|
+
"swiftlint": "@ionic/swiftlint-config",
|
|
70
|
+
"eslintConfig": {
|
|
71
|
+
"extends": "@ionic/eslint-config/recommended"
|
|
72
|
+
},
|
|
49
73
|
"capacitor": {
|
|
50
74
|
"ios": {
|
|
51
75
|
"src": "ios"
|
|
@@ -53,14 +77,5 @@
|
|
|
53
77
|
"android": {
|
|
54
78
|
"src": "android"
|
|
55
79
|
}
|
|
56
|
-
},
|
|
57
|
-
"prettier": "@ionic/prettier-config",
|
|
58
|
-
"swiftlint": "@ionic/swiftlint-config",
|
|
59
|
-
"repository": {
|
|
60
|
-
"type": "git",
|
|
61
|
-
"url": "https://github.com/capacitor-community/text-to-speech.git"
|
|
62
|
-
},
|
|
63
|
-
"bugs": {
|
|
64
|
-
"url": "https://github.com/capacitor-community/text-to-speech/issues"
|
|
65
80
|
}
|
|
66
81
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
-
|
|
5
|
-
### [0.2.3](https://github.com/capacitor-community/text-to-speech/compare/v0.2.2...v0.2.3) (2021-03-12)
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
### Bug Fixes
|
|
9
|
-
|
|
10
|
-
* **ios:** pod failed to validate ([#46](https://github.com/capacitor-community/text-to-speech/issues/46)) ([6a83100](https://github.com/capacitor-community/text-to-speech/commit/6a831003d3c29f9fa6a46dc27e20267246b3ec1a))
|
|
11
|
-
|
|
12
|
-
### [0.2.2](https://github.com/capacitor-community/text-to-speech/compare/v0.2.1...v0.2.2) (2021-03-11)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
### Bug Fixes
|
|
16
|
-
|
|
17
|
-
* **android:** `speechRate` and `pitchRate` are ignored ([#43](https://github.com/capacitor-community/text-to-speech/issues/43)) ([153a500](https://github.com/capacitor-community/text-to-speech/commit/153a500aef2245de61885ce282f7d5111f28b803))
|
|
18
|
-
* **android:** get supported languages ([#29](https://github.com/capacitor-community/text-to-speech/issues/29)) ([bf477aa](https://github.com/capacitor-community/text-to-speech/commit/bf477aab9f713413e8b418d809de26a0482524b0))
|
|
19
|
-
* **android:** get supported voices ([#31](https://github.com/capacitor-community/text-to-speech/issues/31)) ([0870389](https://github.com/capacitor-community/text-to-speech/commit/087038989a6ba77bcce14506b89172046f754ee7))
|
|
20
|
-
* **ios:** not working in background ([#35](https://github.com/capacitor-community/text-to-speech/issues/35)) ([63108ab](https://github.com/capacitor-community/text-to-speech/commit/63108abb6b35ffabb5d04ef9a720267ddad2f33b))
|
|
21
|
-
* **ios:** speech rate adjusted to other platforms ([#36](https://github.com/capacitor-community/text-to-speech/issues/36)) ([d33dceb](https://github.com/capacitor-community/text-to-speech/commit/d33dceb2ed132616a1aaa7b40177ea1d7c6321c3))
|
|
22
|
-
* **web:** stop speaking on a new call ([#44](https://github.com/capacitor-community/text-to-speech/issues/44)) ([6b1b83e](https://github.com/capacitor-community/text-to-speech/commit/6b1b83e28191b1882bc50f2e103f766c4e5182df))
|
|
23
|
-
* different behavior with blank `text` ([#39](https://github.com/capacitor-community/text-to-speech/issues/39)) ([527a51f](https://github.com/capacitor-community/text-to-speech/commit/527a51f7cf6cbc4debec5f239f4479488554d494))
|
|
24
|
-
* publish only necessary files ([#23](https://github.com/capacitor-community/text-to-speech/issues/23)) ([359f2d2](https://github.com/capacitor-community/text-to-speech/commit/359f2d203abff1890369bd10a31668ea202a5ae3))
|
|
25
|
-
|
|
26
|
-
## [0.2.0](https://github.com/capacitor-community/text-to-speech/compare/v0.1.3...v0.2.0) (2020-06-30)
|
|
27
|
-
|
|
28
|
-
### Bug Fixes
|
|
29
|
-
|
|
30
|
-
- Merge pull request #3 from MarnickvdA/master [f84aef3](https://github.com/capacitor-community/text-to-speech/commit/f84aef3f25ebfa402b5b0de7006fe7fda7f2e47b)
|
|
31
|
-
|
|
32
|
-
- Fix for issue #2 'Cant run on iOS'. Changed name of the PodSpec file. Also added JSON parser for the package.json file so you won't have to update the plugin information in two places ;) [dbc97c3](https://github.com/capacitor-community/text-to-speech/commit/dbc97c3e8c44e62b1cff9b3cf3b40d1141c58915)
|
|
33
|
-
|
|
34
|
-
### Features
|
|
35
|
-
|
|
36
|
-
### Docs
|
|
37
|
-
|
|
38
|
-
- docs: update header and fix minor typo to speak method [99861e6](https://github.com/capacitor-community/text-to-speech/commit/99861e6b41609369db978060e398524dd04f4530)
|
|
39
|
-
|
|
40
|
-
- docs: rename `rate` to `speechRate` and `pitch` to `pitchRate` [99861e6](https://github.com/capacitor-community/text-to-speech/commit/99861e6b41609369db978060e398524dd04f4530)
|
|
41
|
-
|
|
42
|
-
### Chores
|
|
43
|
-
|
|
44
|
-
- chore: add changelog [b83f97a](https://github.com/capacitor-community/text-to-speech/commit/b83f97aef2413b174d0cc4b423ab9cf54ab9d4fd)
|
|
45
|
-
- chore(ios): add volume option [eef1b00](https://github.com/capacitor-community/text-to-speech/commit/eef1b00a54c3e1570ed6a52bc83b556d4c9930ea)
|
|
46
|
-
- chore: add homepage property to package [2aad4f4](https://github.com/capacitor-community/text-to-speech/commit/2aad4f43d47fe9d5f776b9ea672f34ac08d81762)
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
package com.getcapacitor.community.tts;
|
|
2
|
-
|
|
3
|
-
public class Constant {
|
|
4
|
-
|
|
5
|
-
public static final String ERROR_TTS_NOT_INITIALIZED = "TextToSpeech is not initialized";
|
|
6
|
-
public static final String ERROR_UTTERANCE = "Failed to read text";
|
|
7
|
-
public static final String ERROR_UNSUPPORTED_LOCALE = "Unsupported locale";
|
|
8
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
-
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
3
|
-
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|
4
|
-
xmlns:tools="http://schemas.android.com/tools"
|
|
5
|
-
android:layout_width="match_parent"
|
|
6
|
-
android:layout_height="match_parent"
|
|
7
|
-
tools:context="com.getcapacitor.BridgeActivity"
|
|
8
|
-
>
|
|
9
|
-
|
|
10
|
-
<WebView
|
|
11
|
-
android:id="@+id/webview"
|
|
12
|
-
android:layout_width="fill_parent"
|
|
13
|
-
android:layout_height="fill_parent" />
|
|
14
|
-
|
|
15
|
-
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|