博览会中的reactNative/voice错误



这是我的代码

import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, Button, View } from 'react-native';
import { useEffect, useState } from 'react';
import Voice from '@react-native-voice/voice';
export default function App() {
let [started, setStarted] = useState(false);
let [results, setResults] = useState([]);
useEffect(() => {
Voice.onSpeechError = onSpeechError;
Voice.onSpeechResults = onSpeechResults;
return () => {
Voice.destroy().then(Voice.removeAllListeners);
}
}, []);

const startSpeechToText = async () => {
await Voice.start();
setStarted(true);
};
const stopSpeechToText = async () => {
await Voice.stop();
setStarted(false);
};
const onSpeechResults = (result) => {
setResults(result.value);
};
const onSpeechError = (error) => {
console.log(error);
};
return (
<View style={styles.container}>
{!started ? <Button title='Start Speech to Text' onPress={startSpeechToText} /> : undefined}
{started ? <Button title='Stop Speech to Text' onPress={stopSpeechToText} /> : undefined}
{results.map((result, index) => <Text key={index}>{result}</Text>)}
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});

当我运行它时,我一直得到这个错误

[未处理的承诺拒绝:TypeError: null不是一个对象(评估'Voice.startSpeech')]

我试着问chatGPT,但他不回答

Packages和import是正确的,所以我不知道错误是什么,也不知道如何修复它

检查它是否适用于其他版本的android,我启动了一些模拟器,似乎在android 12以下有权限问题。在Android 12及以上,我的代码与react-native-voice与expo run: Android工作得很好,但我不知道为什么一旦与expo build: Android构建,我仍然有麦克风权限的问题

最新更新