我正在尝试使用 speechSynthesis http://blog.teamtreehouse.com/getting-started-speech-synthesis-api
首先我用一个界面扩展了窗口:
窗口界面.ts
export interface IWindow extends Window {
webkitSpeechRecognition: any;
speechSynthesis: any;
}
接下来我做了一个窗口服务:
窗口服务.ts
import { Injectable } from '@angular/core';
import { IWindow } from '../interfaces/window-interface';
function _window() : IWindow {
return window;
}
@Injectable()
export class WindowService {
get nativeWindow() : any {
return _window();
}
}
现在在组件中,我正在尝试实现它....没有成功..
应用组件
import { Component, OnInit, ViewChild } from '@angular/core';
import { WindowService } from '../../providers/window.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['.app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private _router: Router, private _window: WindowService ) { }
sayIt() {
var utterance = new SpeechSynthesisUtterance('Hello World');
this._window.speechSynthesis.speak(utterance);
}
}
TS 错误:
Cannot find name 'SpeechSynthesisUtterance'.)
Property 'speechSynthesis' does not exist on type 'WindowService'.)
我还使用这篇关于语音识别的文章作为引用:Angular2: Web Speech API - Voice recognition
仍然出现错误—— 类型“Window”上不存在属性“speechSynthesis”
最佳答案
可以这样操作
var msg = new SpeechSynthesisUtterance("hello world");
(<any>window).speechSynthesis.speak(msg)
从链接中获得帮助
关于angular - 尝试在带有 IWindow 接口(interface)的 Angular2 中使用 speechSynthesis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42802007/