Created
February 5, 2017 02:09
-
-
Save wharley/61f20ec1c6bf7a97db59c313a2a21bf3 to your computer and use it in GitHub Desktop.
Reconhecedor de voz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, NgZone } from '@angular/core'; | |
import { Platform, ToastController } from 'ionic-angular'; | |
import { NavController } from 'ionic-angular'; | |
//declare var SpeechRecognition: any; | |
//OnStart - Para ativar a escuta. | |
//OnEnd - Para parar de ouvir. | |
//OnError - Para exibir erro no toast. | |
//OnResult - Para adicionar texto na lista. | |
declare let platform: any; | |
declare let window: any; | |
@Component({ | |
selector: 'page-home', | |
templateUrl: 'home.html' | |
}) | |
export class HomePage { | |
recognition: any; | |
ready: boolean = false; | |
isWaiting: boolean = false; | |
errorCordova : string = "No error"; | |
public statusSpeaker : string; | |
public resultText : string; | |
public isRecognizing : boolean = false; | |
public spokenWords : Array<string> = new Array<string>() | |
counter : number = 0; | |
constructor( | |
public navCtrl: NavController, | |
platform: Platform, | |
public _zone: NgZone, | |
public toastCtrl: ToastController | |
) { | |
platform = platform; | |
platform.ready().then(() => { | |
this.ready = true; | |
console.log("O reconhecimento é " + this.recognition) | |
if(window.SpeechRecognition) | |
{ | |
this.recognition = new window.SpeechRecognition(); | |
this.recognition.start(); | |
this.recognition.continuous = true; | |
this.recognition.lang = 'en-US'; | |
this.recognition.maxAlternatives = 3; | |
this.recognition.onnomatch = (event => { | |
console.log('No match found.'); | |
}); | |
this.recognition.onstart = (event => { | |
console.log('Reconhecimento iniciado.'); | |
this._zone.run(() => { | |
this.isRecognizing = true; | |
this.isWaiting = false; | |
} | |
) | |
}); | |
this.recognition.onend = (event => { | |
console.log('Reconhecimento parado.'); | |
this._zone.run(() => { | |
this.isRecognizing = false; | |
this.presentToast("Reconhecimento parado.") | |
this.isWaiting = false; | |
}) | |
}); | |
this.recognition.onerror = (event => { | |
console.log('Error...' + event.error); | |
this._zone.run(() => { | |
this.errorCordova = 'Error' | |
this.isRecognizing = false; | |
this.presentToast("Error..." + event.error) | |
this.isWaiting = false; | |
} | |
) | |
}); | |
this.recognition.onresult = (event => { | |
if (event.results) { | |
this._zone.run(() => { | |
var result = event.results[0]; | |
this.resultText = 'Ultimo texto adicionado : \n' + result[0].transcript | |
this.spokenWords.push(result[0].transcript); | |
console.log('Text: ' + result[0].transcript); | |
this.presentToast(this.resultText) | |
} | |
) | |
this.isWaiting = false; | |
this.isRecognizing = false; | |
} | |
}); | |
} | |
}); | |
} | |
presentToast(message) { | |
let toast = this.toastCtrl.create({ | |
message: message, | |
position : "middle", | |
duration: 3000 | |
}); | |
toast.present(); | |
} | |
SpeechToText(){ | |
this.statusSpeaker = 'Esperando...'; | |
console.log('entrou') | |
if(!this.isRecognizing) | |
{ | |
console.log(this.recognition) | |
this.recognition.start(); | |
this.isWaiting = true; | |
} | |
else | |
{ | |
this.isWaiting = true; | |
console.log('errou ou acerto') | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment