This repository was archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextAnswerInput.jsx
166 lines (154 loc) · 4.36 KB
/
TextAnswerInput.jsx
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* eslint-disable react/void-dom-elements-no-children */
import React, { useState } from 'react';
import { Link } from "react-router-dom";
import { ReactMediaRecorder, useReactMediaRecorder } from "react-media-recorder";
import TextField from '@mui/material/TextField';
import Button from '@mui/material/Button';
import IconButton from '@mui/material/IconButton';
import VideocamIcon from '@mui/icons-material/Videocam';
import MicIcon from '@mui/icons-material/Mic';
import './styles/TextAnswerInput.css';
export default function TextAnswerInput(props) {
const [audioSelected, setAudioSelected] = useState(false);
const [videoSelected, setVideoSelected] = useState(false);
const clickMic = (e) => {
if (videoSelected) {
setVideoSelected(false);
}
props.clearRecordings(props.id);
setAudioSelected(!audioSelected);
};
const clickVideocam = (e) => {
if (audioSelected) {
setAudioSelected(false);
}
props.clearRecordings(props.id);
setVideoSelected(!videoSelected);
};
function getBase64(file) {
return new Promise((resolve) => {
let fileInfo;
let baseURL = "";
// Make new FileReader
const reader = new FileReader();
// Convert the file to base64 text
reader.readAsDataURL(file);
// on reader load somthing...
reader.onload = () => {
// Make a fileInfo Object
console.log("Called", reader);
baseURL = reader.result;
console.log(baseURL);
resolve(baseURL);
};
console.log(fileInfo);
});
}
const stopMic = (blobUrl, blob) => {
console.log(blobUrl);
console.log(blob);
const audioFile = new File([blob], 'voice.wav', { type: 'audio/wav' });
console.log(audioFile);
getBase64(audioFile)
.then((result) => {
props.saveAudio(props.id, result);
})
.catch((err) => {
console.log(err);
});
};
const stopVideo = (blobUrl, blob) => {
console.log(blobUrl);
console.log(blob);
const videoFile = new File([blob], 'video.mp4', { type: 'video/mp4' });
console.log(videoFile);
getBase64(videoFile)
.then((result) => {
props.saveVideo(props.id, result);
})
.catch((err) => {
console.log(err);
});
};
return (
<div className="text-answer-input">
<div className="user-question-field">
<TextField
id="standard-read-only-input"
defaultValue={props.question}
InputProps={{
readOnly: true,
}}
variant="standard"
fullWidth
/>
</div>
<div>
<TextField
id={props.id}
onChange={props.onChange}
placeholder="Answer"
multiline
fullWidth
/>
</div>
{props.allowRecording
&& (
<div>
<IconButton aria-label="audio" onClick={clickMic} value={props.id}>
<MicIcon />
</IconButton>
<IconButton aria-label="audio" onClick={clickVideocam} value={props.id}>
<VideocamIcon />
</IconButton>
</div>
)}
{audioSelected
&& (
<div>
<ReactMediaRecorder
audio
onStop={stopMic}
render={({
status, startRecording, stopRecording, mediaBlobUrl,
}) => (
<div>
<p>{status}</p>
<audio src={mediaBlobUrl} controls autoPlay>
<track kind="captions" />
</audio>
<div>
<button onClick={startRecording}>Start Recording</button>
<button onClick={stopRecording}>Stop Recording</button>
</div>
</div>
)}
/>
</div>
)}
{videoSelected
&& (
<div>
<ReactMediaRecorder
video
onStop={stopVideo}
render={({
status, startRecording, stopRecording, mediaBlobUrl,
}) => (
<div>
<p>{status}</p>
<video src={mediaBlobUrl} width={320} height={180} controls autoPlay>
<track kind="captions" />
</video>
<div>
<button onClick={startRecording}>Start Recording</button>
<button onClick={stopRecording}>Stop Recording</button>
</div>
</div>
)}
/>
</div>
)}
</div>
);
}