录音、录音后播放、录音后上传

<template>
  <div style="padding: 20px;">
    <h3>录音上传</h3>

    <div style="font-size:14px">
      <h3>录音时长:{{0+ recorder && 0+recorder.duration.toFixed(2) }}</h3>
      <br />
      <button type="primary" @click="handleStart">开始录音</button>
      <button type="info" @click="handlePause">暂停录音</button>
      <button type="success" @click="handleResume">继续录音</button>
      <button type="warning" @click="handleStop">停止录音</button>
      <br />
      <br />
      <h3>
        播放时长:{{
          recorder &&
            (playTime > recorder.duration
              ? 0+recorder.duration.toFixed(2)
              : 0+playTime.toFixed(2))
        }}
      </h3>
      <br />
      <button type="primary" @click="handlePlay">播放录音</button>
      <button type="info" @click="handlePausePlay">暂停播放</button>
      <button type="success" @click="handleResumePlay">继续播放</button>
      <button type="warning" @click="handleStopPlay">停止播放</button>
      <button type="error" @click="handleDestroy">销毁录音</button>
      <button type="primary" @click="uploadRecord">上传</button>
      <dProgress :percentage="40" pcolor="orange" />
    </div>
  </div>
</template>

<script>
import Recorder from 'js-audio-recorder'
import dProgress from '../../../components/Progress.vue';
export default {
  data() {
    return {
      recorder: null,
      playTime: 0,
      timer: null,
      src: null
    }
  },
  created() {
    this.recorder = new Recorder()
  },
  components:{
    dProgress
  },
  methods: {
    // 开始录音
    handleStart() {
      this.recorder = new Recorder()
      Recorder.getPermission().then(() => {
        console.log('开始录音')
        this.recorder.start() // 开始录音
      }, (error) => {
        this.$message({
          message: '请先允许该网页使用麦克风',
          type: 'info'
        })
        console.log(`${error.name} : ${error.message}`)
      })
    },
    handlePause() {
      console.log('暂停录音')
      this.recorder.pause() // 暂停录音
    },
    handleResume() {
      console.log('恢复录音')
      this.recorder.resume() // 恢复录音
    },
    handleStop() {
      console.log('停止录音')
      this.recorder.stop() // 停止录音
    },
    handlePlay() {
      console.log('播放录音')
      console.log(this.recorder)
      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({
          message: '请先录音',
          type: 'error'
        })
        return false
      }
      this.recorder.pause() // 暂停录音
      this.timer = null
      console.log('上传录音')// 上传录音
      const formData = new FormData()
      const blob = this.recorder.getWAVBlob()// 获取wav格式音频数据
      // 此处获取到blob对象后需要设置fileName满足当前项目上传需求,其它项目可直接传把blob作为file塞入formData
      const newbolb = new Blob([blob], { type: 'audio/wav' })
      const fileOfBlob = new File([newbolb], new Date().getTime() + '.wav')
      formData.append('file', fileOfBlob)
      const url = window.URL.createObjectURL(fileOfBlob)
      this.src = url
      // const axios = require('axios')
      // axios.post(url, formData).then(res => {
      //   console.log(res.data.data[0].url)
      // })
    }
  }
}
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp是一种基于Vue.js框架的跨平台应用开发框架,可以用于开发Android和iOS应用。要实现录音上传功能,可以使用uniapp的原生扩展插件或使用uniapp提供的Web API。 在uniapp中,可以使用uni.mediaRecorder API来进行录音操作。首先,需要在manifest.json文件中配置录音权限,确保应用有录音的权限。然后在页面中使用uni.mediaRecorder创建一个录音对象,并调用开始录音函数开始录音录音结束后,可以调用uni.uploadFile API将录音文件上传到服务器。 具体的步骤如下: 1. 配置录音权限:在manifest.json文件中的permissions节点中添加"record"权限。 2. 引入uni.mediaRecorder API:在页面的<script>标签中引入uni.mediaRecorder API。 3. 创建录音对象:在需要进行录音的页面中,使用uni.mediaRecorder.createMediaRecorder()函数创建一个录音对象。 4. 开始录音:调用录音对象的start()函数开始录音。 5. 结束录音录音完成后,调用录音对象的stop()函数停止录音。 6. 上传录音文件:使用uni.uploadFile API将录音文件上传到服务器。调用uni.uploadFile函数,传入录音文件的路径、服务器地址和上传成功后的回调函数。 值得注意的是,uni.mediaRecorder API在不同平台上的实现可能会有所不同,因此需要根据具体的开发需求和目标平台进行相关调整。 总结起来,要在uniapp中实现录音上传功能,需要配置录音权限,在页面中引入uni.mediaRecorder API,创建录音对象并进行录音操作,最后使用uni.uploadFile API将录音文件上传到服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值