| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- class VolumeTTS {
- ttsParams = {
- lang: 'zh-CN',
- speed: 1, // 将 0-10 转换为 0-1
- pitch: 1, // 将 0-10 转换为 0-1
- volume: 1,
- };
- TTSModule;
- platform = uni.getSystemInfoSync().platform
- constructor(opts) {
- if (opts) this.ttsParams = opts;
- // #ifdef APP-PLUS
- this.init()
- // #endif
- }
- init() {
- this.TTSModule = uni.requireNativePlugin('nrb-tts-plugin')
- this.TTSModule && this.TTSModule.init({
- "lang": "ZH",
- "country": "CN"
- }, res => {
- if (res.success == 0) {
- console.log('TTS初始化成功')
- }
- })
- }
- speak(text) {
- // #ifdef H5
- // H5端使用Web Speech API
- if ('speechSynthesis' in window) {
- const utterance = new SpeechSynthesisUtterance(text)
- utterance.lang = this.ttsParams.lang
- utterance.rate = this.ttsParams.speed
- utterance.pitch = this.ttsParams.pitch
- utterance.volume = this.ttsParams.volume
- speechSynthesis.speak(utterance)
- }
- // #endif
- console.log(this.platform, 'dsad');
- // #ifdef APP-PLUS
- let opts = this.platform == 'ios' ? {
- "rate": this.ttsParams.speed,
- "lang": "zh-CN",
- "volume": 1
- } : {
- "pitch": this.ttsParams.pitch,
- "speechRate": this.ttsParams.speed,
- "queueMode": 1,
- }
- console.log(this.TTSModule,'this.TTSModule')
- this.TTSModule.speak(text, opts, (e) => {
- console.log(e, '读取成功')
- })
- // #endif
- }
- stop() {
- if (this.TTSModule) {
- this.TTSModule.stop()
- }
- }
- }
- export default VolumeTTS
|