当前位置:首页 > JavaScript

js实现tts

2026-04-06 13:48:11JavaScript

使用Web Speech API实现TTS

Web Speech API是浏览器原生支持的语音合成接口,无需额外库即可实现文本转语音功能。

// 创建语音合成实例
const synth = window.speechSynthesis;

// 创建语音内容
const utterance = new SpeechSynthesisUtterance('你好,这是测试语音');

// 设置语音参数
utterance.rate = 1;    // 语速 (0.1-10)
utterance.pitch = 1;   // 音高 (0-2)
utterance.volume = 1;  // 音量 (0-1)

// 获取可用语音列表
const voices = synth.getVoices();

// 设置特定语音(需等待voiceschanged事件)
utterance.voice = voices.find(voice => voice.lang === 'zh-CN');

// 执行语音合成
synth.speak(utterance);

使用第三方库responsive-voice

responsive-voice.js提供更简单的API和跨浏览器支持。

<script src="https://code.responsivevoice.org/responsivevoice.js"></script>
<script>
responsiveVoice.speak("这是测试文本", "Chinese Female", {
    pitch: 1,
    rate: 1,
    volume: 1
});
</script>

实现语音队列系统

对于需要连续播放多个语音的场景,可以建立队列系统。

class TTSQueue {
    constructor() {
        this.queue = [];
        this.synth = window.speechSynthesis;
        this.isSpeaking = false;
    }

    add(text, lang = 'zh-CN') {
        this.queue.push({text, lang});
        if (!this.isSpeaking) this.processQueue();
    }

    processQueue() {
        if (this.queue.length === 0) {
            this.isSpeaking = false;
            return;
        }

        this.isSpeaking = true;
        const {text, lang} = this.queue.shift();
        const utterance = new SpeechSynthesisUtterance(text);

        utterance.voice = this.synth.getVoices()
            .find(voice => voice.lang === lang);

        utterance.onend = () => this.processQueue();
        this.synth.speak(utterance);
    }
}

// 使用示例
const tts = new TTSQueue();
tts.add("第一条消息");
tts.add("第二条消息", "en-US");

浏览器兼容性处理

不同浏览器对语音合成的支持存在差异,需要进行兼容性检测。

function checkTTSsupport() {
    if (!('speechSynthesis' in window)) {
        console.error('该浏览器不支持语音合成API');
        return false;
    }

    // 检测中文语音支持
    const voices = speechSynthesis.getVoices();
    const hasChinese = voices.some(v => v.lang.includes('zh'));

    if (!hasChinese) {
        console.warn('未找到中文语音引擎');
    }

    return true;
}

// 建议在voiceschanged事件后检测
speechSynthesis.onvoiceschanged = checkTTSsupport;

语音合成事件监听

通过事件监听可以实现更精细的控制。

js实现tts

const utterance = new SpeechSynthesisUtterance('测试事件');

utterance.onstart = (event) => {
    console.log('语音开始播放');
};

utterance.onend = (event) => {
    console.log('语音播放结束');
};

utterance.onerror = (event) => {
    console.error('发生错误:', event.error);
};

utterance.onpause = (event) => {
    console.log('语音暂停');
};

utterance.onresume = (event) => {
    console.log('语音恢复');
};

speechSynthesis.speak(utterance);

注意事项

  1. 某些浏览器需要用户交互(如点击事件)后才能触发语音播放
  2. 移动设备可能有额外的限制或不同的行为
  3. 语音质量取决于用户设备安装的语音引擎
  4. 长时间文本可能需要分段处理以避免性能问题

标签: jstts
分享给朋友:

相关文章

js实现vue路由

js实现vue路由

Vue路由基础实现 使用Vue Router实现前端路由需要先安装并配置路由库。Vue Router是Vue.js官方的路由管理器。 安装Vue Router: npm install vue-r…

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaSc…

js树实现

js树实现

树的基本概念 树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。 树的实现方式 在JavaScript中,树可以通过…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…

js图片轮播的实现

js图片轮播的实现

基础图片轮播实现 使用HTML、CSS和JavaScript实现一个简单的图片轮播效果。HTML部分定义轮播容器和图片,CSS负责样式和过渡效果,JavaScript处理逻辑。 <div cl…

js实现按钮点击

js实现按钮点击

实现按钮点击的JavaScript方法 HTML按钮元素 在HTML中创建按钮元素,为其添加id或class以便JavaScript选择: <button id="myButton">点…