首页 > 其他分享 >html JS 语音识别

html JS 语音识别

时间:2024-08-19 16:55:02浏览次数:6  
标签:false JS html 语音 var 识别 event recognition 页面

1、语音识别的过程

语音识别涉及三个过程:首先,需要设备的麦克风接收这段语音;其次,语音识别服务器会根据一系列语法 (基本上,语法是你希望在具体的应用中能够识别出来的词汇) 来检查这段语音;最后,当一个单词或者短语被成功识别后,结果会以文本字符串的形式返回 (结果可以有多个),以及更多的行为可以设置被触发。

示例代码:
您如果想直接尝试的话可以先复制下面代码运气起来试试效果。

操作方法:将代码运行在浏览器,点击屏幕,允许麦克风权限,然后出说其中一种颜色。

结果1:识别成功,但是没听清楚您说的什么,页面不会有任何变化。

结果2:识别成功,页面背景切换为您说的那种颜色。

结果3:识别失败,失败原因将在页面底部展示。

 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width" />
    <title></title>
    <style>
      body,
      html {
        margin: 0;
      }
 
      html {
        height: 100%;
		background-color:white;
      }
 
      body {
        height: inherit;
        overflow: hidden;
        max-width: 800px;
        margin: 0 auto;
      }
 
      h1,
      p {
        font-family: sans-serif;
        text-align: center;
        padding: 20px;
      }
 
      div {
        height: 100px;
        overflow: auto;
        position: absolute;
        bottom: 0px;
        right: 0;
        left: 0;
        background-color: rgba(255, 255, 255, 0.2);
      }
 
      ul {
        margin: 0;
      }
 
      .hints span {
        text-shadow: 0px 0px 6px rgba(255, 255, 255, 0.7);
      }
    </style>
  </head>
  <body>
    <h1>语音识别转换器</h1>
    <p class="hints"></p>
    <div>
      <p class="output"><em>识别进度展示处</em></p>
    </div>
  </body>
  <script>
    var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
    var SpeechGrammarList = SpeechGrammarList || window.webkitSpeechGrammarList
    var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent
 
    var colors = [
      'aqua',
      'azure',
      'beige',
      'bisque',
      'black',
      'blue',
      'brown',
      'chocolate',
      'coral',
      'crimson',
      'cyan',
      'fuchsia',
      'ghostwhite',
      'gold',
      'goldenrod',
      'gray',
      'green',
      'indigo',
      'ivory',
      'khaki',
      'lavender',
      'lime',
      'linen',
      'magenta',
      'maroon',
      'moccasin',
      'navy',
      'olive',
      'orange',
      'orchid',
      'peru',
      'pink',
      'plum',
      'purple',
      'red',
      'salmon',
      'sienna',
      'silver',
      'snow',
      'tan',
      'teal',
      'thistle',
      'tomato',
      'turquoise',
      'violet',
      'white',
      'yellow',
    ]
 
    var recognition = new SpeechRecognition()
    if (SpeechGrammarList) {
      var speechRecognitionList = new SpeechGrammarList()
      var grammar = '#JSGF V1.0; grammar colors; public <color> = ' + colors.join(' | ') + ' ;'
      speechRecognitionList.addFromString(grammar, 1)
      recognition.grammars = speechRecognitionList
    }
    recognition.continuous = false
    recognition.lang = 'en-US'
    recognition.interimResults = false
    recognition.maxAlternatives = 1
 
    var bg = document.querySelector('html')
    var hints = document.querySelector('.hints')
    var diagnostic = document.querySelector('.output')
    var colorHTML = ''
    colors.forEach(function (v, i, a) {
      colorHTML += '<span style="background-color:' + v + ';"> ' + v + ' </span>'
    })
    hints.innerHTML = '点击页面,然后说出一种颜色来更改页面背景色' + '</br>' + colorHTML + '.'
 
    let flag = false
    document.body.onclick = function () {
      if (flag) return
      flag = true
      recognition.start()
      console.log('正识别中...')
      diagnostic.textContent = '正识别中...'
    }
 
    recognition.onresult = function (event) {
      var color = event.results[0][0].transcript;
      bg.style.backgroundColor = ""+color.replace('.','');
      flag = false;
      // console.log('Confidence: ' + event.results[0][0].confidence)
      console.log('识别成功,结果是:', color);
      diagnostic.textContent = '识别成功,结果是: ' + color + '.';
    }
 
    recognition.onspeechend = function () {
      recognition.stop()
      flag = false
      console.log('识别结束')
    }
 
    recognition.onnomatch = function (event) {
      flag = false
      console.log('识别成功,但是没有认出您说的颜色')
      diagnostic.textContent = '识别成功,但是没有认出您说的颜色'
    }
 
    recognition.onerror = function (event) {
      flag = false
      let msg = event.error == 'not-allowed' ? '没有麦克风权限' : event.error
      console.log('识别错误,原因是:', msg)
      diagnostic.textContent = '识别错误,原因是:' + msg
    }
  </script>
