generated from capacitor-community/.github
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
259639e
commit 65645fe
Showing
2 changed files
with
115 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,131 @@ | ||
import { WebPlugin } from '@capacitor/core'; | ||
import { SpeechRecognitionPlugin, UtteranceOptions } from './definitions'; | ||
import { WebPlugin } from "@capacitor/core"; | ||
import { SpeechRecognitionPlugin, UtteranceOptions } from "./definitions"; | ||
|
||
export class SpeechRecognitionWeb extends WebPlugin implements SpeechRecognitionPlugin { | ||
declare var window: any; | ||
|
||
export class SpeechRecognitionWeb extends WebPlugin | ||
implements SpeechRecognitionPlugin { | ||
private speechRecognizer: any; | ||
constructor() { | ||
super({ | ||
name: 'SpeechRecognition', | ||
platforms: ['web'] | ||
name: "SpeechRecognition", | ||
platforms: ["web"], | ||
}); | ||
|
||
if (!this.speechRecognizer && window && window.webkitSpeechRecognition) { | ||
this.speechRecognizer = new window.webkitSpeechRecognition(); | ||
} | ||
} | ||
available(): Promise<{ available: boolean; }> { | ||
throw new Error("Method not implemented."); | ||
|
||
available(): Promise<{ available: boolean }> { | ||
return new Promise((resolve, reject) => { | ||
if (!this.speechRecognizer) { | ||
reject("This feature is not supported."); | ||
return; | ||
} | ||
|
||
resolve({ available: this.speechRecognizer !== undefined }); | ||
}); | ||
} | ||
start(options: UtteranceOptions): Promise<void> { | ||
console.log(options); | ||
throw new Error("Method not implemented."); | ||
|
||
start( | ||
options: UtteranceOptions | ||
): Promise<{ status: string; matches?: any; error?: string }> { | ||
var transcript = ""; | ||
|
||
return new Promise((resolve, reject) => { | ||
const { language, maxResults, partialResults } = options; | ||
const maxResultCount = maxResults ? maxResults : 5; | ||
|
||
if (!this.speechRecognizer) { | ||
reject({ | ||
status: "error", | ||
error: "This feature is not supported.", | ||
}); | ||
return; | ||
} | ||
|
||
if (language) { | ||
this.speechRecognizer.lang = language; | ||
} | ||
|
||
this.speechRecognizer.onend = () => { | ||
if (!partialResults) { | ||
resolve({ | ||
status: "success", | ||
matches: transcript, | ||
}); | ||
} | ||
}; | ||
this.speechRecognizer.onerror = reject; | ||
this.speechRecognizer.onresult = (ev: any) => { | ||
var temp_transcript = ""; | ||
|
||
const results = | ||
ev.results.length > maxResultCount | ||
? maxResultCount | ||
: ev.results.length; | ||
for (var i = ev.resultIndex; i < results; ++i) { | ||
if (ev.results[i].isFinal) { | ||
transcript += ev.results[i][0].transcript; | ||
} else { | ||
temp_transcript += ev.results[1][0].transcript; | ||
} | ||
} | ||
|
||
if (partialResults) { | ||
resolve({ | ||
status: "success", | ||
matches: temp_transcript, | ||
}); | ||
} | ||
}; | ||
}); | ||
} | ||
|
||
stop(): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
return new Promise((resolve, reject) => { | ||
if (!this.speechRecognizer) { | ||
reject("This feature is not supported."); | ||
return; | ||
} | ||
|
||
this.speechRecognizer.stop(); | ||
resolve(); | ||
}); | ||
} | ||
getSupportedLanguages(): Promise<{ languages: any[]; }> { | ||
|
||
getSupportedLanguages(): Promise<{ languages: any[] }> { | ||
throw new Error("Method not implemented."); | ||
} | ||
hasPermission(): Promise<{ permission: boolean; }> { | ||
throw new Error("Method not implemented."); | ||
|
||
hasPermission(): Promise<{ permission: boolean }> { | ||
return new Promise((resolve, reject) => { | ||
if (!this.speechRecognizer) { | ||
reject("This feature is not supported."); | ||
return; | ||
} | ||
|
||
navigator.getUserMedia( | ||
{ audio: true }, | ||
() => { | ||
resolve({ permission: true }); | ||
}, | ||
() => { | ||
reject({ permission: false }); | ||
} | ||
); | ||
}); | ||
} | ||
|
||
requestPermission(): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
async echo(options: { value: string }): Promise<{value: string}> { | ||
console.log('ECHO', options); | ||
return options; | ||
} | ||
} | ||
|
||
const SpeechRecognition = new SpeechRecognitionWeb(); | ||
|
||
export { SpeechRecognition }; | ||
|
||
import { registerWebPlugin } from '@capacitor/core'; | ||
import { registerWebPlugin } from "@capacitor/core"; | ||
registerWebPlugin(SpeechRecognition); |