daily-share icon indicating copy to clipboard operation
daily-share copied to clipboard

原生的语音实现语音播报 window.speechSynthesis(2020-05-07)

Open yaogengzhu opened this issue 4 years ago • 0 comments

原生的语音实现语音播报

最近项目要实现会员问好的一种需求,发现 window下有原生的方法支持播报!

  • dayjs npm库!
// 语音播报
export function voiceReport(message: string) {
    const msg = new SpeechSynthesisUtterance(message)
    msg.volume = 100
    msg.rate = 1
    msg.pitch = 1.5
    window.speechSynthesis.speak(msg)
}

export function sayHello() {
    let time = ""
    const hour = dayjs().hour()
    if (hour >= 0 && hour <= 8) {
      time = "早上"
    } else if (hour > 8 && hour <= 11) {
      time = "上午"
    } else if (hour > 11 && hour <= 13) {
      time = "中午"
    } else if (hour > 13 && hour <= 19) {
      time = "下午"
    } else if (hour > 19 && hour <= 24) {
      time = "晚上"
    }
    voiceReport("尊敬的会员" + time + '好')
}

yaogengzhu avatar May 07 '20 06:05 yaogengzhu