最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue使用js-audio-recorder實(shí)現(xiàn)錄音功能

 更新時間:2023年12月05日 11:36:33   作者:學(xué)而時習(xí)之不亦說乎。  
這篇文章主要為大家詳細(xì)介紹了vue如何使用js-audio-recorder實(shí)現(xiàn)錄音功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

前言

最近項(xiàng)目中需要實(shí)現(xiàn)一個錄音上傳功能,用于語音評論可以上錄音。

下載插件:

npm i js-audio-recorder

完整代碼

<template>
  <div style="padding: 20px;">
    <h3>錄音上傳</h3>
 
    <div style="font-size:14px">
      <h3>錄音時長:{{ recorder && recorder.duration.toFixed(4) }}</h3>
      <br />
      <el-button type="primary" @click="handleStart">開始錄音</el-button>
      <el-button type="info" @click="handlePause">暫停錄音</el-button>
      <el-button type="success" @click="handleResume">繼續(xù)錄音</el-button>
      <el-button type="warning" @click="handleStop">停止錄音</el-button>
      <br />
      <br />
      <h3>
        播放時長:{{
          recorder &&
            (playTime > recorder.duration
              ? recorder.duration.toFixed(4)
              : playTime.toFixed(4))
        }}
      </h3>
      <br />
      <el-button type="primary" @click="handlePlay">播放錄音</el-button>
      <el-button type="info" @click="handlePausePlay">暫停播放</el-button>
      <el-button type="success" @click="handleResumePlay">繼續(xù)播放</el-button>
      <el-button type="warning" @click="handleStopPlay">停止播放</el-button>
      <el-button type="error" @click="handleDestroy">銷毀錄音</el-button>
      <el-button type="primary" @click="uploadRecord">上傳</el-button>
    </div>
  </div>
</template>
 
<script>
import Recorder from 'js-audio-recorder'
 
