Skip to content

Commit 5f04a04

Browse files
committed
fix: tts not working on macOS Chrome
1 parent 79c75d8 commit 5f04a04

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

packages/core/src/hooks/sound.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ export function usePlayWordAudio() {
116116
return playAudio
117117
}
118118

119+
function getVoicesAsync() {
120+
return new Promise(resolve => {
121+
const voices = speechSynthesis.getVoices()
122+
if (voices.length) return resolve(voices)
123+
124+
speechSynthesis.onvoiceschanged = () => {
125+
resolve(speechSynthesis.getVoices())
126+
}
127+
})
128+
}
119129
export function useTTsPlayAudio() {
120130
const settingStore = useSettingStore()
121131

@@ -126,11 +136,13 @@ export function useTTsPlayAudio() {
126136
msg.volume = settingStore.wordSoundVolume / 100
127137
msg.pitch = 1
128138
msg.lang = 'en-US'
129-
let voiceList = speechSynthesis.getVoices().filter(v => v.lang === 'en-US')
130-
if (voiceList && voiceList.length) {
131-
msg.voice = voiceList.find(v => v.name.includes('Emma ')) || voiceList[0]
132-
}
133-
speechSynthesis.speak(msg)
139+
getVoicesAsync().then(voices => {
140+
let voiceList = voices.filter(v => v.lang === 'en-US')
141+
if (voiceList && voiceList.length) {
142+
msg.voice = voiceList.find(v => v.name.includes('Emma') || v.name.includes('US')) ?? voiceList[0]
143+
}
144+
speechSynthesis.speak(msg)
145+
})
134146
}
135147

136148
return play

packages/core/src/stores/base.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getDefaultDict } from '../types'
44
import { _getStudyProgress, checkAndUpgradeSaveDict } from '../utils'
55
import { shallowReactive } from 'vue'
66
import { get } from 'idb-keyval'
7-
import { AppEnv, DictId, SAVE_DICT_KEY } from '../config/env'
7+
import { AppEnv, DictId, IS_DEV, SAVE_DICT_KEY } from '../config/env'
88
import { add2MyDict, dictListVersion, myDictList } from '../apis'
99
import { Toast } from '@typewords/base'
1010
import type { Card } from 'ts-fsrs'
@@ -169,7 +169,13 @@ export const useBaseStore = defineStore('base', {
169169
})
170170
//必须先 reset, 只 $patch 无法将 state 恢复到默认值
171171
this.$reset()
172-
this.$patch(obj)
172+
console.time('$patch')
173+
if (IS_DEV) {
174+
this.$state = obj
175+
} else {
176+
this.$patch(obj)
177+
}
178+
console.timeEnd('$patch')
173179
},
174180
async init() {
175181
return new Promise(async resolve => {

packages/core/src/utils/eventBus.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ export function useEventsByWatch(arrs: any[], watchVal: any) {
3333
watch(
3434
watchVal,
3535
newVal => {
36-
console.log('watch', newVal)
3736
if (newVal) {
3837
arrs.map(arr => emitter.on(arr[0], arr[1]))
3938
} else {
4039
arrs.map(arr => emitter.off(arr[0], arr[1]))
4140
}
42-
console.log('emitter-all', emitter.all)
4341
},
4442
{
4543
immediate: true,

0 commit comments

Comments
 (0)