当前位置:   article > 正文

前端实现录音功能 语音录入 弹框录入_前端语音输入

前端语音输入

实现录音功能/语音录入案例


在这里插入图片描述

本案例是弹框录音录入 可以直接作为录音组件使用

使用插件js-audio-recorder可以得到三种录音数据,pcm,wav,mp3 三种数据流
官网api入口.

tip:使用js-audio-recorder报浏览器不支持getUserMedia
因为浏览器安全限制只支持file:,https:,http://localhost,
不能使用http:ip

修改方法在最底下

安装js-audio-recorder

cnpm i js-audio-recorder --s
  • 1

引入到所需页面

import Recorder from "js-audio-recorder";
  • 1

在这里插入图片描述

<style  scoped>
  .pop {
    padding: 0px;
  }
  .pop .popper__arrow {
    display: none;
  }
.tittle {
  /* display: inline-block; */
  color: #ffffff;
  font-weight: bold;
  font-family: "微软雅黑";
  margin-left: 14px;
  font-size: 20px;

  /* height: 40px; */
  /* line-height: 40px; */
}
.content {
  border: none;
  /* text-indent: 2em; */
  margin: 14px auto;
  width: 90%;
  font-family: "微软雅黑";
  color: rgb(112, 112, 112);
  min-height: 340px !important;
  font-size: 18px;
  overflow-x: hidden;
  overflow-y: scroll;
}
.content::-webkit-scrollbar {
  display: none;
}
.top {
  height: 60px;
  line-height: 60px;
  background-color: #2e4e8f;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 5px 5px 0 0;
}
.top .main {
  height: 40px;
  width: 90%;
  display: flex;
  align-items: center;
}
.top .img_c {
  display: inline-block;
  height: 40px;
  width: 40px;
  text-align: center;
  background-color: #ffffff;
  border-radius: 50%;
}
.top .img_c img {
  height: 30px;
}
.imgs {
  /* display: inline-block; */
  height: 40px;
  width: 40px;
  text-align: center;
  background-color: #2e4e8f;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  cursor: pointer;
}
.imgs img {
  height: 30px;
}
.bottom .el-col-8 {
  height: 40px;
  text-align: center;
  display: flex;
  justify-content: center;
  align-content: center;
  line-height: 40px;
}
.bottom {
  margin: 10px 0;
}
.trans {
  height: 150px;
  background-color: rgb(224, 224, 224);
  border-radius: 4px;
  margin: 20px 0;
  position: relative;

}

