简体   繁体   English

文本字段的反应语音识别

[英]react-speech-recognition for text-field

Im trying to make react-speech-recognition write inside a text-field but for some reason it wont work.我试图让 react-speech-recognition 在文本字段中写入,但由于某种原因它不起作用。 I think it has to do with the last line.我认为这与最后一行有关。


import React from 'react';

import Button from '@material-ui/core/Button';
import Checkbox from '@material-ui/core/Checkbox';
import CircularProgress from '@material-ui/core/CircularProgress';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Grid from '@material-ui/core/Grid';

import TextField from '@material-ui/core/TextField';
import Paper from '@material-ui/core/Paper';
import InputLabel from '@material-ui/core/InputLabel';
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition'

// import EventIcon from '@material-ui/icons/Event';
import SearchIcon from '@material-ui/icons/Search';
// import TodayIcon from '@material-ui/icons/Today';

import InputUnstyled from '@mui/base/InputUnstyled';

const Dictaphone = () => {
    const {
        transcript,
        listening,
        resetTranscript,
        browserSupportsSpeechRecognition
    } = useSpeechRecognition();

    if (!browserSupportsSpeechRecognition) {
        return <span>Browser doesn't support speech recognition.</span>;
    }

    return (
        <div>
            <p>Microphone: {listening ? 'on' : 'off'}</p>
            <button onClick={SpeechRecognition.startListening}>Start</button>
            <button onClick={SpeechRecognition.stopListening}>Stop</button>
            <button onClick={resetTranscript}>Reset</button>
            <p>{transcript}</p>
        </div>
    );
};

function updateInput(ish) {
    document.setSearchField = "Rendering";
}

function SearchForm({ runSearch, setSearchField, setAuthorField, setVenue, setBeginDate, setEndDate, setShowGraph, show_loading }) {
    const {
        transcript,
        listening,
        resetTranscript,
        browserSupportsSpeechRecognition
    } = useSpeechRecognition();

    return (

        <Grid container spacing={2}>
            <Grid item xs={1} />
            <Grid item xs={10}>
                <Paper elevation={2}>
                    <div className="search-body">
                        <form onSubmit={runSearch}>
                            <Grid container spacing={2}>
                                <Grid item xs={12}>

                                    <div>
                                        <p>Microphone: {listening ? 'on' : 'off'}</p>
                                        <button onClick={SpeechRecognition.startListening}>Start</button>
                                        <button onClick={SpeechRecognition.stopListening}>Stop</button>
                                        <button onClick={resetTranscript}>Reset</button>
                                        <p>{transcript}</p>
                                    </div>

                                    <TextField
                                        id="busca" label="Palavras-chave *"
                                        type='text'
                                        placeholder={transcript}
                                        name='q'
                                        variant="outlined"
                                        fullWidth
                                        InputProps={{
                                            endAdornment: <SearchIcon />
                                        }}
                                        InputLabelProps={{
                                            shrink: true

                                        }}
                                        onChange={setSearchField} />

I tried using value={transcript} in the text-field but it only will work after i write something after it.我尝试在文本字段中使用value={transcript}但它只有在我在它之后写一些东西之后才会起作用。 Example: i say "hello", it will write it in the text-field but it wont actually send "hello" until i write something with the keyboard after like "1".示例:我说“你好”,它会写在文本字段中,但它实际上不会发送“你好”,直到我在“1”之后用键盘写一些东西。 then it will send "hello1".然后它将发送“hello1”。

<TextField
    id="busca" label="Palavras-chave *"
    type='text'
    value={transcript}
    placeholder={transcript}
    name='q'
    variant="outlined"
    fullWidth
    InputProps={{
        endAdornment: <SearchIcon />
    }}
    InputLabelProps={{
        shrink: true

    }}
    onChange={setSearchField} />

I think it might work if you replace TextField with textarea including value={transcript} for the same.我认为如果将 TextField 替换为包含 value={transcript} 的 textarea 可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM