Skip to content

Commit

Permalink
Fixes #1 - Add web support
Browse files Browse the repository at this point in the history
  • Loading branch information
priyankpat committed Jun 25, 2020
1 parent 259639e commit 65645fe
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 24 deletions.
10 changes: 6 additions & 4 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ declare module "@capacitor/core" {
}

export interface SpeechRecognitionPlugin {
available(): Promise<{ available: boolean; }>;
start(options: UtteranceOptions): Promise<void>;
available(): Promise<{ available: boolean }>;
start(
options: UtteranceOptions
): Promise<{ status: string; matches?: any; error?: string }>;
stop(): Promise<void>;
getSupportedLanguages(): Promise<{ languages: any[]; }>;
hasPermission(): Promise<{ permission: boolean; }>;
getSupportedLanguages(): Promise<{ languages: any[] }>;
hasPermission(): Promise<{ permission: boolean }>;
requestPermission(): Promise<void>;
}

Expand Down
129 changes: 109 additions & 20 deletions src/web.ts
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);

0 comments on commit 65645fe

Please sign in to comment.