问题描述
我正在使用react-native-community / voice使用语音来发短信。对于android,它工作正常,但对于iOS,它没有给出适当的结果。我也尝试使用钩子,但是得到了相同的结果。
我使用了5秒的计时器来处理销毁声音的方法(这在我的iOS项目中也不起作用)。
我正在使用
react-native-cli: 2.0.1
react-native: 0.59.8
有人可以在这方面帮助我吗?
先谢谢了。 这是我的代码:-
state = {
recognized: '',pitch: '',error: '',end: '',started: '',results: [],partialResults: [],dataSource: [],isNet:false,isLoading : false,};
constructor(props: Props) {
super(props);
Voice.onSpeechStart = this.onSpeechStart;
Voice.onSpeechRecognized = this.onSpeechRecognized;
Voice.onSpeechEnd = this.onSpeechEnd;
Voice.onSpeechError = this.onSpeechError;
Voice.onSpeechResults = this.onSpeechResults;
Voice.onSpeechPartialResults = this.onSpeechPartialResults;
Voice.onSpeechVolumeChanged = this.onSpeechVolumeChanged;
}
componentwillUnmount() {
Voice.destroy().then(Voice.removeAllListeners);
}
onSpeechStart = (e: any) => {
this.setState({
started: '',});
};
onSpeechRecognized = (e: SpeechRecognizedEvent) => {
this.setState({
recognized: '',});
};
onSpeechEnd = (e: any) => {
this.setState({
end: '',});
};
onSpeechError = (e: SpeechErrorEvent) => {
this.setState({
error: JSON.stringify(e.error),});
};
onSpeechResults = (e: SpeechResultsEvent) => {
this.setState({
results: e.value,});
// My Own Method this.getSearchResult(e.value[0])
};
onSpeechPartialResults = (e: SpeechResultsEvent) => {
this.setState({
partialResults: e.value,});
};
onSpeechVolumeChanged = (e: any) => {
this.setState({
pitch: e.value,});
};
_startRecognizing = async () => {
this.setState({
recognized: '',end: 'Listning ...',});
try {
Platform.OS === 'ios'
{
setTimeout(() => {
this._destroyRecognizer()
},5000)
}
await Voice.start('en-US');
} catch (e) {
console.error(e);
}
};
_stopRecognizing = async () => {
try {
await Voice.stop();
} catch (e) {
console.error(e);
}
};
_cancelRecognizing = async () => {
try {
await Voice.cancel();
} catch (e) {
console.error(e);
}
};
_destroyRecognizer = async () => {
try {
await Voice.destroy();
} catch (e) {
console.error(e);
}
this.setState({
recognized: '',});
};
任何帮助都会感激
解决方法
我有类似的问题,我只是通过此方法解决
// for ios
onSpeechEnd(e) {
this.setState({
end: true
});
if (Platform.OS === 'ios') {
// use this.state.results here
}
}
// for android
onSpeechResults(e) {
this.setState({
results: e.value
});
if (Platform.OS === 'android') {
use this.state.results here
}
}