.transvalue{
  width: 100%;
  height: 90%;
  overflow-x: hidden;
  overflow-y: scroll;
}
.transvalue::-webkit-scrollbar {
  display: none;
}
.el-textarea {
  height: 100%;
}
.el-textarea__inner {
  height: 100%;
  background-color: rgb(224, 224, 224);
}
.txtimg {
  width: 18px;
}
.el-col-12 {
  height: 40px;
  line-height: 40px;
}
.tansimg {
  height: 40px;
  text-align: center;
  line-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tansimg img {
  height: 20px;
}
.cleartext {
  display: inline-block;
  width: 25px;
  height: 25px;
  position: absolute;
  bottom: 5px;
  right: 25px;
}
.cleartext img {
  width: 25px;
}
.close {
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  margin-left: 100px;
  cursor: pointer;
}
.close img {
  width: 20px;
}
.bottom_text {
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
}
.circle-border {
  width: 40px;
  height: 40px;
  padding: 3px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  background: rgb(63,249,220);
  background: linear-gradient(0deg, rgba(63,249,220,0.1) 33%, rgba(63,249,220,1) 100%);
  animation: spin .8s linear 0s infinite;
  cursor: pointer;
}


.circle-core {
  width: 40px;
  height: 40px;
  background-color: #2e4e8f;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  animation: spins .8s linear 0s infinite;

}
@keyframes spin {
  from {
    transform: rotate(0);
  }
  to{
    transform: rotate(359deg);
  }
}
@keyframes spins {
  from {
    transform: rotate(359deg);
  }
  to{
    transform: rotate(0);
  }
}
.warn{
  font-size:12px;
  color: red;
  cursor: pointer;
}
</style>
<template>
  <div class="home">
    <el-popover
      placement="bottom"
      popper-class="pop"
      width="300"
      trigger="manual"
      v-model="transvoice"
    >
      <el-button slot="reference" @click="transvoice = !transvoice"
        >语音识别</el-button
      >
      <div>
        <div class="top">
          <div class="main">
            <div class="img_c"><img src="./img/recorder.png" alt="" /> </div>
            <span class="tittle">语音输入</span>
            <span class="close" @click="closevoice()"
              ><img src="./img/close.png" alt=""
            /></span>
          </div>
        </div>
        <div class="content" id="content">
          <p v-for="(item, index) in textarea" :key="index">{{ item }}</p>
        </div>
        <div class="bottom">
          <el-row>
            <el-col :span="8">
              <span @click="cleartext" class="bottom_text" v-if="isShow"
                >清空</span
              >
            </el-col>
            <el-col :span="8">
            
              <div class="imgs"  @click="handleStart" v-if="isRecorder">
                <img src="./img/recorder1.png" alt="" />
              </div>
             <div class="circle-border" @click="handleStart" v-else>
            <div class="circle-core"><img  style="width: 80%;height: 80%;"  src="./img/01.png" alt=""></div>
              </div>
              <!-- </el-popover> -->
            </el-col>
            <el-col :span="8" v-clipboard:copy="textarea">
              <span class="bottom_text" v-if="isShow">复制</span>
            </el-col>
          </el-row>
        </div>
      </div>
    </el-popover>
  </div>
</template>
<script>
import Recorder from "js-audio-recorder";
import $ from "jquery";
import { post, upload } from "@/request/gAjax";
import ClipboardJS from "clipboard";
import md5 from "js-md5";

export default {
  name: "home",
  data() {
    return {
      textarea: [],
      isStart: false,
      visible: false,
      transvisible: false,
      options: [],
      formData: "",
      filename: "",
      textvalue: "",
      isShow: false,
      totaldata: "",
      transvoice: false,
      index: 0,
      isRecorder:true,
      isShowto:false,
    };
  },
  created() {
  },
  methods: {   
    closevoice() {
      console.log("123");
      this.transvoice = false;
    },
    closetext() {
      this.transvisible = false;
    },
    cleartextarea() {
      this.lantextarea = "";
      this.textvalue = "";
    },
    onCopy() {
      console.log("复制成功");
    },
    cleartext() {
      this.textarea=[]
      // this.textarea.push("请录入");
    },
    handleStart() {
      if (this.isStart == false) {
        this.recorder = new Recorder();
        Recorder.getPermission().then(
          () => {
            console.log("开始录音");
            this.recorder.start();
            // 开始录音
            this.isStart = !this.isStart;
            this.visible = !this.visible;
            this.isRecorder=!this.isRecorder
          },
          (error) => {
            alert(error.message);
            console.log(`${error.name} : ${error.message}`);
          }
        );
      } else {
        this.isStart = !this.isStart;
        this.visible = !this.visible;
            this.isRecorder=!this.isRecorder

        this.uploadRecord();
      }
    },
    handlePause() {
      console.log("暂停录音");
      this.recorder.pause(); // 暂停录音
    },
    handleResume() {
      console.log("恢复录音");
      this.recorder.resume(); // 恢复录音
    },
    handleStop() {
      console.log("停止录音");
      this.recorder.stop(); // 停止录音
    },
    handlePlay() {
      console.log("播放录音");
      this.recorder.play(); // 播放录音

      // 播放时长
      this.timer = setInterval(() => {
        try {
          this.playTime = this.recorder.getPlayTime();
        } catch (error) {
          this.timer = null;
        }
      }, 100);
    },
    handlePausePlay() {
      console.log("暂停播放");
      this.recorder.pausePlay(); // 暂停播放

      // 播放时长
      this.playTime = this.recorder.getPlayTime();
      this.time = null;
    },
    handleResumePlay() {
      console.log("恢复播放");
      this.recorder.resumePlay(); // 恢复播放

      // 播放时长
      this.timer = setInterval(() => {
        try {
          this.playTime = this.recorder.getPlayTime();
        } catch (error) {
          this.timer = null;
        }
      }, 100);
    },
    handleStopPlay() {
      console.log("停止播放");
      this.recorder.stopPlay(); // 停止播放

      // 播放时长
      this.playTime = this.recorder.getPlayTime();
      this.timer = null;
    },
    handleDestroy() {
      console.log("销毁实例");
      this.recorder.destroy(); // 毁实例
      this.timer = null;
    },
    uploadRecord() {
      
      if (this.recorder == null || this.recorder.duration === 0) {
        this.$Message.error("请先录音");
        return false;
      }
      this.recorder.pause(); // 暂停录音
      this.timer = null;
      console.log("上传录音"); // 上传录音
      this.formData = new FormData();
      var blob = this.recorder.getWAVBlob(); //获取wav格式音频数据
      console.log(blob);
      //此处获取到blob对象后需要设置fileName满足当前项目上传需求,其它项目可直接传把blob作为file塞入formData
      var newbolb = new Blob([blob], { type: "audio/wav" });
      this.formData.append("file", blob);
      var _this = this;
      //此处调用语音转文字接口(这里是自己封装的接口 根据自己所需修改)
      upload("*********", this.formData).then((res) => {
        _this.textarea.push(res.data);
        //将返回文字 显示到页面
        _this.isShow = true;
        $("#content").scrollTop($("#content").scrollHeight);
        //滚动到最底部
      });
    },
  },
};
</script>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416

修改方法
1.可以手动修改浏览器
地址栏输入:chrome://flags/#unsafely-treat-insecure-origin-as-secure
在这里插入图片描述
2.如果上线项目有域名的话可以不用修改,如果没有域名让后端或者运维同学将项目和接口部署为https即可

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

闽ICP备14008679号