Web Speech API的语音识别技术

2024-03-13 18:36
文章标签 技术 web api 语音 识别 speech

本文主要是介绍Web Speech API的语音识别技术,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SpeechSynthesis对象

这是一个实验性技术

目前兼容性如图:

pc端几乎兼容,移动端部分不兼容

请添加图片描述

网页语音 API 的SpeechSynthesis 接口是语音服务的控制接口;

它可以用于获取设备上关于可用的合成声音的信息,开始、暂停语音,或除此之外的其他命令。

  • SpeechSynthesis 也从它的父接口继承属性,EventTarget.

  • SpeechSynthesis.paused 只读
    当SpeechSynthesis 处于暂停状态时, Boolean值返回 true 。

  • SpeechSynthesis.pending只读
    当语音播放队列到目前为止保持没有说完的语音时, Boolean值返回 true 。

  • SpeechSynthesis.speaking只读
    当语音谈话正在进行的时候,即使SpeechSynthesis处于暂停状态, Boolean返回 true 。

事件操作

  • SpeechSynthesis.onvoiceschanged (en-US)
    当由SpeechSynthesis.getVoices()方法返回的SpeechSynthesisVoice (en-US)列表改变时触发。

方法

SpeechSynthesis 也从它的父接口继承方法, EventTarget.

  • SpeechSynthesis.cancel() (en-US)
    移除所有语音谈话队列中的谈话。

  • SpeechSynthesis.getVoices()
    返回当前设备所有可用声音的 SpeechSynthesisVoice (en-US)列表。

  • SpeechSynthesis.pause() (en-US)
    把 SpeechSynthesis 对象置为暂停状态。

  • SpeechSynthesis.resume() (en-US)
    把 SpeechSynthesis 对象置为一个非暂停状态:如果已经暂停了则继续。

  • SpeechSynthesis.speak() (en-US)
    添加一个 utterance到语音谈话队列;它将会在其他语音谈话播放完之后播放。

示例

用 window.speechSynthesis抓取关于语音播放控制器

在定义了一些必要的变量后,用 SpeechSynthesis.getVoices()获取了一列可用的声音并且用它们生成一列可选表单,这样用户能够选择他们想要的声音。

inputForm.onsubmit 的内部操作中,我们用preventDefault()阻止了表单的提交,创建了一个从文本框获取文本的新SpeechSynthesisUtterance (en-US)实例,在元素可选的声音设置成语音谈话的 voice 属性,然后通过SpeechSynthesis.speak() (en-US)方法开始语音播放。

var synth = window.speechSynthesis;var inputForm = document.querySelector("form");
var inputTxt = document.querySelector(".txt");
var voiceSelect = document.querySelector("select");var pitch = document.querySelector("#pitch");
var pitchValue = document.querySelector(".pitch-value");
var rate = document.querySelector("#rate");
var rateValue = document.querySelector(".rate-value");var voices = [];function populateVoiceList() {voices = synth.getVoices();for (i = 0; i < voices.length; i++) {var option = document.createElement("option");option.textContent = voices[i].name + " (" + voices[i].lang + ")";if (voices[i].default) {option.textContent += " -- DEFAULT";}option.setAttribute("data-lang", voices[i].lang);option.setAttribute("data-name", voices[i].name);voiceSelect.appendChild(option);}
}populateVoiceList();
if (speechSynthesis.onvoiceschanged !== undefined) {speechSynthesis.onvoiceschanged = populateVoiceList;
}inputForm.onsubmit = function (event) {event.preventDefault();var utterThis = new SpeechSynthesisUtterance(inputTxt.value);var selectedOption = voiceSelect.selectedOptions[0].getAttribute("data-name");for (i = 0; i < voices.length; i++) {if (voices[i].name === selectedOption) {utterThis.voice = voices[i];}}utterThis.pitch = pitch.value;utterThis.rate = rate.value;synth.speak(utterThis);inputTxt.blur();
};

属性:

paused

  • SpeechSynthesis 接口的只读属性 paused 是一个 Boolean值,当SpeechSynthesis对象处于暂停状态时,返回true ,否则返回 false。

  • 它能被设置为 暂停状态即使当前并没有语音在播放队列中。如果utterances被添加到语音播放队列,队列中的语音并不会播放直到使用 SpeechSynthesis.resume() (en-US)使SpeechSynthesis对象处于非暂停状态。

语法

var amIPaused = speechSynthesisInstance.paused;
Value
一个Boolean (en-US)。

示例

var synth = window.speechSynthesis;synth.pause();var amIPaused = synth.paused; // 将返回 true

pending

  • 只读属性 SpeechSynthesisinterface是一个布尔值,返回 true如果话语队列包含尚未说出的话语。


  • 布尔值。

示例

const synth = window.speechSynthesis;const utterance1 = new SpeechSynthesisUtterance("helloWorld.",
);
const utterance2 = new SpeechSynthesisUtterance("helloWorld2.",
);synth.speak(utterance1);
synth.speak(utterance2);const amIPending = synth.pending; // 如果话语1仍在说话并且话语2在队列中,则将返回trues in the queue

speaking

  • 只读属性 SpeechSynthesisinterface是一个布尔值,返回 true如果话语当前正在被说出的过程中-甚至 如果SpeechSynthesis在 paused州。


  • 布尔值。

示例

