macdonst / SpeechRecognitionPlugin

W3C Web Speech API - Speech Recognition plugin for PhoneGap
MIT License
189 stars 95 forks source link

No response or error in logcat using recognition.onresult on Android M and KitKat 4.4.4 #36

Open ghost opened 8 years ago

ghost commented 8 years ago

this code never fires any alert, running on Android M Same result when tested on Android KitKat 4.4.4

var recognition = new SpeechRecognition();

recognition.onresult = function(event) { alert(event); if (event.results.length > 0) { alert(event.results[0][0].transcript) } }

If I alert recognition it is returned as "[object object ]" the issue is with "onresult"

MaurersMake commented 8 years ago

I run the same piece of code and it does not fire on the first "record". Immediately hit the record button again and it works.

thadeuszlay commented 8 years ago

I've got the same issue as OP

jcesarmobile commented 8 years ago

Can you check with latest code?

are you calling start() somewhere? It won't fire onresult if you don't call start first

MaurersMake commented 8 years ago
$scope.record = function () {
    var recognition = new SpeechRecognition();
    //recongni.continuous = true;
    recognition.onresult = function (event) {
        if (event.results.length > 0) {
            $scope.recognizedText = event.results[0][0].transcript;
            $scope.$apply()
        }
    };

    recognition.onerror = function (error) {
        $scope.recognizedText = JSON.stringify(error);
    };

    recognition.start();
};
jcesarmobile commented 8 years ago

And the android version where you are testing this?

MaurersMake commented 8 years ago

My phone is Samsung Galaxy S6 Active with Android 5.1.1

jcesarmobile commented 8 years ago

Does it work the second time you call record? or never?

is the record function is being executed?

MaurersMake commented 8 years ago

I fired up development again and started running on device again. (I had put it away for a while waiting for responses). Now it works perfectly on the first click of "record" button. It DOES NOT work on the second click and it hits the onerror event.

MaurersMake commented 8 years ago

I question whether I have the latest source or not now. I use Visual Studio for this Cordova app - looking into making sure I have the latest code from you in my project.

jcesarmobile commented 8 years ago

The problem on the second call was fixed yesterday. I haven't increased the plugin version, so you'll have to uninstall and reinstall the plugin

MaurersMake commented 8 years ago

Ok - I have the latest source code implemented. Getting more strange results. I click "record" works perfect. Click "record" again - on error, again - on error, again - on error ... the fourth click tries to work, then the fifth click works. Then repeat pattern.

Here is my controller: (Cordova and angular) app.controller('AskGenieCtrl', function($scope, AskGenieSvc) { var recognition; $scope.data = { speechText: 'how now brown cow' }; $scope.recognizedText = ''; $scope.parsedText = '';

Initialize();

$scope.speakText = function () {
    TTS.speak({
        text: $scope.data.speechText,
        locale: 'en-EN',
        rate: 1
    }, function () {
    }, function (reason) {
    });
};

$scope.record = function () {
    recognition.start();
};

function Initialize() {
    recognition = new SpeechRecognition();
    recognition.onresult = function (event) {
        if (event.results.length > 0) {
            var text = event.results[0][0].transcript;
            $scope.recognizedText = text;
            $scope.parsedText = parseText(text);
            $scope.$apply();
        }
    };

    recognition.onerror = function (error) {
        alert(JSON.stringify(error));
    };
}

function parseText(text) {
    var wordCount = 1;
    for (i = 0; i < text.length; i++) {
        if (text.charAt(i) == " ") {
            wordCount++;
        }
    }
    var words = text.split(" ");
    return "Words: " + wordCount + "... " + words;
}

});