export default {
  data() {
    return {
      recorder: null,
      playTime: 0,
      timer: null,
      src: null
    }
  },
  created() {
    this.recorder = new Recorder()
  },
  methods: {
    // 開始錄音
    handleStart() {
      this.recorder = new Recorder()
      Recorder.getPermission().then(() => {
        console.log('開始錄音')
        this.recorder.start() // 開始錄音
      }, (error) => {
        this.$message({
          message: '請先允許該網(wǎng)頁使用麥克風(fēng)',
          type: 'info'
        })
        console.log(`${error.name} : ${error.message}`)
      })
    },
    handlePause() {
      console.log('暫停錄音')
      this.recorder.pause() // 暫停錄音
    },
    handleResume() {
      console.log('恢復(fù)錄音')
      this.recorder.resume() // 恢復(fù)錄音
    },
    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('恢復(fù)播放')
      this.recorder.resumePlay() // 恢復(fù)播放
 
      // 播放時長
      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('銷毀實(shí)例')
      this.recorder.destroy() // 毀實(shí)例
      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格式音頻數(shù)據(jù)
      // 此處獲取到blob對象后需要設(shè)置fileName滿足當(dāng)前項(xiàng)目上傳需求,其它項(xiàng)目可直接傳把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>

播放通過audio標(biāo)簽控制,可以上傳到公司服務(wù)器獲取線上地址,還可以通過blob對象獲取到播放url

  const blob = this.recorder.getWAVBlob()
  this.url = window.URL.createObjectURL(blob)

注意事項(xiàng)

注意,調(diào)試環(huán)境這里會報錯,所以開始解決報錯問題:

報錯:error:瀏覽器不支持getUserMedia !

其實(shí)這是因?yàn)闉g覽器不支持http:IP開頭的路徑,認(rèn)為這個路徑不安全

瀏覽器只支持file:,https:,http://localhost,

解決辦法:

chrome瀏覽器

地址欄輸入:chrome://flags/#unsafely-treat-insecure-origin-as-secure

輸入你的本地網(wǎng)址,改為enabled,選擇重啟瀏覽器按鈕【生產(chǎn)環(huán)境當(dāng)中由于是使用域名進(jìn)行訪問,所以就不會報錯?!?/p>

然后就OK了

總結(jié)

1.錄音時長duration是recorder的屬性,可以實(shí)時獲??;但播放時長需要通過方法getPlayTime()獲取,播放時不能實(shí)時改變,此處我用了個100ms延遲的定時器假裝實(shí)時獲取,如果有更好的辦法,歡迎指教。

2.getWAVBlob()獲取錄音數(shù)據(jù)的方法獲取的時blob對象,當(dāng)前項(xiàng)目中需要驗(yàn)證fileName,所以需要把blob轉(zhuǎn)成file,改變fileName上傳。

3.官網(wǎng)提供的demo中還有波形圖,可以參考。

官網(wǎng)地址:https://recorder-api.zhuyuntao.cn/

官網(wǎng)項(xiàng)目演示地址:https://recorder.zhuyuntao.cn/

更新

客戶說錄音文件太大,20s就有2M左右,需要壓縮。

查看文檔降低采樣位數(shù),采樣率,單聲道可以降低音頻質(zhì)量,測試20s大概只有200k左右,只需獲取record對象時申明即可。

this.recorder = new Recorder({
                    sampleBits: 8, // 采樣位數(shù),支持 8 或 16,默認(rèn)是16
                    sampleRate: 11025, // 采樣率,支持 11025、16000、22050、24000、44100、48000,根據(jù)瀏覽器默認(rèn)值,我的chrome是48000
                    numChannels: 1 // 聲道,支持 1 或 2, 默認(rèn)是1
                    // compiling: false,(0.x版本中生效,1.x增加中)  // 是否邊錄邊轉(zhuǎn)換,默認(rèn)是false
                })

到此這篇關(guān)于vue使用js-audio-recorder實(shí)現(xiàn)錄音功能的文章就介紹到這了,更多相關(guān)vue錄音內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • ESLint在Vue3?+?TypeScript中的配置與使用方法

    ESLint在Vue3?+?TypeScript中的配置與使用方法

    在Vue項(xiàng)目中配置ESLint,可以確保代碼風(fēng)格的一致性和代碼質(zhì)量,這篇文章主要介紹了ESLint在Vue3+TypeScript中配置與使用方法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-12-12
  • vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄

    vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄

    這篇文章主要介紹了vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 淺談vue2 單頁面如何設(shè)置網(wǎng)頁title

    淺談vue2 單頁面如何設(shè)置網(wǎng)頁title

    這篇文章主要介紹了淺談vue2 單頁面如何設(shè)置網(wǎng)頁title,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Uniapp圖片上傳的兩種實(shí)現(xiàn)方式詳解

    Uniapp圖片上傳的兩種實(shí)現(xiàn)方式詳解

    UniApp是一個跨平臺的前端框架,它允許開發(fā)者使用一套代碼構(gòu)建同時運(yùn)行在微信小程序、H5、iOS、Android等多個平臺的應(yīng)用,在UniApp中,上傳圖片通常涉及到使用其提供的File?API或者第三方插件來進(jìn)行操作,所以本文給大家介紹了Uniapp圖片上傳的兩種實(shí)現(xiàn)方式
    2025-10-10
  • vue3中覆蓋組件樣式的方法小結(jié)

    vue3中覆蓋組件樣式的方法小結(jié)

    在 Vue 3 中,覆蓋組件樣式的方法與 Vue 2 相似,但由于 Vue 3 更多地依賴于 CSS 的模塊化,一些最佳實(shí)踐可能會有所不同,下面就跟隨小編一起來了解一下吧
    2024-04-04
  • Vue.JS入門教程之事件監(jiān)聽

    Vue.JS入門教程之事件監(jiān)聽

    這篇文章主要為大家詳細(xì)介紹了Vue.JS入門教程之事件監(jiān)聽,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 詳解vue中在父組件點(diǎn)擊按鈕觸發(fā)子組件的事件

    詳解vue中在父組件點(diǎn)擊按鈕觸發(fā)子組件的事件

    這篇文章主要介紹了詳解vue中在父組件點(diǎn)擊按鈕觸發(fā)子組件的事件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • vue實(shí)現(xiàn)圖片滑動驗(yàn)證

    vue實(shí)現(xiàn)圖片滑動驗(yàn)證

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)圖片滑動驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Vue中的路由導(dǎo)航守衛(wèi)導(dǎo)航解析流程

    Vue中的路由導(dǎo)航守衛(wèi)導(dǎo)航解析流程

    這篇文章主要介紹了Vue中的路由導(dǎo)航守衛(wèi)導(dǎo)航解析流程,正如其名,vue-router 提供的導(dǎo)航守衛(wèi)主要用來通過跳轉(zhuǎn)或取消的方式守衛(wèi)導(dǎo)航。這里有很多方式植入路由導(dǎo)航中:全局的,單個路由獨(dú)享的,或者組件級的
    2023-04-04
  • vue 監(jiān)聽鍵盤回車事件詳解 @keyup.enter || @keyup.enter.native

    vue 監(jiān)聽鍵盤回車事件詳解 @keyup.enter || @keyup.enter.native

    今天小編就為大家分享一篇vue 監(jiān)聽鍵盤回車事件詳解 @keyup.enter || @keyup.enter.native,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08

最新評論

文昌市| 当雄县| 商城县| 呼图壁县| 类乌齐县| 镇沅| 孝义市| 澄江县| 蓬安县| 靖宇县| 肥乡县| 宝应县| 永安市| 库尔勒市| 云林县| 缙云县| 镇远县| 吉林省| 秦皇岛市| 渝中区| 深州市| 南宫市| 德安县| 蚌埠市| 彰武县| 六盘水市| 瑞丽市| 嘉兴市| 西青区| 启东市| 壶关县| 大化| 内黄县| 建湖县| 昭苏县| 洛川县| 思南县| 酒泉市| 衡南县| 新营市| 枣庄市|