├── .gitattributes
├── .gitignore
├── .npmignore
├── README.md
├── android
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.buildship.core.prefs
├── README.md
├── build.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ └── java
│ └── com
│ └── reactlibrary
│ ├── VoiceRecognizerModule.java
│ └── VoiceRecognizerPackage.java
├── index.ts
├── package.json
└── yarn.lock
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.pbxproj -text
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # OSX
2 | #
3 | .DS_Store
4 |
5 | # node.js
6 | #
7 | node_modules/
8 | npm-debug.log
9 | yarn-error.log
10 |
11 | # Xcode
12 | #
13 | build/
14 | *.pbxuser
15 | !default.pbxuser
16 | *.mode1v3
17 | !default.mode1v3
18 | *.mode2v3
19 | !default.mode2v3
20 | *.perspectivev3
21 | !default.perspectivev3
22 | xcuserdata
23 | *.xccheckout
24 | *.moved-aside
25 | DerivedData
26 | *.hmap
27 | *.ipa
28 | *.xcuserstate
29 | project.xcworkspace
30 |
31 | # Android/IntelliJ
32 | #
33 | build/
34 | .idea
35 | .gradle
36 | local.properties
37 | *.iml
38 |
39 | # BUCK
40 | buck-out/
41 | \.buckd/
42 | *.keystore
43 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | # OSX
2 | #
3 | .DS_Store
4 |
5 | # node.js
6 | #
7 | node_modules/
8 | npm-debug.log
9 | yarn-error.log
10 |
11 | # Xcode
12 | #
13 | build/
14 | *.pbxuser
15 | !default.pbxuser
16 | *.mode1v3
17 | !default.mode1v3
18 | *.mode2v3
19 | !default.mode2v3
20 | *.perspectivev3
21 | !default.perspectivev3
22 | xcuserdata
23 | *.xccheckout
24 | *.moved-aside
25 | DerivedData
26 | *.hmap
27 | *.ipa
28 | *.xcuserstate
29 | project.xcworkspace
30 |
31 | # Android/IntelliJ
32 | #
33 | build/
34 | .idea
35 | .gradle
36 | local.properties
37 | *.iml
38 |
39 | # BUCK
40 | buck-out/
41 | \.buckd/
42 | *.keystore
43 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-native-voice-recognizer:microphone:
2 | 
3 | >A tiny lib for open native platform voice regonizer in react native.
4 | ## Motivation
5 | `react-native-community` provides an awesome @react-native-community/voice for reconiting voice but sometimes you don't want to create your own voice reconizer also you want a clean way and recogniting with native platform tools.
6 | ## Feautures
7 | - Tiny And Clean Way. :rocket:
8 | - Native Implementation. :chart_with_upwards_trend:
9 | - Easy To Use. :heart_eyes: ( only have one method )
10 | - No `ios` Support. :hankey: ( may be add in future )
11 | ## Result
12 |
13 | ## Installation
14 | installing with yarn :
15 |
16 | ```bash
17 | yarn add react-native-voice-recognizer
18 | ```
19 |
20 | or alternativly with npm :
21 | ```bash
22 | npm i react-native-voice-recognizer
23 | ```
24 |
25 | Note : if you are using RN@>=0.60 you dont need any more enjoy RN auto-linking with `npx react-native run-android`
but for RN@<0.60 you need to `link` with `react-native link` or linking manually .
26 | >manully linking will add soon here.
27 |
28 | ## Usage
29 |
30 | first import
31 | ```js
32 | import VoiceRecognizer from 'react-native-voice-recognizer';
33 | ```
34 | then use with then/catch ES6 syntax :
35 | ```js
36 | //settings are required
37 | const locale = 'en-US'; //or for example : 'fa-IR'
38 | const promptLabel = 'Say Something...';
39 |
40 | //request for recognizing
41 | VoiceRecognizer.requestVoice(locale, promptLabel)
42 | .then(result => {
43 | //do anything with result and enjoy
44 | })
45 | .catch(e => {
46 | //your device does not support voice recognition
47 | });
48 | ```
49 | And Enjoy. :heart_eyes:
50 | Or use with async/await ES2017 feature :
51 | ```js
52 | const startListennig = async () => {
53 | //settings are requires
54 | const locale = 'en-US'; //or for example :'fa-IR'
55 | const promptLabel = 'Say Something...'; //or anything you want
56 |
57 | try {
58 | const result = await VoiceRecognizer.requestVoice(locale, promptLabel);
59 | //do anything with result and enjoy
60 | } catch (error) {
61 | //your device does not support voice recognition
62 | }
63 | };
64 | ```
65 | ## Api
66 |
67 | > soon add here
68 |
69 | ## Contributing
70 |
71 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
72 |
73 | Please make sure to update tests as appropriate.
74 |
75 | ## Licence
76 | MIT
77 |
78 | maked with :heart: for developers.
79 |
80 |
81 |
--------------------------------------------------------------------------------
/android/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/android/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | android
4 | Project android created by Buildship.
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.buildship.core.gradleprojectbuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.buildship.core.gradleprojectnature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/android/.settings/org.eclipse.buildship.core.prefs:
--------------------------------------------------------------------------------
1 | arguments=
2 | auto.sync=false
3 | build.scans.enabled=false
4 | connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.3))
5 | connection.project.dir=
6 | eclipse.preferences.version=1
7 | gradle.user.home=
8 | java.home=/usr/lib/jvm/jdk1.8.0_231
9 | jvm.arguments=
10 | offline.mode=false
11 | override.workspace.settings=true
12 | show.console.view=true
13 | show.executions.view=true
14 |
--------------------------------------------------------------------------------
/android/README.md:
--------------------------------------------------------------------------------
1 | README
2 | ======
3 |
4 | If you want to publish the lib as a maven dependency, follow these steps before publishing a new version to npm:
5 |
6 | 1. Be sure to have the Android [SDK](https://developer.android.com/studio/index.html) and [NDK](https://developer.android.com/ndk/guides/index.html) installed
7 | 2. Be sure to have a `local.properties` file in this folder that points to the Android SDK and NDK
8 | ```
9 | ndk.dir=/Users/{username}/Library/Android/sdk/ndk-bundle
10 | sdk.dir=/Users/{username}/Library/Android/sdk
11 | ```
12 | 3. Delete the `maven` folder
13 | 4. Run `./gradlew installArchives`
14 | 5. Verify that latest set of generated files is in the maven folder with the correct version number
15 |
--------------------------------------------------------------------------------
/android/build.gradle:
--------------------------------------------------------------------------------
1 | // android/build.gradle
2 |
3 | // based on:
4 | //
5 | // * https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle
6 | // original location:
7 | // - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/build.gradle
8 | //
9 | // * https://github.com/facebook/react-native/blob/0.60-stable/template/android/app/build.gradle
10 | // original location:
11 | // - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/app/build.gradle
12 |
13 | def DEFAULT_COMPILE_SDK_VERSION = 28
14 | def DEFAULT_BUILD_TOOLS_VERSION = '28.0.3'
15 | def DEFAULT_MIN_SDK_VERSION = 16
16 | def DEFAULT_TARGET_SDK_VERSION = 28
17 |
18 | def safeExtGet(prop, fallback) {
19 | rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
20 | }
21 |
22 | apply plugin: 'com.android.library'
23 | apply plugin: 'maven'
24 |
25 | buildscript {
26 | // The Android Gradle plugin is only required when opening the android folder stand-alone.
27 | // This avoids unnecessary downloads and potential conflicts when the library is included as a
28 | // module dependency in an application project.
29 | // ref: https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:build_script_external_dependencies
30 | if (project == rootProject) {
31 | repositories {
32 | google()
33 | jcenter()
34 | }
35 | dependencies {
36 | classpath 'com.android.tools.build:gradle:3.4.1'
37 | }
38 | }
39 | }
40 |
41 | apply plugin: 'com.android.library'
42 | apply plugin: 'maven'
43 |
44 | android {
45 | compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
46 | buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
47 | defaultConfig {
48 | minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
49 | targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
50 | versionCode 1
51 | versionName "1.0"
52 | }
53 | lintOptions {
54 | abortOnError false
55 | }
56 | }
57 |
58 | repositories {
59 | // ref: https://www.baeldung.com/maven-local-repository
60 | mavenLocal()
61 | maven {
62 | // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
63 | url "$rootDir/../node_modules/react-native/android"
64 | }
65 | maven {
66 | // Android JSC is installed from npm
67 | url "$rootDir/../node_modules/jsc-android/dist"
68 | }
69 | google()
70 | jcenter()
71 | }
72 |
73 | dependencies {
74 | //noinspection GradleDynamicVersion
75 | implementation 'com.facebook.react:react-native:+' // From node_modules
76 | }
77 |
78 | def configureReactNativePom(def pom) {
79 | def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)
80 |
81 | pom.project {
82 | name packageJson.title
83 | artifactId packageJson.name
84 | version = packageJson.version
85 | group = "com.reactlibrary"
86 | description packageJson.description
87 | url packageJson.repository.baseUrl
88 |
89 | licenses {
90 | license {
91 | name packageJson.license
92 | url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
93 | distribution 'repo'
94 | }
95 | }
96 |
97 | developers {
98 | developer {
99 | id packageJson.author.username
100 | name packageJson.author.name
101 | }
102 | }
103 | }
104 | }
105 |
106 | afterEvaluate { project ->
107 | // some Gradle build hooks ref:
108 | // https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html
109 | task androidJavadoc(type: Javadoc) {
110 | source = android.sourceSets.main.java.srcDirs
111 | classpath += files(android.bootClasspath)
112 | classpath += files(project.getConfigurations().getByName('compile').asList())
113 | include '**/*.java'
114 | }
115 |
116 | task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
117 | classifier = 'javadoc'
118 | from androidJavadoc.destinationDir
119 | }
120 |
121 | task androidSourcesJar(type: Jar) {
122 | classifier = 'sources'
123 | from android.sourceSets.main.java.srcDirs
124 | include '**/*.java'
125 | }
126 |
127 | android.libraryVariants.all { variant ->
128 | def name = variant.name.capitalize()
129 | def javaCompileTask = variant.javaCompileProvider.get()
130 |
131 | task "jar${name}"(type: Jar, dependsOn: javaCompileTask) {
132 | from javaCompileTask.destinationDir
133 | }
134 | }
135 |
136 | artifacts {
137 | archives androidSourcesJar
138 | archives androidJavadocJar
139 | }
140 |
141 | task installArchives(type: Upload) {
142 | configuration = configurations.archives
143 | repositories.mavenDeployer {
144 | // Deploy to react-native-event-bridge/maven, ready to publish to npm
145 | repository url: "file://${projectDir}/../android/maven"
146 | configureReactNativePom pom
147 | }
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/android/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/android/src/main/java/com/reactlibrary/VoiceRecognizerModule.java:
--------------------------------------------------------------------------------
1 | package com.reactlibrary;
2 |
3 | //required for bridge native and js
4 | import com.facebook.react.bridge.ReactApplicationContext;
5 | import com.facebook.react.bridge.ReactContextBaseJavaModule;
6 | import com.facebook.react.bridge.ReactMethod;
7 | import com.facebook.react.bridge.Callback;
8 | import com.facebook.react.bridge.NativeModule;
9 | import com.facebook.react.bridge.ReactContext;
10 | import com.facebook.react.bridge.Promise;
11 | import com.facebook.react.bridge.BaseActivityEventListener;
12 | import com.facebook.react.bridge.ActivityEventListener;
13 |
14 | //required for android native
15 | import android.speech.RecognizerIntent;
16 | import android.content.ActivityNotFoundException;
17 | import android.app.Activity;
18 | import android.content.Intent;
19 |
20 | //required for java
21 | import java.util.ArrayList;
22 |
23 | public class VoiceRecognizerModule extends ReactContextBaseJavaModule implements ActivityEventListener {
24 |
25 | private static final int REQUEST_VOICE_RECOGNIZER_CODE = 100;
26 | private final ReactApplicationContext reactContext;
27 | private Promise mPickerPromise;
28 |
29 | public VoiceRecognizerModule(ReactApplicationContext reactContext) {
30 | super(reactContext);
31 | reactContext.addActivityEventListener(this);
32 | this.reactContext = reactContext;
33 | }
34 |
35 | @Override
36 | public void onActivityResult(Activity activity, final int requestCode, final int resultCode, final Intent data) {
37 | if (requestCode == REQUEST_VOICE_RECOGNIZER_CODE) {
38 | if (resultCode == Activity.RESULT_OK && null != data) {
39 | ArrayList result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
40 | mPickerPromise.resolve(result.get(0).toString());
41 | }
42 | }
43 | }
44 |
45 | @Override
46 | public String getName() {
47 | return "VoiceRecognizer";
48 | }
49 |
50 | @Override
51 | public void onNewIntent(Intent intent) {
52 |
53 | }
54 |
55 | @ReactMethod
56 | public void requestVoice(final String language, final String prompt, final Promise promise) {
57 | mPickerPromise = promise;
58 | try {
59 | Activity currentActivity = getCurrentActivity();
60 | Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
61 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, language);
62 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
63 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);
64 | intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, language);
65 | intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
66 | currentActivity.startActivityForResult(intent, REQUEST_VOICE_RECOGNIZER_CODE);
67 |
68 | } catch (Exception e) {
69 | mPickerPromise.reject("ERROR", e);
70 | mPickerPromise = null;
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/android/src/main/java/com/reactlibrary/VoiceRecognizerPackage.java:
--------------------------------------------------------------------------------
1 | package com.reactlibrary;
2 |
3 | import java.util.Arrays;
4 | import java.util.Collections;
5 | import java.util.List;
6 |
7 | import com.facebook.react.ReactPackage;
8 | import com.facebook.react.bridge.NativeModule;
9 | import com.facebook.react.bridge.ReactApplicationContext;
10 | import com.facebook.react.uimanager.ViewManager;
11 | import com.facebook.react.bridge.JavaScriptModule;
12 |
13 | public class VoiceRecognizerPackage implements ReactPackage {
14 | @Override
15 | public List createNativeModules(ReactApplicationContext reactContext) {
16 | return Arrays.asList(new VoiceRecognizerModule(reactContext));
17 | }
18 |
19 | @Override
20 | public List createViewManagers(ReactApplicationContext reactContext) {
21 | return Collections.emptyList();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/index.ts:
--------------------------------------------------------------------------------
1 | import { NativeModules } from "react-native";
2 |
3 | const { VoiceRecognizer } = NativeModules;
4 |
5 | export default VoiceRecognizer;
6 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-native-voice-recognizer",
3 | "title": "React Native Voice Recognizer",
4 | "version": "1.0.2",
5 | "description": "A tiny lib for open native platform voice regonizer in react native.",
6 | "main": "index.ts",
7 | "types": "index.d.ts",
8 | "scripts": {
9 | "test": "echo \"Error: no test specified\" && exit 1"
10 | },
11 | "repository": {
12 | "type": "git",
13 | "url": "git+https://github.com/farshidshahmoradi1996/react-native-voice-recognizer.git",
14 | "baseUrl": "https://github.com/farshidshahmoradi1996/react-native-voice-recognizer"
15 | },
16 | "keywords": [
17 | "react-native"
18 | ],
19 | "author": {
20 | "name": "Farshid Shahmoradi",
21 | "email": "farshidshahmoradi1996@gmail.com"
22 | },
23 | "license": "MIT",
24 | "licenseFilename": "LICENSE",
25 | "readmeFilename": "README.md",
26 | "peerDependencies": {
27 | "react": "^16.8.1",
28 | "react-native": ">=0.60.0-rc.0 <1.0.x"
29 | },
30 | "devDependencies": {
31 | "react": "^16.9.0",
32 | "react-native": "^0.61.5"
33 | }
34 | }
35 |
--------------------------------------------------------------------------------