macdonst / SpeechRecognitionPlugin

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

how to reference with ionic 2 #76

Open ah3243 opened 8 years ago

ah3243 commented 8 years ago

Hi are you able to write an example of how to implement this app with ionic 2 and typescript. I'm writting a short demo app but i'm not sure how to reference the plugin. I'm getting a '[ts] Cannot find name SpeechRecognition' Error. Is this plugin compatible with ionic 2 yet?

Below is my main ts component


    import { Component } from '@angular/core';
    import { Platform } from 'ionic-angular';
    import { NavController } from 'ionic-angular';

    //declare var SpeechRecognition: any;
    declare var platform: any;

    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {

      recognition: any;
      ready: boolean = false;

      constructor(public navCtrl: NavController, platform: Platform) {
        platform = platform;
        platform.ready().then(() => { 
          this.ready = true;
        });
      }

      SpeechToText(){
        if(this.ready){
          this.recognition = new SpeechRecognition(); 
          this.recognition.lang = 'en-US';
          this.recognition.onnomatch = (event => {
              console.log('No match found.');
          });
          this.recognition.onerror = (event => {
              console.log('Error happens.');
          });
          this.recognition.onresult = (event => {
              if (event.results.length > 0) {
                  console.log('Text: ', event.results[0][0].transcript);          
              }
          });     
          this.recognition.start();
        };
      }  
    }

thanks

okonon commented 8 years ago

hey @ah3243 i found this ionic guide about how to implement your own ionic-native wrapper. But i do not have enough time to try it myself once you change your code in cloned ionic-native repo you need to run npm run build and then copy compiled code to your project's node_modules/ionic-native/src

@macdonst Any plans to support ionic 2?

yasseralsamman commented 7 years ago

Your example is working just remove the comment at declare var SpeechRecognition: any;