File size: 3,070 Bytes
803d869
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// convomate/src/components/Sections/VoiceInput.jsx
import React, { useState, useEffect } from "react";
import "font-awesome/css/font-awesome.min.css";

const VoiceInput = ({

  onResult,

  language = "en-US",

  buttonStyle = {},

  disabled = false,

}) => {
  const [isListening, setIsListening] = useState(false);
  const [recognition, setRecognition] = useState(null);

  useEffect(() => {
    const SpeechRecognition =
      window.SpeechRecognition || window.webkitSpeechRecognition;
    if (SpeechRecognition) {
      const recognizer = new SpeechRecognition();
      recognizer.continuous = false;
      recognizer.interimResults = false;
      recognizer.lang = language;
      setRecognition(recognizer);

      recognizer.onresult = (event) => {
        const transcript = event.results[0][0].transcript;
        onResult(transcript);
        setIsListening(false);
      };

      recognizer.onerror = (event) => {
        console.error("Voice input error:", event.error);
        setIsListening(false);
      };

      recognizer.onend = () => {
        if (isListening) setIsListening(false);
      };
    }

    return () => {
      if (recognition) {
        recognition.stop();
      }
    };
  }, [language, onResult]);

  const startListening = () => {
    if (!recognition || disabled) return;
    recognition.start();
    setIsListening(true);
  };

  const stopListening = () => {
    if (recognition && isListening) {
      recognition.stop();
      setIsListening(false);
    }
  };

  return (
    <div style={{ position: "relative", display: "inline-block" }}>

      {!isListening ? (

        <button

          type="button"

          onClick={startListening}

          disabled={!recognition || disabled}

          style={{

            backgroundColor: "#007bff",

            border: "none",

            color: "#fff",

            padding: "10px 15px",

            borderRadius: "50%",

            cursor: "pointer",

            ...buttonStyle,

          }}

          title="Start voice input"

        >

          <i className="fa fa-microphone"></i>

        </button>

      ) : (

        <button

          type="button"

          onClick={stopListening}

          style={{

            backgroundColor: "#dc3545",

            border: "none",

            color: "#fff",

            padding: "10px 15px",

            borderRadius: "50%",

            cursor: "pointer",

            ...buttonStyle,

          }}

          title="Stop listening"

        >

          <i className="fa fa-microphone-slash"></i>

        </button>

      )}

      {isListening && (

        <span

          style={{

            position: "absolute",

            top: "-5px",

            right: "-5px",

            width: "10px",

            height: "10px",

            backgroundColor: "red",

            borderRadius: "50%",

            animation: "pulse 1.5s infinite",

          }}

        ></span>

      )}

    </div>
  );
};

export default VoiceInput;