使用React-Native社区/语音以React-Native进行语音到文本转换
我想将语音转换成文本,并通过语音识别得到文本 我正在使用() 在构建项目并在我的手机上安装使用React-Native社区/语音以React-Native进行语音到文本转换,react-native,react-native-android,speech-to-text,React Native,React Native Android,Speech To Text,我想将语音转换成文本,并通过语音识别得到文本 我正在使用() 在构建项目并在我的手机上安装apk并按下录制按钮后,我得到以下错误: 错误:{“消息”:“5/客户端错误”} 注意:我在AndroidManifest中添加了以下权限: <uses-permission android:name="android.permission.RECORD_AUDIO" /> 这是我的代码(): import React,{Component,useState,useffect}来自'Rea
apk
并按下录制按钮后,我得到以下错误:
错误:{“消息”:“5/客户端错误”}
注意:我在AndroidManifest中添加了以下权限:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
这是我的代码():
import React,{Component,useState,useffect}来自'React';
从“react native”导入{样式表、文本、视图、图像、TouchableHighlight};
从'@react native community/Voice'导入语音;
常量应用=(道具)=>{
常量[voiceState,setVoiceState]=useState({
承认:'',
音高:'',
错误:“”,
结束:“”,
开始:“”,
结果:[],
部分结果:[],
})
useffect(()=>{
Voice.onSpeechStart=onSpeechStart;
Voice.onSpeechRecognized=onSpeechRecognized;
Voice.onSpeechEnd=onSpeechEnd;
Voice.onSpeechError=onSpeechError;
Voice.onSpeechResults=onSpeechResults;
Voice.onSpeechPartialResults=onSpeechPartialResults;
Voice.onSpeechVolumeChanged=onSpeechVolumeChanged;
Voice.destroy().then(Voice.removeAllListeners);
}, [])
const onSpeechStart=(e)=>{
log('onSpeechStart:',e);
setVoiceState({…voiceState,已启动:'√', })
};
const onSpeechRecognized=(e)=>{
log('onSpeechRecognized:',e);
设置语音状态({
…语音状态,已识别:'√',
})
};
速度常数=(e)=>{
log('onSpeechEnd:',e);
设置语音状态({
…语音状态,结束:'√',
})
};
const onSpeechError=(e)=>{
log('onSpeechError:',e);
设置语音状态({
…voiceState,错误:JSON.stringify(e.error)
})
};
const onSpeechResults=(e)=>{
log('onSpeechResults:',e);
设置语音状态({
…语音状态,结果:e.value,
})
};
const onSpeechPartialResults=(e)=>{
log('onSpeechPartialResults:',e);
设置语音状态({
…语音状态,部分结果:e.value,
})
};
常量onSpeechVolumeChanged=(e)=>{
log('onSpeechVolumeChanged:',e);
设置语音状态({
…语音状态,音高:e.value,
})
};
const\u startRecogning=async()=>{
设置语音状态({
…语音状态,
承认:'',
音高:'',
错误:“”,
开始:“”,
结果:[],
部分结果:[],
结束:“”,
})
试一试{
等待声音。开始('en-US');
}捕获(e){
控制台错误(e);
}
};
常量=异步()=>{
试一试{
等待声音。停止();
}捕获(e){
控制台错误(e);
}
};
常量=异步()=>{
试一试{
等待声音。取消();
}捕获(e){
控制台错误(e);
}
};
常量识别器=异步()=>{
试一试{
等待声音。摧毁();
}捕获(e){
控制台错误(e);
}
设置语音状态({
…语音状态,
承认:'',
音高:'',
错误:“”,
开始:“”,
结果:[],
部分结果:[],
结束:“”,
})
};
返回(
欢迎来到我们的母语!
按下按钮,开始讲话。
{`Started:${voiceState.Started}`}
{`认识到:${
语音识别
}`}
{`Pitch:${voiceState.Pitch}`}
{`Error:${voiceState.Error}`}
结果
{voiceState.results.map((结果,索引)=>{
返回(
{result}
);
})}
部分结果
{voiceState.partialResults.map((结果,索引)=>{
返回(
{result}
);
})}
{`End:${voiceState.End}`}
别再认了
取消
毁灭
);
}
import React, { Component, useState, useEffect } from 'react';
import { StyleSheet, Text, View, Image, TouchableHighlight } from 'react-native';
import Voice from '@react-native-community/voice';
const App = (props) => {
const [voiceState, setVoiceState] = useState({
recognized: '',
pitch: '',
error: '',
end: '',
started: '',
results: [],
partialResults: [],
})
useEffect(() => {
Voice.onSpeechStart = onSpeechStart;
Voice.onSpeechRecognized = onSpeechRecognized;
Voice.onSpeechEnd = onSpeechEnd;
Voice.onSpeechError = onSpeechError;
Voice.onSpeechResults = onSpeechResults;
Voice.onSpeechPartialResults = onSpeechPartialResults;
Voice.onSpeechVolumeChanged = onSpeechVolumeChanged;
Voice.destroy().then(Voice.removeAllListeners);
}, [])
const onSpeechStart = (e) => {
console.log('onSpeechStart: ', e);
setVoiceState({ ...voiceState, started: '√', })
};
const onSpeechRecognized = (e) => {
console.log('onSpeechRecognized: ', e);
setVoiceState({
...voiceState, recognized: '√',
})
};
const onSpeechEnd = (e) => {
console.log('onSpeechEnd: ', e);
setVoiceState({
...voiceState, end: '√',
})
};
const onSpeechError = (e) => {
console.log('onSpeechError: ', e);
setVoiceState({
...voiceState, error: JSON.stringify(e.error)
})
};
const onSpeechResults = (e) => {
console.log('onSpeechResults: ', e);
setVoiceState({
...voiceState, results: e.value,
})
};
const onSpeechPartialResults = (e) => {
console.log('onSpeechPartialResults: ', e);
setVoiceState({
...voiceState, partialResults: e.value,
})
};
const onSpeechVolumeChanged = (e) => {
console.log('onSpeechVolumeChanged: ', e);
setVoiceState({
...voiceState, pitch: e.value,
})
};
const _startRecognizing = async () => {
setVoiceState({
...voiceState,
recognized: '',
pitch: '',
error: '',
started: '',
results: [],
partialResults: [],
end: '',
})
try {
await Voice.start('en-US');
} catch (e) {
console.error(e);
}
};
const _stopRecognizing = async () => {
try {
await Voice.stop();
} catch (e) {
console.error(e);
}
};
const _cancelRecognizing = async () => {
try {
await Voice.cancel();
} catch (e) {
console.error(e);
}
};
const _destroyRecognizer = async () => {
try {
await Voice.destroy();
} catch (e) {
console.error(e);
}
setVoiceState({
...voiceState,
recognized: '',
pitch: '',
error: '',
started: '',
results: [],
partialResults: [],
end: '',
})
};
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native Voice!</Text>
<Text style={styles.instructions}>
Press the button and start speaking.
</Text>
<Text style={styles.stat}>{`Started: ${voiceState.started}`}</Text>
<Text style={styles.stat}>{`Recognized: ${
voiceState.recognized
}`}</Text>
<Text style={styles.stat}>{`Pitch: ${voiceState.pitch}`}</Text>
<Text style={styles.stat}>{`Error: ${voiceState.error}`}</Text>
<Text style={styles.stat}>Results</Text>
{voiceState.results.map((result, index) => {
return (
<Text key={`result-${index}`} style={styles.stat}>
{result}
</Text>
);
})}
<Text style={styles.stat}>Partial Results</Text>
{voiceState.partialResults.map((result, index) => {
return (
<Text key={`partial-result-${index}`} style={styles.stat}>
{result}
</Text>
);
})}
<Text style={styles.stat}>{`End: ${voiceState.end}`}</Text>
<TouchableHighlight onPress={_startRecognizing}>
<Image style={styles.button} source={require('../assets/voice-recording.png')} />
</TouchableHighlight>
<TouchableHighlight onPress={_stopRecognizing}>
<Text style={styles.action}>Stop Recognizing</Text>
</TouchableHighlight>
<TouchableHighlight onPress={_cancelRecognizing}>
<Text style={styles.action}>Cancel</Text>
</TouchableHighlight>
<TouchableHighlight onPress={_destroyRecognizer}>
<Text style={styles.action}>Destroy</Text>
</TouchableHighlight>
</View>
);
}