tns plugin add nativescript-texttospeech
...
//import the plugin
import { TNSTextToSpeech, SpeakOptions } from 'nativescript-texttospeech';
...
@Component({
...
})
export class RecipeDetailComponent implements OnInit {
...
//set a default boolean for a button to animate when the tts plugin 'speaks'
isSpeaking: boolean = false;
...
private TTS: TNSTextToSpeech;
constructor(
...
) {
...
this.TTS = new TNSTextToSpeech();
}
...
//on button press, the text visible on the screen is spoken aloud
speak(text: string){
this.isSpeaking = true;
let speakOptions: SpeakOptions = {
text: text,
speakRate: 0.5,
//language: "fr",
finishedCallback: (() => {
this.isSpeaking = false;
})
}
this.TTS.speak(speakOptions);
}
}
安装并集成插件后,有趣的调整就可以开始了。提供了各种 SpeakOptions,以便您可以自定义播放。根据您的喜好调整语音速率、音量和音调,请注意,模拟器和平台的结果会有所不同。我发现将 speakRate 设置为推荐的“1.0”会导致我的食谱变成“松鼠”版本,但我可以想象,对于某些用户来说,增加音量滑块将非常有用。
text: string ** required **
queue?: boolean = false
pitch?: number = 1.0
speakRate?: number = 1.0
volume?: number = 1.0
language?: string = default system language
finishedCallback?: Function
本文是社区构建的有趣 NativeScript 插件系列的第一篇。您使用什么插件?您最喜欢哪些?请在评论中告诉我们。