forked from react-native-voice/voice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (103 loc) · 2.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
import React, {
NativeModules,
DeviceEventEmitter,
} from 'react-native';
const { Voice } = NativeModules;
class RCTVoice {
constructor() {
this._loaded = false;
this._listeners = null;
this._events = {
'onSpeechStart': this._onSpeechStart.bind(this),
'onSpeechRecognized': this._onSpeechRecognized.bind(this),
'onSpeechEnd': this._onSpeechEnd.bind(this),
'onSpeechError': this._onSpeechError.bind(this),
'onSpeechResults': this._onSpeechResults.bind(this),
'onSpeechPartialResults': this._onSpeechPartialResults.bind(this),
'onSpeechVolumeChanged': this._onSpeechVolumeChanged.bind(this)
};
}
destroy() {
return Voice.destroySpeech((error) => {
if (error) {
return error;
}
if (this._listeners) {
this._listeners.map((listener, index) => listener.remove());
this._listeners = null;
}
return null;
});
}
start(locale) {
if (!this._loaded && !this._listeners) {
this._listeners = Object.keys(this._events)
.map((key, index) => DeviceEventEmitter.addListener(key, this._events[key]));
}
return Voice.startSpeech(locale, (error) => {
if (error) {
return error;
}
return null;
});
}
stop() {
return Voice.stopSpeech((error) => {
if (error) {
return error;
}
return null;
});
}
cancel() {
return Voice.cancelSpeech((error) => {
if (error) {
return error;
}
return null;
});
}
isAvailable(callback) {
Voice.isSpeechAvailable(callback);
}
isRecognizing() {
return Voice.isRecognizing(isRecognizing => isRecognizing);
}
_onSpeechStart(e) {
if (this.onSpeechStart) {
this.onSpeechStart(e);
}
}
_onSpeechRecognized(e) {
if (this.onSpeechRecognized) {
this.onSpeechRecognized(e);
}
}
_onSpeechEnd(e) {
if (this.onSpeechEnd) {
this.onSpeechEnd(e);
}
}
_onSpeechError(e) {
if (this.onSpeechError) {
this.onSpeechError(e);
}
}
_onSpeechResults(e) {
if (this.onSpeechResults) {
this.onSpeechResults(e);
}
}
_onSpeechPartialResults(e) {
if (this.onSpeechPartialResults) {
this.onSpeechPartialResults(e);
}
}
_onSpeechVolumeChanged(e) {
if (this.onSpeechVolumeChanged) {
this.onSpeechVolumeChanged(e);
}
}
}
module.exports = new RCTVoice();