Skip to content

Commit

Permalink
update webcomp
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 committed May 27, 2024
1 parent 742ab9d commit 39fab0d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Published on npm](https://img.shields.io/npm/v/@node-projects/speech-recognition.webcomponent.svg?logo=npm)](https://www.npmjs.com/package/@node-projects/speech-recognition.webcomponent)

# description
a simple speech-recognition.webcomponet ..
a simple speech-recognition.webcomponent ..

# development
## upload new version
Expand Down
20 changes: 17 additions & 3 deletions src/SpeechRecognition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class SpeechRecognitionWebcomponent extends BaseCustomWebComponentConstru

//@ts-ignore
private _recognition: SpeechRecognition;
private _lastStartedAt: number;

public static properties = {
autostart: Boolean,
Expand Down Expand Up @@ -43,6 +44,7 @@ export class SpeechRecognitionWebcomponent extends BaseCustomWebComponentConstru

start() {
this.init();
this._lastStartedAt = new Date().getTime();
this._recognition.start();
}

Expand All @@ -55,10 +57,10 @@ export class SpeechRecognitionWebcomponent extends BaseCustomWebComponentConstru
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
}
recognition.continuous = true;
recognition.continuous = false;
recognition.lang = this.lang;
//recognition.interimResults = false;
//recognition.maxAlternatives = 1;
recognition.interimResults = false;
recognition.maxAlternatives = 1;

recognition.onresult = (e) => {
this.text = e.results[0][0].transcript;
Expand All @@ -76,6 +78,18 @@ export class SpeechRecognitionWebcomponent extends BaseCustomWebComponentConstru
recognition.onerror = (e) => {
}

recognition.onend = () => {
let timeSinceLastStart = new Date().getTime() - this._lastStartedAt;
if (timeSinceLastStart > 1000) {
this._lastStartedAt = new Date().getTime();
setTimeout(() => {
this._recognition.start();
}, 50);
} else {
console.log("too fast restart");
}
}

//@ts-ignore
this._recognition = recognition;
}
Expand Down

0 comments on commit 39fab0d

Please sign in to comment.