Skip to content

Commit 4296289

Browse files
author
bieshan
committed
add: stop button and fix logic :neckbeard:
1 parent 2f55955 commit 4296289

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

pages/index.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ const Home = () => {
4242
recognizerRef.current.lang = "ja-JP";
4343
recognizerRef.current.interimResults = true;
4444
recognizerRef.current.continuous = true;
45+
recognizerRef.current.maxAlternatives = 1;
4546
recognizerRef.current.onstart = () => {
4647
setDetecting(true);
4748
};
4849
recognizerRef.current.onend = () => {
50+
setTranscript("");
4951
setDetecting(false);
5052
};
5153
recognizerRef.current.onresult = event => {
@@ -57,15 +59,15 @@ const Home = () => {
5759
return prevState + transcript;
5860
});
5961
setTranscript("");
60-
} else {
61-
// 音声認識の途中経過
62-
if (tagValues.some(value => transcript.includes(value))) {
63-
// NOTE: ユーザーが効果音を追加しなければデフォルトを鳴らす
64-
(userMusic || music).play();
65-
setAlertOpen(true);
66-
}
67-
setTranscript(transcript);
62+
return;
6863
}
64+
// 音声認識の途中経過
65+
if (tagValues.some(value => transcript.includes(value))) {
66+
// NOTE: ユーザーが効果音を追加しなければデフォルトを鳴らす
67+
(userMusic || music).play();
68+
setAlertOpen(true);
69+
}
70+
setTranscript(transcript);
6971
});
7072
};
7173
});
@@ -136,11 +138,11 @@ const Home = () => {
136138
</Grid>
137139
<Box m={2}>
138140
<Grid container alignItems="center" justify="center">
139-
<Grid item>
141+
<Grid item xs={3}>
140142
<Button
141143
variant="outlined"
142144
disabled={detecting}
143-
color="secondary"
145+
color="primary"
144146
size="large"
145147
onClick={() => {
146148
recognizerRef.current.start();
@@ -149,6 +151,20 @@ const Home = () => {
149151
{detecting ? "検知中..." : "検知開始"}
150152
</Button>
151153
</Grid>
154+
<Grid item xs={6}/>
155+
<Grid item xs={3}>
156+
<Button
157+
variant="outlined"
158+
disabled={!detecting}
159+
color="secondary"
160+
size="large"
161+
onClick={() => {
162+
recognizerRef.current.onend();
163+
}}
164+
>
165+
{detecting ? "検知停止" : "検知待ち"}
166+
</Button>
167+
</Grid>
152168
</Grid>
153169
</Box>
154170
</Container>

0 commit comments

Comments
 (0)