Skip to content

IOS: Add Speech Recognition from audio file #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,58 @@ this.speechRecognition.startListening(
});
```

##### IOS ONLY
You can enable speech recognition from an audio file

>>> please no, if your audio file is short (about ~10 seconds) the stop function will not stop the recognition due to processing speed, with larger file there is no problem

```typescript
// import the options
import { SpeechRecognitionTranscription } from "nativescript-speech-recognition";

// do not fogred to add your audio folder/files to the webpack.config.js
// webpack.Utils.addCopyRule("audio/**");
let audio = '../audio/alex.mp3'
const appPath: Folder = <Folder>knownFolders.currentApp();
const folder: Folder = <Folder>appPath.getFolder(path.join("audio"));
const file: string = folder.getFile(audio).path;

this.speechRecognition.available().then((avail: boolean) => {
if (!avail) {
this.set("feedback", "speech recognition not available");
return;
}
this.speechRecognition.startListening(
{
returnPartialResults: true,
locale: "en-US",
fromFile: true,
path: file,
onResult: (transcription: SpeechRecognitionTranscription) => {
this.set("feedback", transcription.text);
if (transcription.finished) {
this.set("listening", false);
}
},
onError: (error: string | number) => {
console.log(">>>> error: " + error);
// because of the way iOS and Android differ, this is either:
// - iOS: A 'string', describing the issue.
// - Android: A 'number', referencing an 'ERROR_*' constant from https://developer.android.com/reference/android/speech/SpeechRecognizer.
// If this code is either 6 or 7 you may want to restart listening.
if (isAndroid && error === 6 /* timeout */) {
// this.startListening(locale);
}
}
}
).then((started: boolean) => {
this.set("listening", true);
}).catch((error: string | number) => {
console.log(`Error while trying to start listening: ${error}`);
});
});
```

##### Angular tip
If you're using this plugin in Angular, then note that the `onResult` callback is not part of Angular's lifecycle.
So either update the UI in [an `ngZone` as shown here](https://github.com/EddyVerbruggen/nativescript-pluginshowcase/blob/28f65ef98716ad7c4698071b9c394cceb2d9748f/app/speech/speech.component.ts#L154),
Expand Down
4 changes: 2 additions & 2 deletions demo/app/app.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@import "~@nativescript/theme/css/core.css";
@import "~@nativescript/theme/css/default.css";
@import "@nativescript/theme/css/core.css";
@import "@nativescript/theme/css/default.css";
Binary file added demo/app/audio/alex.mp3
Binary file not shown.
30 changes: 18 additions & 12 deletions demo/app/main-page.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
<StackLayout class="p-20">
<Label text="Speech Recognition" class="t-25 font-weight-bold text-center c-black" textWrap="true"/>
<!--<Label class="header" text="Search accessories not added to a Home, they'll be listed below" textWrap="true"/>-->
<StackLayout orientation="horizontal" horizontalAlignment="center">
<Button text="default" class="p-15" tap="{{ startListeningDefault }}" visibility="{{ listening ? 'collapse' : 'visible' }}"/>
<Button text="nl-NL" class="p-15" tap="{{ startListeningNL }}" visibility="{{ listening ? 'collapse' : 'visible' }}"/>
<Button text="en-US" class="p-15" tap="{{ startListeningEN }}" visibility="{{ listening ? 'collapse' : 'visible' }}"/>
<Button text="Stop" class="p-20" tap="{{ stopListening }}" visibility="{{ listening ? 'visible' : 'collapse' }}"/>
</StackLayout>
<Label style="border-radius: 8" class="m-10 p-15 t-15 text-center c-bg-charcoal c-white" text="{{ feedback }}" textWrap="true"/>
<ActivityIndicator class="m-20" busy="{{ listening }}" visibility="{{ listening ? 'visible' : 'collapse' }}"/>
</StackLayout>
<StackLayout class="p-20">
<Label text="Speech Recognition" class="t-25 font-weight-bold text-center c-black" textWrap="true"/>
<!--<Label class="header" text="Search accessories not added to a Home, they'll be listed below" textWrap="true"/>-->
<StackLayout orientation="horizontal" horizontalAlignment="center">
<Button text="default" class="p-15" tap="{{ startListeningDefault }}" visibility="{{ listening ? 'collapse' : 'visible' }}"/>
<Button text="nl-NL" class="p-15" tap="{{ startListeningNL }}" visibility="{{ listening ? 'collapse' : 'visible' }}"/>
<Button text="en-US" class="p-15" tap="{{ startListeningEN }}" visibility="{{ listening ? 'collapse' : 'visible' }}"/>
<Button text="Stop" class="p-20" tap="{{ stopListening }}" visibility="{{ listening ? 'visible' : 'collapse' }}"/>
</StackLayout>

<Label style="border-radius: 8" class="m-10 p-15 t-15 text-center c-bg-charcoal c-white" text="{{ feedback }}" textWrap="true"/>

<ActivityIndicator class="m-20" busy="{{ listening }}" visibility="{{ listening ? 'visible' : 'collapse' }}"/>

<StackLayout>
<Button text="Recognize from file :)" class="p-15" tap="{{ startListeningFromFile }}"/>
</StackLayout>
</StackLayout>
</Page>
38 changes: 25 additions & 13 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable, isAndroid } from "@nativescript/core";
import { Observable, isAndroid, Folder, knownFolders, path } from "@nativescript/core";
import { SpeechRecognition, SpeechRecognitionTranscription } from "nativescript-speech-recognition";

export class HelloWorldModel extends Observable {
Expand All @@ -19,6 +19,14 @@ export class HelloWorldModel extends Observable {
this.startListening();
}

public startListeningFromFile(): void {
let audio = "../audio/alex.mp3";
const appPath: Folder = <Folder>knownFolders.currentApp();
const folder: Folder = <Folder>appPath.getFolder(path.join("audio"));
const file: string = folder.getFile(audio).path;
this.startListening("en-US", true, file);
}

public startListeningNL(): void {
this.startListening("nl-NL");
}
Expand All @@ -27,54 +35,58 @@ export class HelloWorldModel extends Observable {
this.startListening("en-US");
}

public startListening(locale?: string): void {
let that = this; // TODO remove 'that'
/**
* local: is the local like en-US
* fromFile: if set to true you will need to provide a `path` for the audio file
* path: the file path
*/
public startListening(locale?: string, fromFile: boolean = false, path?: string): void {

this.speechRecognition.available().then((avail: boolean) => {
if (!avail) {
that.set("feedback", "speech recognition not available");
this.set("feedback", "speech recognition not available");
return;
}
that.speechRecognition.startListening(
this.speechRecognition.startListening(
{
returnPartialResults: true,
locale: locale,
fromFile: fromFile,
path: path,
onResult: (transcription: SpeechRecognitionTranscription) => {
that.set("feedback", transcription.text);
this.set("feedback", transcription.text);
if (transcription.finished) {
that.set("listening", false);
this.set("listening", false);
}
},
onError: (error: string | number) => {
console.log(">>>> error: " + error);
// because of the way iOS and Android differ, this is either:
// - iOS: A 'string', describing the issue.
// - Android: A 'number', referencing an 'ERROR_*' constant from https://developer.android.com/reference/android/speech/SpeechRecognizer.
// If that code is either 6 or 7 you may want to restart listening.
// If this code is either 6 or 7 you may want to restart listening.
if (isAndroid && error === 6 /* timeout */) {
// that.startListening(locale);
// this.startListening(locale);
}
}
}
).then((started: boolean) => {
that.set("listening", true);
this.set("listening", true);
}).catch((error: string | number) => {
console.log(`Error while trying to start listening: ${error}`);
});
});
}

public stopListening(): void {
let that = this;
this.speechRecognition.stopListening().then(() => {
that.set("listening", false);
this.set("listening", false);
}, (errorMessage: string) => {
console.log(`Error while trying to stop listening: ${errorMessage}`);
});
}

public requestPermission(): void {
let that = this;
this.speechRecognition.requestPermission().then((granted: boolean) => {
console.log("Granted? " + granted);
});
Expand Down
10 changes: 8 additions & 2 deletions demo/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const filePatterns = ['app/tests/**/*.*'];
module.exports = function (config) {
const options = {

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
basePath: 'app',


// frameworks to use
Expand All @@ -11,7 +12,7 @@ module.exports = function (config) {


// list of files / patterns to load in the browser
files: ['app/tests/**/*.*'],
files: filePatterns,


// list of files to exclude
Expand Down Expand Up @@ -78,6 +79,9 @@ module.exports = function (config) {

config.set(options);
}
module.exports.filePatterns = filePatterns;
// You can also use RegEx if you'd like:
// module.exports.filesRegex = /\.\/tests\/.*\.ts$/;

function setWebpackPreprocessor(config, options) {
if (config && config.bundle) {
Expand All @@ -99,6 +103,8 @@ function setWebpack(config, options) {
const env = {};
env[config.platform] = true;
env.sourceMap = config.debugBrk;
env.appPath = config.appPath;
env.karmaWebpack = true;
options.webpack = require('./webpack.config')(env);
delete options.webpack.entry;
delete options.webpack.output.libraryTarget;
Expand Down
10 changes: 5 additions & 5 deletions demo/nativescript.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NativeScriptConfig } from '@nativescript/core';
import { NativeScriptConfig } from "@nativescript/core";

export default {
id: 'org.nativescript.plugin.speechrecognition',
appResourcesPath: 'app/App_Resources',
id: "org.nativescript.plugin.speechrecognition",
appResourcesPath: "app/App_Resources",
android: {
v8Flags: '--expose_gc',
markingMode: 'none',
v8Flags: "--expose_gc",
markingMode: "none",
}
} as NativeScriptConfig;
Loading