使用 React Native 社区语音功能将语音转换为文本时出现错误

问题描述 投票:0回答:1

我正在尝试构建一个具有语音转文本功能的人工智能助手应用程序。我正在使用本机社区/语音包来实现此功能,但当我尝试捕获用户的声音时,它显示此错误 -

error - [TypeError: Cannot read property 'startSpeech' of null]

这是我的主屏幕,我正在其中使用 React Native 社区语音 -

import Voice from '@react-native-community/voice';
import * as Permissions from 'expo-permissions';

export default function HomeScreen() {
  const [message,setMessage]=useState(dummyMessages);
  const [recording,setRecording]=useState(false);
  const [speaking,setSpeaking]=useState(false);

  useEffect(() => {
    checkMicrophonePermission();
  }, []);

  const checkMicrophonePermission = async () => {
    try {
      const { status } = await Permissions.askAsync(Permissions.AUDIO_RECORDING);
      
      if (status === 'granted') {
        console.log('Microphone permission granted');
        // Now you can proceed with using the microphone
      } else {
        console.log('Microphone permission denied');
        // Handle denial or show a message to the user
      }
    } catch (error) {
      console.error('Error checking microphone permission:', error);
    }
  };
  
  const speechStartHandler = e =>{
    console.log('Speech start handler');
  }

  const speechEndHandler = e =>{
    setRecording(false);
    console.log('Speech End handler');
  }
  const speechResultHandler = e =>{
    console.log('voice enent:' ,e);
  }
  const speechErrorHandler = e =>{
    console.log('Speech error handler :' ,e);
  }

  const startRecording=async()=>{
    setRecording(true)
    try{
      await Voice.start('en-GB');
    }catch(error){
      console.log('error -', error);
    }
  }

  const stopRecording=async()=>{
    try{
      await Voice.stop();
      setRecording(false);
      //fetch result
    }catch(error){
      console.log('error', error);
    }
  }

  const clear=()=>{
    setMessage([])
  }

  const stopSpeaking=()=>{
    setSpeaking(false);
  }

  useEffect(()=>{
    // voice handler events
    Voice.onSpeechStart = speechStartHandler;
    Voice.onSpeechEnd = speechEndHandler;
    Voice.onSpeechResults = speechResultHandler;
    Voice.onSpeechError= speechErrorHandler;

    return ()=>{
      //destroy the voice instance 
      Voice.destroy().then(Voice.removeAllListeners);
    }
   },[])

  return (
        {/* features || message */}
      {/* recording button, clear history button and stop speaking button */}
    <View style={styles.BtnContainer}>
    {/* start recording */}
      {
        recording? (
          <View style={[styles.recBtn, {backgroundColor:"lightpink", padding:8}]}>
        <TouchableOpacity 
        style={[styles.recBtn, {backgroundColor:"red"}]}
        onPress={stopRecording}>
          <Image style={styles.recBtnImg} source={require('../../assets/mic.png')}/>
        </TouchableOpacity>
      </View>
        ):(
          <View style={[styles.recBtn, {backgroundColor:"#cceee1", padding:8}]}>
        <TouchableOpacity style={styles.recBtn} onPress={startRecording}>
          <Image style={styles.recBtnImg} source={require('../../assets/mic.png')}/>
        </TouchableOpacity>
      </View>
        )

{/* clear history */}
 

我尝试了所有方法,但当我尝试录制语音时仍然出现此错误

react-native expo speech-to-text react-native-voice
1个回答
0
投票

像这样在 App.js 上导入字体

import AppLoading from 'expo-app-loading';
import { useFonts } from 'expo-font';
let customFonts = {
'raleway':require('./assets/Fonts/Raleway-Regular.ttf')
};

const App = () => {
const [isLoaded] = useFonts(customFonts);
if (!isLoaded) {
return <AppLoading />;
}
return <RootApp />;
}
© www.soinside.com 2019 - 2024. All rights reserved.