-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
153 lines (139 loc) · 3.8 KB
/
App.tsx
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Copyright 2020 Picovoice Inc.
//
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
import React, { Component } from 'react';
import { Button, PermissionsAndroid, Platform } from 'react-native';
import {
StyleSheet,
View,
EventSubscription,
NativeEventEmitter,
} from 'react-native';
import { VoiceProcessor, BufferEmitter } from 'react-native-voice-processor';
type Props = {};
type State = {
isListening: boolean;
buttonText: string;
buttonDisabled: boolean;
};
export default class App extends Component<Props, State> {
_bufferListener?: EventSubscription;
_bufferEmitter: NativeEventEmitter;
constructor(props: Props) {
super(props);
this.state = {
buttonText: 'Start',
isListening: false,
buttonDisabled: false,
};
this._bufferEmitter = new NativeEventEmitter(BufferEmitter);
this._bufferListener = this._bufferEmitter.addListener(
BufferEmitter.BUFFER_EMITTER_KEY,
async (buffer: number[]) => {
console.log(`Buffer of size ${buffer.length} received!`);
}
);
}
componentDidMount() {}
_startProcessing() {
let recordAudioRequest;
if (Platform.OS == 'android') {
recordAudioRequest = this._requestRecordAudioPermission();
} else {
recordAudioRequest = new Promise(function (resolve, _) {
resolve(true);
});
}
recordAudioRequest.then((hasPermission) => {
if (!hasPermission) {
console.error('Did not grant required microphone permission.');
return;
}
VoiceProcessor.getVoiceProcessor(512, 16000)
.start()
.then((didStart) => {
if (didStart) {
this.setState({
isListening: true,
buttonText: 'Stop',
buttonDisabled: false,
});
}
});
});
}
_stopProcessing() {
VoiceProcessor.getVoiceProcessor(512, 16000)
.stop()
.then((didStop) => {
if (didStop) {
this.setState({
isListening: false,
buttonText: 'Start',
buttonDisabled: false,
});
}
});
}
_toggleProcessing() {
this.setState({
buttonDisabled: true,
});
if (this.state.isListening) {
this._stopProcessing();
} else {
this._startProcessing();
}
}
async _requestRecordAudioPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
{
title: 'Microphone Permission',
message:
'VoiceProcessorExample needs your permission to receive audio buffers.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true;
} else {
return false;
}
} catch (err) {
console.error(err);
return false;
}
}
render() {
return (
<View style={styles.container}>
<View style={{ margin: 5 }}>
<Button
title={this.state.buttonText}
disabled={this.state.buttonDisabled}
onPress={() => this._toggleProcessing()}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
});