const synth = window.speechSynthesis;const utterance1 = new SpeechSynthesisUtterance("话语1.",
);
const utterance2 = new SpeechSynthesisUtterance("话语2.",
);synth.speak(utterance1);
synth.speak(utterance2);const amISpeaking = synth.speaking; // 如果话语1或话语2当前正在说话,则将返回true

方法详情

cancel方法

  • 从话语队列中移除所有话语。

​ 如果正在说话,说话将立即停止。

语法

实例.cancel()

参数

返回值

无(undefined)。

示例

const synth = window.speechSynthesis;const utterance1 = new SpeechSynthesisUtterance("话语1.",
);
const utterance2 = new SpeechSynthesisUtterance("话语2.",
);synth.speak(utterance1);
synth.speak(utterance2);synth.cancel(); //话语1立即停止,并且两者都从队列中删除

getVoices方法

  • SpeechSynthesis接口返回一个 SpeechSynthesisVoice对象表示所有可用的声音上 当前设备

语法

实例.getVoices()

参数

返回值

示例

function populateVoiceList() {if (typeof speechSynthesis === "undefined") {return;}const voices = speechSynthesis.getVoices();for (let i = 0; i < voices.length; i++) {const option = document.createElement("option");option.textContent = `${voices[i].name} (${voices[i].lang})`;if (voices[i].default) {option.textContent += " — DEFAULT";}option.setAttribute("data-lang", voices[i].lang);option.setAttribute("data-name", voices[i].name);document.getElementById("voiceSelect").appendChild(option);}
}populateVoiceList();
if (typeof speechSynthesis !== "undefined" &&speechSynthesis.onvoiceschanged !== undefined
) {speechSynthesis.onvoiceschanged = populateVoiceList;
}

pause方法

  • SpeechSynthesis对象置于暂停状态。

语法

实例.pause()

参数

返回值

SpeechSynthesisVoice对象的列表(数组)。

示例

const synth = window.speechSynthesis;const utterance1 = new SpeechSynthesisUtterance("话语1.",
);
const utterance2 = new SpeechSynthesisUtterance("话语2.",
);
synth.speak(utterance1);
synth.speak(utterance2);synth.pause(); // 暂停说话

resume方法

  • 如果它已经暂停,则恢复它。

语法

实例.resume()

参数

返回值

示例

let synth = window.speechSynthesis;const utterance1 = new SpeechSynthesisUtterance("话语1.",
);
const utterance2 = new SpeechSynthesisUtterance("话语2.",
);synth.speak(utterance1);
synth.speak(utterance2);synth.pause(); 
synth.resume(); //恢复暂停

voiceschanged事件

  • Web Speech API的voiceschanged事件在由SpeechSynthesisVoice方法返回的SpeechSynthesis.getVoices()对象的列表发生更改时触发(在voiceschanged事件触发时)。

语法

在类似addEventListener()的方法中使用事件名称,或者设置事件处理程序属性。

addEventListener("voiceschanged", (event) => {});onvoiceschanged = (event) => {};

事件类型

没有添加属性的泛型Event。

示例

这可以用来重新填充一个声音列表,用户可以在事件触发时从中选择。您可以在voiceschanged方法中使用addEventListener事件:

const synth = window.speechSynthesis;synth.addEventListener("voiceschanged", () => {const voices = synth.getVoices();for (let i = 0; i < voices.length; i++) {const option = document.createElement("option");option.textContent = `${voices[i].name} (${voices[i].lang})`;option.setAttribute("data-lang", voices[i].lang);option.setAttribute("data-name", voices[i].name);voiceSelect.appendChild(option);}
});

或者使用onvoiceschanged事件处理程序属性:

const synth = window.speechSynthesis;
synth.onvoiceschanged = () => {const voices = synth.getVoices();for (let i = 0; i < voices.length; i++) {const option = document.createElement("option");option.textContent = `${voices[i].name} (${voices[i].lang})`;option.setAttribute("data-lang", voices[i].lang);option.setAttribute("data-name", voices[i].name);voiceSelect.appendChild(option);}
};

这篇关于Web Speech API的语音识别技术的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/805812

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

使用Python实现文本转语音(TTS)并播放音频

《使用Python实现文本转语音(TTS)并播放音频》在开发涉及语音交互或需要语音提示的应用时,文本转语音(TTS)技术是一个非常实用的工具,下面我们来看看如何使用gTTS和playsound库将文本... 目录什么是 gTTS 和 playsound安装依赖库实现步骤 1. 导入库2. 定义文本和语言 3

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

Pytorch微调BERT实现命名实体识别

《Pytorch微调BERT实现命名实体识别》命名实体识别(NER)是自然语言处理(NLP)中的一项关键任务,它涉及识别和分类文本中的关键实体,BERT是一种强大的语言表示模型,在各种NLP任务中显著... 目录环境准备加载预训练BERT模型准备数据集标记与对齐微调 BERT最后总结环境准备在继续之前,确

讯飞webapi语音识别接口调用示例代码(python)

《讯飞webapi语音识别接口调用示例代码(python)》:本文主要介绍如何使用Python3调用讯飞WebAPI语音识别接口,重点解决了在处理语音识别结果时判断是否为最后一帧的问题,通过运行代... 目录前言一、环境二、引入库三、代码实例四、运行结果五、总结前言基于python3 讯飞webAPI语音