Sunday 14 February 2016

Post 44: Speech recognition


Install plugin:

cordova plugin add https://github.com/macdonst/SpeechRecognitionPlugin
Go into your index.html file and replace the body with this:

<body ng-app="starter">
    <ion-pane ng-controller="AppCtrl">        
      <ion-content class="padding">     
        <button class="button button-full button-positive" ng-click="record()">
          Record
        </button>
        <div class="card">
          <div class="item item-text-wrap">
            {{recognizedText}}
          </div>
        </div>
      </ion-content>
    </ion-pane>
</body>
In your app.js replace the whole content with this:
angular.module('starter', ['ionic'])

.controller('AppCtrl', function($scope) {
  $scope.recognizedText = '';

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

For Android 6.0 users, you have to grant the app permission to access the device's microphone.

Credits to: http://devgirl.org/2016/01/08/speaking-with-cordova/



No comments:

Post a Comment

Tweet