欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

最近項目中需要實現(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)頁使用麥克風',
          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格式音頻數(shù)據(jù)
      // 此處獲取到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>

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

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

注意事項

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

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

其實這是因為瀏覽器不支持http:IP開頭的路徑,認為這個路徑不安全

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

解決辦法:

chrome瀏覽器

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

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

然后就OK了

總結(jié)

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

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

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

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

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

更新

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

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

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

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

相關文章

  • vue axios登錄請求攔截器

    vue axios登錄請求攔截器

    這篇文章主要介紹了vue axios登錄請求攔截器,判斷是否登錄超時,或?qū)φ埱蠼Y(jié)果做一個統(tǒng)一處理的教程詳解,需要的朋友可以參考下
    2018-04-04
  • Vue項目中使用fontawesome圖標庫的方法

    Vue項目中使用fontawesome圖標庫的方法

    fontawesome的圖標有免費版和專業(yè)版,本文主要使用free版本,一般free版本的圖標夠用,free圖標又劃分為三個圖標庫,主要有實心圖標solid、常規(guī)圖標regular及品牌圖標brand,根據(jù)需求去下載對應的圖標庫,無須全部下載,對vue?fontawesome圖標庫相關知識感興趣的朋友一起看看吧
    2023-12-12
  • Vue學習筆記進階篇之單元素過度

    Vue學習筆記進階篇之單元素過度

    這篇文章主要介紹了Vue學習筆記進階篇之單元素過度,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Vue路由重復點擊報錯問題及解決

    Vue路由重復點擊報錯問題及解決

    這篇文章主要介紹了Vue路由重復點擊報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue 實現(xiàn)數(shù)字滾動增加效果的實例代碼

    vue 實現(xiàn)數(shù)字滾動增加效果的實例代碼

    最近做了個項目需要做數(shù)字滾動增加的效果,剛開始接到這個項目還真是懵了,后來發(fā)現(xiàn)實現(xiàn)代碼很簡單的,下面小編給大家?guī)砹藇ue 實現(xiàn)數(shù)字滾動增加效果的實例代碼,需要的朋友參考下吧
    2018-07-07
  • vue項目中實現(xiàn)圖片預覽的公用組件功能

    vue項目中實現(xiàn)圖片預覽的公用組件功能

    小編接到查看影像的功能需求,根據(jù)需求,多個組件需要用到查看影像的功能,所以考慮做一個公用組件,通過組件傳值的方法將查看影像文件的入?yún)鬟^去。下面小編通過實例代碼給大家分享vue項目中實現(xiàn)圖片預覽的公用組件功能,需要的朋友參考下吧
    2018-10-10
  • Vue 數(shù)據(jù)綁定的原理分析

    Vue 數(shù)據(jù)綁定的原理分析

    這篇文章主要介紹了Vue 數(shù)據(jù)綁定的原理,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-11-11
  • Vue在啟動時卡住了,啟動不了的問題及解決

    Vue在啟動時卡住了,啟動不了的問題及解決

    這篇文章主要介紹了Vue在啟動時卡住了,啟動不了的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue+webrtc(騰訊云) 實現(xiàn)直播功能的實踐

    vue+webrtc(騰訊云) 實現(xiàn)直播功能的實踐

    本文主要介紹了vue+webrtc(騰訊云) 實現(xiàn)直播功能的實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • VUE使用axios調(diào)用后臺API接口的方法

    VUE使用axios調(diào)用后臺API接口的方法

    這篇文章主要介紹了VUE使用axios調(diào)用后臺API接口的方法,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-08-08

最新評論