Skip to content

Commit 4f06e19

Browse files
author
bieshan
committed
add: tricky logic for android chrome :neckbeard:
test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard: test: just test :neckbeard:
1 parent 902adc9 commit 4f06e19

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

pages/index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Home = () => {
1919
const [detecting, setDetecting] = useState(false); // 音声認識ステータス
2020
const [finalText, setFinalText] = useState(""); // 確定された文章
2121
const [transcript, setTranscript] = useState("ボタンを押して検知開始"); // 認識中の文章
22+
const [android, setAndroid] = useState(false); // Android chrome用のフラグ
2223
// 単語検知
2324
const initialTagValues = ["年収"]; // デフォルト検知単語
2425
const candidates = ["年収", "自由", "成功"]; // 検知単語候補
@@ -35,28 +36,37 @@ const Home = () => {
3536
alert("お使いのブラウザには未対応です");
3637
return;
3738
}
39+
40+
// Androidのためのプラグ
41+
if (/Android/i.test(navigator.userAgent)) {
42+
setAndroid(true);
43+
};
44+
3845
// NOTE: 将来的にwebkit prefixが取れる可能性があるため
3946
const SpeechRecognition =
4047
window.SpeechRecognition || window.webkitSpeechRecognition;
4148
recognizerRef.current = new SpeechRecognition();
4249
recognizerRef.current.lang = "ja-JP";
4350
recognizerRef.current.interimResults = true;
4451
recognizerRef.current.continuous = true;
45-
recognizerRef.current.onstart = () => {
46-
setDetecting(true);
47-
};
4852
recognizerRef.current.onend = () => {
49-
setTranscript("");
50-
setDetecting(false);
53+
if (detecting) {
54+
recognizerRef.current.start();
55+
};
5156
};
5257
recognizerRef.current.onresult = event => {
5358
[...event.results].slice(event.resultIndex).forEach(result => {
5459
const transcript = result[0].transcript;
5560
if (result.isFinal) {
5661
// 音声認識が完了して文章が確定
5762
setFinalText(prevState => {
63+
if (android) {
64+
// Android chromeなら値をそのまま返す
65+
return transcript;
66+
};
5867
return prevState + transcript;
5968
});
69+
// 文章確定したら候補を削除
6070
setTranscript("");
6171
return;
6272
}
@@ -144,6 +154,7 @@ const Home = () => {
144154
color="primary"
145155
size="large"
146156
onClick={() => {
157+
setDetecting(true);
147158
recognizerRef.current.start();
148159
}}
149160
>
@@ -158,7 +169,8 @@ const Home = () => {
158169
color="secondary"
159170
size="large"
160171
onClick={() => {
161-
recognizerRef.current.onend();
172+
setDetecting(false);
173+
recognizerRef.current.stop();
162174
}}
163175
>
164176
{detecting ? "検知停止" : "検知待ち"}

0 commit comments

Comments
 (0)