</html>

  

失败可能原因:

1、您没有开启当前页面的麦克风功能。

2、您的电脑或者手机没有麦克风功能。

解决方法:

1、您可以重新打开该页面,然后开启当前页面的麦克风权限。

2、佩戴耳机,利用耳机的麦克风功能

 

二、页面上只有文字显示,页面效果与示例图不一样

可能原因:

1、您使用的浏览器不支持语音识别。

2、代码有误。

解决方法:

1、pc端使用Chrome、Edge、Safari浏览器。手机端使用Safari、Samsung等浏览器或者在微信内部利用WebView打开链接进行访问。

2、检查代码。

三、建议使用Edge浏览器,成功率90%以上

————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/nibabaoo/article/details/138187630

 

标签:false,JS,html,语音,var,识别,event,recognition,页面
From: https://www.cnblogs.com/wgscd/p/18367640

相关文章

  • jsoncpp 介绍
    前言全局说明VisualStudio2013jsoncpp编译一、说明环境:Windows7旗舰版VisualStudio2013二、常用的JSON解析库有:(ChatGPT)nlohmann/json(一个现代C++JSON库)https://github.com/nlohmann/json/releasesJsonCpp(一个常见的JSON解析库)https://github.com......
  • HTML
    titlepublish01-HTML标签:图片标签trueimg标签介绍<imgsrc="图片的URL"/>能插入的图片类型能够插入的图片类型是:jpg(jpeg)、gif、png、bmp等。不能往网页中插入的图片格式是:psd、ai等。img标签的src属性这里涉及到图片的一个属性:src属性:指图片的路径......
  • HTML5服装电商网上商城模板源码
    文章目录1.设计来源1.1主界面1.2购物车界面1.3电子产品界面1.4商品详情界面1.5联系我们界面1.6各种标签演示界面2.效果和源码2.1动态效果2.2源代码源码下载万套模板,程序开发,在线开发,在线沟通         【博主推荐】:前些天发现了一个巨牛的人工智能......
  • 经典记忆卡片游戏html代码
    记忆卡片游戏是一款简单而富有挑战性的经典游戏,旨在锻炼玩家的记忆力和观察力。游戏通常由一组图案相同的卡片组成,玩家需要通过翻转卡片找到匹配的对。每当找到一对匹配的卡片时,玩家将获得一定的分数或奖励,游戏结束时,分数最高者获胜。无论是与朋友竞技,还是单独训练,这款游戏都适合......
  • el-table使用sortablejs推拽排序卡顿问题解决
    使用sortablejs拖拽el-table排序时,对于纯文本表格,正常使用即可,不会卡顿initSort(){consttbody=document.querySelector('.el-table__body-wrappertbody')const_this=thisSortable.create(tbody,{draggable:'.el-table__row',......
  • MySQL中处理JSON数据:大数据分析的新方向
    1.简介1.1.概述在MySQL中处理JSON数据的能力是在MySQL5.7版本中引入的,并在后续的版本中不断得到增强。这使得MySQL能够直接操作和查询JSON格式的数据,极大地扩展了其处理复杂数据结构的能力。1.2.主要特点灵活性与可扩展性:JSON允许开发者存储不规则和嵌套的数据结......
  • HTML5+CSS3学习笔记补充——移动端网页+Bootstrap框架
    移动端网页和Bootstrap框架1.视口:用来约束HTML尺寸<!--视口标签是HTML骨架默认生成的设置网页宽度与逻辑分辨率(即设备)宽度一致--><metaname="viewport"content="width=device-width,initial-scale=1.0">2.二倍图:防止设计稿图片在高分辨率屏幕下模糊失真3.......
  • ZoneJs 源码解析
    ZoneJs源码解析ZoneJs是什么,它能干什么,它是怎么做到的?Zone是为js的执行提供了一个共享的数据上下文。为js函数执行维护了一个逻辑上的调用栈。同时提供了对于函数执行方法的拦截,在函数执行前后,添加一些通用的逻辑(例如日志,异常处理)。统一的任务模型,提供对于宏任务/微任务/......
  • 易优CMS插件html.php页面缓存配置
    插件html.php页面缓存配置作用于插件前台,指定需要缓存的页面,这只在运营模式下才有效。参数规则:mca:weapp_控制器_操作名filename:生成在/data/runtime目录下的指定路径,建议参考以下p:当前url控制器的操作方法传入的全部参数变量名cache:页面缓存有效时间,单位是秒案例:假设......
  • 上传图片js
    <html><body><scriptsrc="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>functionchangImg(e){for(vari=0;i<e.target.files.length;i++){......