文字转语音:语音合成(Speech Synthesis) 数组文字循环播放

前言:
HTML5中和Web Speech相关的API实际上有两类,一类是"语音识别(Speech Recognition)",另外一个就是"语音合成(Speech Synthesis)", 这两个名词实际上指的分别是"语音转文字",和"文字变语音"。

speak() -- 只能接收SpeechSynthesisUtterance作为唯一的参数,作用是读合成的话语。 stop() -- 立即终止合成过程。 pause() -- 暂停合成过程。 resume() -- 重新开始合成过程。 getVoices() -- 此方法不接受任何参数,用来返回浏览器支持的语音包列表,是个数组。 speechSynthesis.getVoices()返回因每个浏览器不同及版本的不同返回不太一样,大致是这样:

注意:语言包获取不稳定,有时候返回为空,可以用定时器多试几次。

  • SpeechSynthesisUtterance主要用来构建语音合成实例,

  • speechSynthesis大概用来触发浏览器语音模块,让浏览器把内容读出来。 想要浏览器开口说话,只需要:

    javascript 复制代码
    let speechInstance = new SpeechSynthesisUtterance('大家好,我是渣渣辉。');
    speechSynthesis.speak(speechInstance);`
    
    就是这么简单,不妨copy进浏览器试一下?
    // 更换语言
    var msg = new SpeechSynthesisUtterance();
    var voices = window.speechSynthesis.getVoices(); // 获取语言包
    msg.voice = voices[18]; // 18:普通话
    msg.text = "Hello World";
    speechSynthesis.speak(msg); // 

    SpeechSynthesisUtterance实例有以下属性,可以通过设置一下属性调整发音:

  • text -- 要合成的文字内容,字符串。

  • lang -- 使用的语言,字符串, 例如:"zh-cn"

  • voiceURI -- 指定希望使用的声音和服务,字符串。

  • volume -- 声音的音量,区间范围是0到1,默认是1。

  • rate -- 语速,数值,默认值是1,范围是0.1到10,表示语速的倍数,例如2表示正常语速的两倍。

  • pitch -- 表示说话的音高,数值,范围从0(最小)到2(最大)。默认值为1。

    还有一些方法:

  • onstart -- 语音合成开始时候的回调。

  • onpause -- 语音合成暂停时候的回调。

  • onresume -- 语音合成重新开始时候的回调。

  • onend -- 语音合成结束时候的回调。

  • speechSynthesis对象有以下方法:

代码如下:

javascript 复制代码
<template>
    <div class="box">
        <!-- <input type="text" name="text" id="text" v-model="text" /> -->
        <button @click="playVoice">播放语音</button>
        <button @click="pauseVoice" v-if="status === '开始播放'">暂停播放</button>
        <button @click="resumeVoice" v-if="status === '暂停播放'">继续播放</button>
        <button @click="stopVoice">停止播放</button>
        <div>播放状态:{{ status }}</div>
    </div>
</template>
  
<script>
const synth = window.speechSynthesis
const msg = new window.SpeechSynthesisUtterance()
export default {
    data() {
        return {
            currentMsgIndex: 0,
            text: "",
            status: "",
            tableData: [
                { text: '切换后基站BCCH与TCH电平不一致' },
                { text: '切换带异常' },
                { text: '切换后TA偏大' },
                { text: '切换后基站BCCH与TCH电平不一致' },
                { text: '切换带异常' },
                { text: '切换后TA偏大' },
                { text: '切换后基站BCCH与TCH电平不一致' },
                { text: '切换带异常' },
                { text: '切换后TA偏大' }
            ]
        }
    },
    created() {
        this.playVoice()
    },
    mounted() {
        msg.onstart = e => {
            // this.currentMsgIndex = 0
            this.status = '开始播放'
            console.log(this.status)
        }
        msg.onend = e => {
            this.status = '结束播放'
            console.log(this.status)
            this.currentMsgIndex++
            if (this.currentMsgIndex < this.tableData.length) {
                this.playVoice()
            } else {
                this.currentMsgIndex = 0
            }
            console.log(this.currentMsgIndex)
        }
        msg.onpause = e => {
            this.status = "暂停播放"
            console.log('onpause:', this.status)
        }
    },
    destroyed() {
        this.stopVoice()
    },
    methods: {
        //播放
        playVoice() {
            this.handleSpeak(this.tableData[this.currentMsgIndex].text)
        },
        //暂停
        pauseVoice() {
            this.handlePause()
        },
        //继续
        resumeVoice() {
            this.handleResume()
        },
        //停止
        stopVoice() {
            this.handleStop()
        },
        handleSpeak(TEXT) {
            console.log('TEXT:', TEXT)
            // if (!this.text) return alert('请输入文本!')
            msg.text = TEXT // this.text
            msg.lang = 'zh-CN' // 语言
            msg.volume = 1 // 音量:0~1,默认1
            msg.rate = 1 // 语速:0.1~10,默认1
            msg.pitch = 1 // 音高:0~2,默认1
            // msg.voiceURI=''// 希望使用的声音
            synth.speak(msg)
        },
        handleStop(e) {
            synth.cancel(msg)
        },
        handlePause(e) {
            synth.pause(this.tableData[this.currentMsgIndex].text)
        },
        handleResume(e) {
            synth.resume(msg)
        }
    }
}
</script>
  
相关推荐
ldccorpora43 分钟前
Chinese News Translation Text Part 1数据集介绍,官网编号LDC2005T06
数据结构·人工智能·python·算法·语音识别
蚍蜉撼树谈何易3 小时前
一、语音识别基础(1.1 语音特征的提取)
人工智能·语音识别
aini_lovee5 小时前
基于判别码的深度神经网络快速自适应语音识别 MATLAB实现
matlab·语音识别·dnn
ldccorpora5 小时前
Chinese Treebank 5.0数据集介绍,官网编号LDC2005T01
人工智能·深度学习·自然语言处理·动态规划·语音识别
行业探路者2 天前
二维码标签是什么?主要有线上生成二维码和文件生成二维码功能吗?
学习·音视频·语音识别·二维码·设备巡检
2401_861277552 天前
中国电信星辰AI大模型有哪些主要功能
人工智能·云计算·软件工程·语音识别
乐迪信息2 天前
乐迪信息:船体AI烟火检测,24小时火灾自动预警
人工智能·物联网·算法·目标检测·语音识别
agicall.com2 天前
国产麒麟系统安装信创电话助手
人工智能·语音识别·自动录音·固话座机·离线语音转写
技术大咖--上好嘉3 天前
让陪伴不缺席,让安心常在线——智慧康养服务APP功能一览
人工智能·语音识别
小咖自动剪辑3 天前
小咖批量剪辑助手:视频批量自动剪辑与混剪处理软件(Windows)
人工智能·实时互动·音视频·语音识别·视频编解码