Advertisement
Guest User

SpeechRecognition Ionic React

a guest
Feb 28th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { SpeechRecognition } from '@ionic-native/speech-recognition/ngx';
  3. import toastr from 'toastr';
  4. import '../../toastr.min.css';
  5.  
  6.  
  7. class Contato extends React.Component<{}, { matches: string, isRecording: boolean }>{
  8.   constructor(props: any, private speechRecognition: SpeechRecognition ){
  9.     super(props);
  10.  
  11.     this.state = {
  12.       matches: '',
  13.       isRecording: false,
  14.     };
  15.  
  16.  
  17.     this.checkAvaliable = this.checkAvaliable.bind(this);
  18.     this.checkPermissions = this.checkPermissions.bind(this);
  19.     this.getPermissions = this.getPermissions.bind(this);
  20.     this.startListening = this.startListening.bind(this);
  21.     this.stopListening = this.stopListening.bind(this);
  22.  
  23.   }
  24.  
  25.   checkAvaliable = () => {
  26.     // Check feature available
  27.     this.speechRecognition.isRecognitionAvailable()
  28.     .then((available: boolean) => console.log(available))
  29.   }
  30.  
  31.   checkPermissions = () => {
  32.     // Check permission
  33.     this.speechRecognition.hasPermission()
  34.     .then((hasPermission: boolean) => console.log(hasPermission))
  35.   }
  36.  
  37.   getLanguageList = () => {
  38.     // Get the list of supported languages
  39.     this.speechRecognition.getSupportedLanguages()
  40.     .then(
  41.       (languages: string[]) => console.log(languages),
  42.       (error) => console.log(error)
  43.     )
  44.   }
  45.  
  46.   getPermissions = () => {
  47.     // Request permissions
  48.     this.speechRecognition.requestPermission()
  49.     .then(
  50.       () => console.log('Granted'),
  51.       () => console.log('Denied')
  52.     )
  53.   }
  54.  
  55.   startListening = () => {
  56.     let options = {
  57.       language: 'pt-BR',
  58.     };
  59.  
  60.     // Start the recognition process
  61.     this.speechRecognition.startListening(options)
  62.     .subscribe(
  63.       (matches: string[]) => console.log(matches),
  64.       (onerror) => console.log('error:', onerror)
  65.     )
  66.   }
  67.  
  68.   stopListening = () => {
  69.     this.speechRecognition.stopListening().then(() => {
  70.         this.setState({isRecording: false})
  71.     });
  72.   }
  73.  
  74.   render(){
  75.     return(
  76.     <>
  77.       <div className="mb-3">
  78.         <h3>O que eu entendi...</h3>
  79.         <span>{this.state.matches}</span>
  80.       </div>
  81.       <div className="row mt-5">
  82.         <div className="col-12">
  83.           <button className="btn btn-tiber w-100" onClick={this.checkAvaliable}>Check Avaliable</button>
  84.         </div>
  85.         <div className="col-12">
  86.           <button className="btn btn-tiber w-100" onClick={this.checkPermissions}>Check Permissions</button>
  87.         </div>
  88.         <div className="col-12">
  89.           <button className="btn btn-tiber w-100" onClick={this.getLanguageList}>Get Languages</button>
  90.         </div>
  91.         <div className="col-12">
  92.           <button className="btn btn-tiber w-100" onClick={this.getPermissions}>Get Permission</button>
  93.         </div>
  94.         <div className="col-12">
  95.           <button className="btn btn-tiber w-100" onClick={this.startListening}>Start Listening</button>
  96.         </div>
  97.       </div>
  98.     </>
  99.     );
  100.   }
  101.  
  102.  
  103. }
  104.  
  105.  
  106.  
  107. export default Contato;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement