Skip to content

Commit 97eafbf

Browse files
authored
Merge pull request #7 from yoidea/add-stop-button
fix logic for android
2 parents 2f55955 + af17af4 commit 97eafbf

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

pages/index.js

Lines changed: 18 additions & 8 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,6 +36,12 @@ 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;
@@ -47,24 +54,27 @@ const Home = () => {
4754
};
4855
recognizerRef.current.onend = () => {
4956
setDetecting(false);
57+
if (android && !alertOpen) {
58+
recognizerRef.current.start();
59+
}
5060
};
5161
recognizerRef.current.onresult = event => {
5262
[...event.results].slice(event.resultIndex).forEach(result => {
5363
const transcript = result[0].transcript;
64+
setTranscript(transcript);
5465
if (result.isFinal) {
55-
// 音声認識が完了して文章が確定
56-
setFinalText(prevState => {
57-
return prevState + transcript;
58-
});
59-
setTranscript("");
60-
} else {
61-
// 音声認識の途中経過
6266
if (tagValues.some(value => transcript.includes(value))) {
6367
// NOTE: ユーザーが効果音を追加しなければデフォルトを鳴らす
6468
(userMusic || music).play();
6569
setAlertOpen(true);
6670
}
67-
setTranscript(transcript);
71+
// 音声認識が完了して文章が確定
72+
setFinalText(prevState => {
73+
// Android chromeなら値をそのまま返す
74+
return android ? transcript : (prevState + transcript);
75+
});
76+
// 文章確定したら候補を削除
77+
setTranscript("");
6878
}
6979
});
7080
};

0 commit comments

Comments
 (0)