poiuytrez/SpeechRecognizer

module is not defined

Opened this issue · 18 comments

I'm new to android development, and am having a problem setting this plugin up. I have followed all of the instructions in the documentation, but when I run the app, i get the following error:

Uncaught ReferenceError: module is not defined at file:///android_asset/www/SpeechRecognizer.js:51

when i look at line 51 in SpechRecognizer.js, I'm seeing:
module.exports = new SpeechRecognizer();

I double checked, and I made the change to rex/xml/config.xml that was detailed in the documentation.

Are there any other configurations that I can check and change in order to get this plugin working?

Thanks,
Josh

Hi Josh,
my small reccomandation:
move to cordova/phonegap >= 3.0.0 because the older seems to be less and less supported and because it let's you use the plugman approch without having to add dependencies int he html file and modify manually the config.xml
You will need then to issue the command:

cordova plugin add https://github.com/poiuytrez/SpeechRecognizer.git

INSTALLATION works fine without any further step for configuration.

Please note: I should have written "Installation works well", so far I couldn't get the text of the speech.

Anyway for install:

ensure to have Cordova >=3.0.0 (I used 3.1.0)
issue:
cordova plugin add https://github.com/poiuytrez/SpeechRecognizer.git

Than in your JS (I use jquery)

function recognizeSpeech() {
                var maxMatches = 5;
                var promptString = "Speak now"; // optional
                var language = "en-US";                     // optional
                window.plugins.speechrecognizer.startRecognize(function(result){
                    alert(result);
                }, function(errorMessage){
                    console.log("Error message: " + errorMessage);
                }, maxMatches, promptString, language);
            }



$("#lnk_whatever").click(function (e) {
    e.stopImmediatePropagation();
    e.preventDefault();

    recognizeSpeech();
    });

I'm also trying to implement basic usage but I'm getting errors.

Uncaught ReferenceError: module is not defined at file:///android_asset/www/SpeechRecognizer.js:51

Uncaught SyntaxError: Unexpected token ( at file:///android_asset/www/index.html:25

By clicking the button 'Start Recognition', the following error occurs

Uncaught TypeError: Cannot read property 'speechrecognizer' of undefined at file:///android_asset/www/index.html:20

I followed your tips luigi37.

  • I'm use Cordova 3+;

  • I already have installed SpeechPlugin by command;

    Any other tips?
    Thanks.

Edit: Same problem here: #15

Could you please post the first 30 rows of your index.html?

Hi, luigi37, thanks for reply.
The index file is the same of the basic usage here: https://github.com/poiuytrez/SpeechRecognizer

<!DOCTYPE html>
<html>
    <head>
        <title>Speech Recognition plugin demo</title>
        <script type="text/javascript" src="cordova-2.8.js"></script>
        <script type="text/javascript" src="SpeechRecognizer.js"></script>
    </head>
    <body>

        <script type="text/javascript">

            function onDeviceReady(){
                console.log("Device is ready");
            }

            function recognizeSpeech() {
                var maxMatches = 5;
                var promptString = "Speak now"; // optional
                var language = "en-US";                     // optional
                window.plugins.speechrecognizer.startRecognize(function(result){
                    alert(result);
                }, function(errorMessage){
                    console.log("Error message: " + errorMessage);
                }, maxMatches, promptString, language);
            }

            // Show the list of the supported languages
            function getSupportedLanguages() {
                window.plugins.speechrecognizer.getSupportedLanguages(function(languages){
                    // display the json array
                    alert(languages);
                }, function(error){
                    alert("Could not retrieve the supported languages : " + error);
                });
            }

            document.addEventListener("deviceready", onDeviceReady, true);
        </script>

        <button onclick="recognizeSpeech();">Start recognition</button>
        <button onclick="getSupportedLanguages();">Get Supported Languages</button>
    </body>
</html>

Hi Murillo,
that's one problem:

 <script type="text/javascript" src="SpeechRecognizer.js"></script>

You should remove it, with 3.0.0 is not needed anymore ...

Second problem is that (if I remember well) on 3.0.0 on it should be:

<script type="text/javascript" src="cordova.js"></script>      

Without the version.

And.... you need to avoid having in www the cordova.js file.... cordova puts the right one in the specific platform directories...

I hope this solves it...

I made the changes and now my code is like this

<!DOCTYPE html>
<html>
    <head>
        <title>Speech Recognition plugin demo</title>
        <script type="text/javascript" src="cordova.js" />
        <script type="text/javascript" src="SpeechRecognizer.js" />
    </head>
    <body>

        <script type="text/javascript">

            function onDeviceReady(){
                console.log("Device is ready");
            }

            function recognizeSpeech() {
                var maxMatches = 5;
                var promptString = "Speak now"; // optional
                var language = "en-US";                     // optional
                window.plugins.speechrecognizer.startRecognize(function(result){
                    alert(result);
                }, function(errorMessage){
                    console.log("Error message: " + errorMessage);
                }, maxMatches, promptString, language);
            }

            // Show the list of the supported languages
            function getSupportedLanguages() {
                window.plugins.speechrecognizer.getSupportedLanguages(function(languages){
                    // display the json array
                    alert(languages);
                }, function(error){
                    alert("Could not retrieve the supported languages : " + error);
                });
            }

            document.addEventListener("deviceready", onDeviceReady, true);
        </script>

        <button onclick="recognizeSpeech();">Start recognition</button>
        <button onclick="getSupportedLanguages();">Get Supported Languages</button>
    </body>
</html>

And now, the errors changed too.

02-27 13:03:15.225: D/CordovaLog(14788): file:///android_asset/www/index.html: Line 40 : Uncaught ReferenceError: recognizeSpeech is not defined
02-27 13:03:15.225: E/Web Console(14788): Uncaught ReferenceError: recognizeSpeech is not defined:40

Completely remove:

<script type="text/javascript" src="SpeechRecognizer.js" /></script>

Removed. The error persists.

02-27 13:40:53.358: E/Web Console(22552): Uncaught ReferenceError: recognizeSpeech is not defined:39

Start from scratch and do as follow:

cordova create buz
cd buz
cordova plugin add https://github.com/poiuytrez/SpeechRecognizer
cordova platform add android

modify index.html like this:

<!DOCTYPE html>
<!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>

        <button id="btn1">Start recognition</button>
        <button id="btn2">Get Supported Languages</button>
    </body>
</html>

modify js/index.js like this:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
            function recognizeSpeech() {
                var maxMatches = 5;
                var promptString = "Speak now"; // optional
                var language = "en-US";                     // optional
                window.plugins.speechrecognizer.startRecognize(function(result){
                    alert(result);
                }, function(errorMessage){
                    console.log("Error message: " + errorMessage);
                }, maxMatches, promptString, language);
            }

            // Show the list of the supported languages
            function getSupportedLanguages() {
                window.plugins.speechrecognizer.getSupportedLanguages(function(languages){
                    // display the json array
                    alert(languages);
                }, function(error){
                    alert("Could not retrieve the supported languages : " + error);
                });
            }
            document.getElementById('btn1').addEventListener("click", function (event) {recognizeSpeech();});
        document.getElementById('btn2').addEventListener("click", function (event) {getSupportedLanguages();});

    }
};

