Skip to main content
added 1 character in body
Source Link
Günter Zöchbauer
  • 661.2k
  • 235
  • 2.1k
  • 1.6k

I assume the webkitSpeechRecognition API is not patched by Angulars zone.

To work around use zone.run(...) to force the execution back into Angulars zone explicitely:

@Injectable()
export class VoiceRecognitionService {

  constructor(private zone:NgZone) {
    /* void */
  }

  /**
   * Record
   * @param {string} language - Language of the voice recognition
   * @returns {Observable<string>} - Observable of voice converted to string 
   */
  record(language: string): Observable<string> {
    return Observable.create((observer) => {
      const { webkitSpeechRecognition }: IWindow = <IWindow>window;
      const recognition = new webkitSpeechRecognition();

      recognition.onresult = (e) => this.zone.run(() => observer.next(e.results.item(e.results.length - 1).item(0).transcript));
      recognition.onerror = (e) => this.zone.run(() => observer.error(e));
      recognition.onend = (e) => this.zone.run(() => observer.completed(e));

      recognition.continuous = true;
      recognition.interimResults = true;
      recognition.lang = language;
      recognition.start();
    });
  }
}

If the callbacks need a different number of parameters ((e)) please adjust the code accordingly. I just assumed one parameter for each.

The advantage of this approach is that the userusers of this service don't have to take additional measures.

I assume the webkitSpeechRecognition API is not patched by Angulars zone.

To work around use zone.run(...) to force the execution back into Angulars zone explicitely:

@Injectable()
export class VoiceRecognitionService {

  constructor(private zone:NgZone) {
    /* void */
  }

  /**
   * Record
   * @param {string} language - Language of the voice recognition
   * @returns {Observable<string>} - Observable of voice converted to string 
   */
  record(language: string): Observable<string> {
    return Observable.create((observer) => {
      const { webkitSpeechRecognition }: IWindow = <IWindow>window;
      const recognition = new webkitSpeechRecognition();

      recognition.onresult = (e) => this.zone.run(() => observer.next(e.results.item(e.results.length - 1).item(0).transcript));
      recognition.onerror = (e) => this.zone.run(() => observer.error(e));
      recognition.onend = (e) => this.zone.run(() => observer.completed(e));

      recognition.continuous = true;
      recognition.interimResults = true;
      recognition.lang = language;
      recognition.start();
    });
  }
}

If the callbacks need a different number of parameters ((e)) please adjust the code accordingly. I just assumed one parameter for each.

The advantage of this approach is that the user of this service don't have to take additional measures.

I assume the webkitSpeechRecognition API is not patched by Angulars zone.

To work around use zone.run(...) to force the execution back into Angulars zone explicitely:

@Injectable()
export class VoiceRecognitionService {

  constructor(private zone:NgZone) {
    /* void */
  }

  /**
   * Record
   * @param {string} language - Language of the voice recognition
   * @returns {Observable<string>} - Observable of voice converted to string 
   */
  record(language: string): Observable<string> {
    return Observable.create((observer) => {
      const { webkitSpeechRecognition }: IWindow = <IWindow>window;
      const recognition = new webkitSpeechRecognition();

      recognition.onresult = (e) => this.zone.run(() => observer.next(e.results.item(e.results.length - 1).item(0).transcript));
      recognition.onerror = (e) => this.zone.run(() => observer.error(e));
      recognition.onend = (e) => this.zone.run(() => observer.completed(e));

      recognition.continuous = true;
      recognition.interimResults = true;
      recognition.lang = language;
      recognition.start();
    });
  }
}

If the callbacks need a different number of parameters ((e)) please adjust the code accordingly. I just assumed one parameter for each.

The advantage of this approach is that the users of this service don't have to take additional measures.

Source Link
Günter Zöchbauer
  • 661.2k
  • 235
  • 2.1k
  • 1.6k

I assume the webkitSpeechRecognition API is not patched by Angulars zone.

To work around use zone.run(...) to force the execution back into Angulars zone explicitely:

@Injectable()
export class VoiceRecognitionService {

  constructor(private zone:NgZone) {
    /* void */
  }

  /**
   * Record
   * @param {string} language - Language of the voice recognition
   * @returns {Observable<string>} - Observable of voice converted to string 
   */
  record(language: string): Observable<string> {
    return Observable.create((observer) => {
      const { webkitSpeechRecognition }: IWindow = <IWindow>window;
      const recognition = new webkitSpeechRecognition();

      recognition.onresult = (e) => this.zone.run(() => observer.next(e.results.item(e.results.length - 1).item(0).transcript));
      recognition.onerror = (e) => this.zone.run(() => observer.error(e));
      recognition.onend = (e) => this.zone.run(() => observer.completed(e));

      recognition.continuous = true;
      recognition.interimResults = true;
      recognition.lang = language;
      recognition.start();
    });
  }
}

If the callbacks need a different number of parameters ((e)) please adjust the code accordingly. I just assumed one parameter for each.

The advantage of this approach is that the user of this service don't have to take additional measures.