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

參考地址

一、下載插件

npm i js-audio-recorder

二、示例

<template>

? <div style="padding: 20px;">

? ? <h3>錄音上傳</h3>

? ? <div style="font-size:14px">

? ? ? <h3>錄音時(shí)長(zhǎng):{{ 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>

? ? ? ? 播放時(shí)長(zhǎng):{{

? ? ? ? ? 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: '請(qǐng)先允許該網(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() // 播放錄音

? ? ? // 播放時(shí)長(zhǎng)

? ? ? this.timer = setInterval(() => {

? ? ? ? try {

? ? ? ? ? this.playTime = this.recorder.getPlayTime()

? ? ? ? } catch (error) {

? ? ? ? ? this.timer = null

? ? ? ? }

? ? ? }, 100)

? ? },

? ? handlePausePlay() {

? ? ? console.log('暫停播放')

? ? ? this.recorder.pausePlay() // 暫停播放

? ? ? // 播放時(shí)長(zhǎng)

? ? ? this.playTime = this.recorder.getPlayTime()

? ? ? this.time = null

? ? },

? ? handleResumePlay() {

? ? ? console.log('恢復(fù)播放')

? ? ? this.recorder.resumePlay() // 恢復(fù)播放

? ? ? // 播放時(shí)長(zhǎng)

? ? ? this.timer = setInterval(() => {

? ? ? ? try {

? ? ? ? ? this.playTime = this.recorder.getPlayTime()

? ? ? ? } catch (error) {

? ? ? ? ? this.timer = null

? ? ? ? }

? ? ? }, 100)

? ? },

? ? handleStopPlay() {

? ? ? console.log('停止播放')

? ? ? this.recorder.stopPlay() // 停止播放

? ? ? // 播放時(shí)長(zhǎng)

? ? ? 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: '請(qǐng)先錄音',

? ? ? ? ? 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對(duì)象后需要設(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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末怎憋,一起剝皮案震驚了整個(gè)濱河市只祠,隨后出現(xiàn)的幾起案子枯怖,更是在濱河造成了極大的恐慌勒庄,老刑警劉巖挣轨,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件军熏,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡卷扮,警方通過查閱死者的電腦和手機(jī)荡澎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晤锹,“玉大人摩幔,你說我怎么就攤上這事”廾” “怎么了或衡?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)车遂。 經(jīng)常有香客問我封断,道長(zhǎng),這世上最難降的妖魔是什么舶担? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任坡疼,我火速辦了婚禮,結(jié)果婚禮上衣陶,老公的妹妹穿的比我還像新娘柄瑰。我一直安慰自己闸氮,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布狱意。 她就那樣靜靜地躺著湖苞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪详囤。 梳的紋絲不亂的頭發(fā)上财骨,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天,我揣著相機(jī)與錄音藏姐,去河邊找鬼隆箩。 笑死,一個(gè)胖子當(dāng)著我的面吹牛羔杨,可吹牛的內(nèi)容都是我干的捌臊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼兜材,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼理澎!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起曙寡,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤糠爬,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后举庶,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體执隧,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年户侥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了镀琉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蕊唐,死狀恐怖屋摔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情替梨,我是刑警寧澤凡壤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站耙替,受9級(jí)特大地震影響亚侠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜俗扇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一硝烂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧铜幽,春花似錦滞谢、人聲如沸串稀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽母截。三九已至,卻和暖如春橄教,著一層夾襖步出監(jiān)牢的瞬間清寇,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工护蝶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留华烟,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓持灰,卻偏偏與公主長(zhǎng)得像盔夜,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子堤魁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容