.... and I confirm it not only get installed but also recognise the voice.

Here, to create an app, I have use the command starting with 'phonegap' instead 'cordova'. Using cordova, the result is 'command not found'.

So, I create the app using 'phonegap' commands and edited the codes.

phonegap create buz
phonegap build android
phonegap plugin add https://github.com/poiuytrez/SpeechRecognizer

The errors persist.

02-27 16:34:45.310: E/Web Console(16567): Uncaught TypeError: Cannot read property 'speechrecognizer' of undefined at file:///android_asset/www/js/index.js:44

Thank you for all support.

Which version of phonegap do you have?
The sintax of phonegap should include "local"...

I suggest anyway to install cordova...

Phonegap 3.3.0.
I installed cordova and did the process again.
The error persist. So sad.

02-27 21:55:49.150: E/Web Console(31167): Uncaught TypeError: Cannot read property 'speechrecognizer' of undefined at file:///android_asset/www/js/index.js:44

What do you use to test?

Actually to test plugins which are not part of the core cordova package, you should use chrome + ripple (command line version part of cordova/phonegap package, not chrome extension).

I test my apps on a tablet Samsung GT P6210 with Android Version 3.2. I already tested apps with Speech Recognizer and they works.

Has anyone resolved this issue? I am getting the same error:

Uncaught TypeError: Cannot read property 'speechrecognizer' of undefined

I have partly identified the problem, but it confuses me a lot. The issue seems to be that I cannot access window.plugins.speechrecognizer because it is somehow there and at the same time it isn’t. When I log window after cordova.js has been loaded I can see that it has an attribute plugins. However, when I try to access window.plugins or window.plugins.speechrecoginzer i get the above-mentioned TypeError. My index.html body looks as follows (I have previously put similar statements into the standard index.js, that doesn’t make a difference):

        <div class="app">
            <h1>XBMController</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
            <div class="remote">
                <div class="speech-row">
                    <input type="button" class="speech-button" id="speech-button1" value="Recognize Speech">
                </div>
            </div>
        </div>
        <script type="text/javascript" src="lib/jquery-1.8.2.min.js"></script>

        <script type="text/javascript" src="cordova.js"></script>

        <script type="text/javascript" src="js/main.js"></script>
        <script type="text/javascript">
            console.log(window);            
            document.getElementById('speech-button1').addEventListener("click", function (event) {window.plugins.speechrecognizer.startRecognize(controls.speech.processResult, null, null, 'Speak now', 'en-GB');});
            app.initialize();
        </script>

I am using cordova 3.5.0 under windows 7 and I have test the program under several emulator configurations and my Motorola Razr I. When applicable, I use Chrome 35.0 for debugging.

Does anyone know how to resolve this issue or could someone send me different configuration for me to test? Thanks.

I also come across this issue several days ago, it doesn't work out still. Did you fix it?

speechRecognition.js:1 Uncaught ReferenceError: module is not defined

is there a basic example which works with android ???