当前位置:   article > 正文

使用JS实现一个会语音的聊天机器人_javascript语音游戏

javascript语音游戏

我使用的是黑马程序员的API接口(小思同学),在此鸣谢,我们今天从第一步到最后一步解析机器人的制作,该API接口免费,请大家妥善处置!!

可以实现实时交互文字以及语音聊天功能

一.准备工作

我们需要准备俩个接口和API文档在代码中已经书写

二.HTML架构

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>聊天</title>
  8. <link rel="stylesheet" href="./css/index.css">
  9. <script src="./js/jquery-1.12.4.min.js"></script>
  10. <script src="./js/index.js"></script>
  11. </head>
  12. <body>
  13. <div class="max-box">
  14. <!-- 顶部信息栏 -->
  15. <div class="information-box">
  16. <h2>小思同学</h2>
  17. </div>
  18. <!-- 内容栏 -->
  19. <div class="neirong">
  20. <ul class="left-news">
  21. <li class="one-li"><img src="./img/694228115d1c56b2c1f24e86a6e005e9.jpeg" alt=""></li>
  22. <li class="two-li">你好
  23. </li>
  24. </ul>
  25. </div>
  26. <!-- 顶部消息栏 -->
  27. <div class="news-box">
  28. <input type="text" name="" id="">
  29. <button>S</button>
  30. </div>
  31. </div>
  32. <audio src="" id="voice" autoplay style="display: none;"></audio>
  33. </body>
  34. </html>

三.CSS

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. font-family: '幼圆';
  5. box-sizing: border-box;
  6. }
  7. img{
  8. width: 100%;
  9. height: 100%;
  10. }
  11. ul{
  12. list-style: none;
  13. }
  14. .max-box{
  15. position: relative;
  16. width: 100%;
  17. height: 100vh;
  18. }
  19. .information-box{
  20. position: sticky;
  21. top: 0;
  22. width: 100%;
  23. height: 10vh;
  24. background-color: #333333;
  25. z-index: 99;
  26. }
  27. .information-box h2{
  28. color: #fff;
  29. line-height: 10vh;
  30. text-align: center;
  31. }
  32. .news-box{
  33. display: flex;
  34. position: fixed;
  35. bottom: 0;
  36. width: 100%;
  37. height: 10vh;
  38. background-color: #333333;
  39. border: 2px double #000;
  40. padding: 1vh;
  41. }
  42. .news-box input{
  43. flex: 8;
  44. outline: none;
  45. font-size: 30px;
  46. }
  47. .news-box button{
  48. flex: 2;
  49. background-color: #75d45d;
  50. color: #fff;
  51. font-size: 30px;
  52. font-weight: bold;
  53. border: none;
  54. cursor: pointer;
  55. }
  56. .left-news,
  57. .right-news{
  58. position: relative;
  59. display: block;
  60. width: 100%;
  61. height: 12vh;
  62. overflow: hidden;
  63. text-overflow: ellipsis;
  64. background-color: transparent;
  65. padding: 1vh;
  66. margin-bottom: 6vh;
  67. }
  68. .neirong ul:last-of-type{
  69. margin-bottom: 0;
  70. }
  71. .left-news .one-li,
  72. .right-news .one-li{
  73. display: inline-block;
  74. width: 10vh;
  75. height: 10vh;
  76. border-radius: 10px;
  77. overflow: hidden;
  78. }
  79. .left-news .two-li,
  80. .right-news .two-li{
  81. display: inline-block;
  82. position: absolute;
  83. top: 0;
  84. height: 10vh;
  85. top: 0;
  86. background-color: #eeeeee;
  87. border-radius: 10px;
  88. text-align: center;
  89. line-height: 10vh;
  90. font-size: 20px;
  91. font-weight: bold;
  92. margin-left: 2vh;
  93. margin-right: 2vh;
  94. margin-top: 1vh;
  95. padding-left: 2vh;
  96. padding-right: 2vh;
  97. }
  98. .right-news .one-li{
  99. position: absolute;
  100. right: 1vh;
  101. }
  102. .right-news .two-li{
  103. position: absolute;
  104. right: 12vh;
  105. background-color: #0f0f0f;
  106. color: #fff;
  107. }
  108. .neirong{
  109. padding-bottom: 10vh;
  110. background-color: #fff;
  111. }

四.JS-jQuery-Ajax

  1. $(function(){
  2. //封装用户输入函数
  3. function getnews(){
  4. //获取用户输入的值
  5. var text = $("input").val().trim();
  6. //判断输入值是否为空
  7. if(text.length <= 0){
  8. return $("input").val("");
  9. }else{
  10. //不为空追加数据
  11. $(".neirong").append('<ul class="right-news"><li class="two-li">' + text + '</li><li class="one-li"><img src="./img/a074b25dcd61d56154056d231cf20561.jpeg" alt=""></li></ul>');
  12. $("input").val("");
  13. getMsg(text);
  14. }
  15. }
  16. //绑定点击事件
  17. $("button").on("click",function(){
  18. getnews();
  19. })
  20. //绑定回车键盘事件
  21. $(document).on('keyup',function(event){
  22. if(event.keyCode === 13){
  23. getnews();
  24. }
  25. })
  26. //获取聊天机器人的消息
  27. function getMsg(text){ //封装函数
  28. $.ajax({
  29. method:'GET',
  30. url:'http://www.liulongbin.top:3006/api/robot',
  31. data:{
  32. spoken:text
  33. },
  34. success:function(res){
  35. // console.log(res);
  36. if(res.message === 'success'){
  37. var msg = res.data.info.text;
  38. $(".neirong").append('<ul class="left-news"><li class="one-li"><img src="./img/694228115d1c56b2c1f24e86a6e005e9.jpeg" alt=""></li><li class="two-li">' + msg + '</li></ul>');
  39. getVoice(msg);
  40. }
  41. }
  42. })
  43. //文字转音频
  44. function getVoice(msg){
  45. $.ajax({
  46. method:'GET',
  47. url:'http://www.liulongbin.top:3006/api/synthesize',
  48. data:{
  49. text:msg
  50. },
  51. success:function(res){
  52. // console.log(res);
  53. if(res.status === 200){
  54. $("audio").attr('src',res.voiceUrl);
  55. }
  56. }
  57. })
  58. }
  59. }
  60. })

API接口由黑马提供

学习前端。关注小蜗

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/693512
推荐阅读
相关标签
  

闽ICP备14008679号