Skip to content

Commit

Permalink
fix: delete no need code :neckbeard:
Browse files Browse the repository at this point in the history
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:

delete: stop button :neckbeard:

fix: button format:neckbeard:

fix: onend function :neckbeard:

fix: logic :neckbeard:

fix: logic :neckbeard:

fix: logic :neckbeard:

fix: logic :neckbeard:

fix: format :neckbeard:

fix: logic :neckbeard:

fix: logic :neckbeard:

fix: logic :neckbeard:

fix: logic :neckbeard:

fix: logic :neckbeard:

fix: format :neckbeard:
  • Loading branch information
bieshan committed Jan 5, 2021
1 parent 4296289 commit af17af4
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const Home = () => {
const [detecting, setDetecting] = useState(false); // 音声認識ステータス
const [finalText, setFinalText] = useState(""); // 確定された文章
const [transcript, setTranscript] = useState("ボタンを押して検知開始"); // 認識中の文章
const [android, setAndroid] = useState(false); // Android chrome用のフラグ
// 単語検知
const initialTagValues = ["年収"]; // デフォルト検知単語
const candidates = ["年収", "自由", "成功"]; // 検知単語候補
Expand All @@ -35,39 +36,46 @@ const Home = () => {
alert("お使いのブラウザには未対応です");
return;
}

// Androidのためのプラグ
if (/Android/i.test(navigator.userAgent)) {
setAndroid(true);
};

// NOTE: 将来的にwebkit prefixが取れる可能性があるため
const SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
recognizerRef.current = new SpeechRecognition();
recognizerRef.current.lang = "ja-JP";
recognizerRef.current.interimResults = true;
recognizerRef.current.continuous = true;
recognizerRef.current.maxAlternatives = 1;
recognizerRef.current.onstart = () => {
setDetecting(true);
};
recognizerRef.current.onend = () => {
setTranscript("");
setDetecting(false);
if (android && !alertOpen) {
recognizerRef.current.start();
}
};
recognizerRef.current.onresult = event => {
[...event.results].slice(event.resultIndex).forEach(result => {
const transcript = result[0].transcript;
setTranscript(transcript);
if (result.isFinal) {
if (tagValues.some(value => transcript.includes(value))) {
// NOTE: ユーザーが効果音を追加しなければデフォルトを鳴らす
(userMusic || music).play();
setAlertOpen(true);
}
// 音声認識が完了して文章が確定
setFinalText(prevState => {
return prevState + transcript;
// Android chromeなら値をそのまま返す
return android ? transcript : (prevState + transcript);
});
// 文章確定したら候補を削除
setTranscript("");
return;
}
// 音声認識の途中経過
if (tagValues.some(value => transcript.includes(value))) {
// NOTE: ユーザーが効果音を追加しなければデフォルトを鳴らす
(userMusic || music).play();
setAlertOpen(true);
}
setTranscript(transcript);
});
};
});
Expand Down Expand Up @@ -138,11 +146,11 @@ const Home = () => {
</Grid>
<Box m={2}>
<Grid container alignItems="center" justify="center">
<Grid item xs={3}>
<Grid item>
<Button
variant="outlined"
disabled={detecting}
color="primary"
color="secondary"
size="large"
onClick={() => {
recognizerRef.current.start();
Expand All @@ -151,20 +159,6 @@ const Home = () => {
{detecting ? "検知中..." : "検知開始"}
</Button>
</Grid>
<Grid item xs={6}/>
<Grid item xs={3}>
<Button
variant="outlined"
disabled={!detecting}
color="secondary"
size="large"
onClick={() => {
recognizerRef.current.onend();
}}
>
{detecting ? "検知停止" : "検知待ち"}
</Button>
</Grid>
</Grid>
</Box>
</Container>
Expand Down

0 comments on commit af17af4

Please sign in to comment.