我正在使用 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: '',
pitch: '',
error: '',
started: '',
results: [],
partialResults: [],
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: '',
pitch: '',
error: '',
started: '',
results: [],
partialResults: [],
end: '',
});
};
任何帮助将不胜感激