Vue+node實(shí)現(xiàn)音頻錄制播放功能
實(shí)現(xiàn)效果:
主要實(shí)現(xiàn)代碼邏輯部分,具體頁(yè)面結(jié)構(gòu)就不一一介紹了。
vue部分:
安裝recorderx
cnpm install recorderx --save
或者
npm install recorderx --save
在具體的組件中引入
<script> import axios from "axios"; import { Toast } from "vant"; import Recorderx, { ENCODE_TYPE } from "recorderx"; const rc = new Recorderx(); export default { data(){ return{ startime:null, endtime :null } }, methods:{ //錄制語(yǔ)音 recordingVoice() { // that.news_img = !that.news_img rc.start() .then(() => { this.startime = new Date(); }) .catch(error => { alert("獲取麥克風(fēng)失敗"); }); }, //發(fā)送語(yǔ)音 async sendVoice() { rc.pause(); this.endtime = new Date(); let wav = rc.getRecord({ encodeTo: ENCODE_TYPE.WAV, compressible: true }); let voiceTime = Math.ceil((this.endtime - this.startime) / 1000); const formData = new FormData(); formData.append("chatVoice", wav, Date.parse(new Date()) + ".wav"); formData.append("voiceTime", voiceTime); let headers = { headers: { "Content-Type": "multipart/form-data" } }; axios .post("/api/uploadChatVoice", formData, headers) .then(res => { //console.log(res) if (res.data.status === 2) { rc.clear(); let chatVoiceMsg = res.data.chatVoiceMsg; } } }); }, //播放語(yǔ)音 playChatVoice(audio) { let audioUrl = audio; if(audioUrl){ let audioExample = new Audio(); audioExample.src = audioUrl; //想要播放的音頻地址 audioExample.play(); }else{ Toast('語(yǔ)音地址已被摧毀'); } }, } }; </script>
node部分:
這里我使用的是express框架搭建的后臺(tái)
具體的獲取前臺(tái)的請(qǐng)求代碼如下
安裝multiparty
cnpm install multiparty --save
const express = require('express'); const router = express.Router(); const multiparty = require('multiparty'); const NET_URL = 'http://127.0.0.1:3000/'; router.post('/uploadChatVoice', (req, res, next) => { let form = new multiparty.Form(); form.uploadDir = 'chatVoiceUpload'; form.parse(req, (err, fields, files) => { console.log(files, fields) let chatVoiceUrl = NET_URL + files.chatVoice[0].path.replace(/\\/g, "/"); let chatVoiceTime = fields.voiceTime[0] console.log(chatVoiceUrl) if (chatVoiceUrl) { res.json({ status: 2, chatVoiceMsg: { chatVoiceTime, chatVoiceUrl, } }) } else { res.json({ status: 1, chatVoiceMsg: { chatVoiceTime: "", chatVoiceUrl: "" } }) } //console.log(files) }) })
在app.js中,定義語(yǔ)音文件路徑
app.use('/chatVoiceUpload', express.static('chatVoiceUpload'));
到此這篇關(guān)于Vue+node實(shí)現(xiàn)音頻錄制播放功能的文章就介紹到這了,更多相關(guān)Vue 實(shí)現(xiàn)音頻錄制播放內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項(xiàng)目pdf(base64)轉(zhuǎn)圖片遇到的問(wèn)題及解決方法
這篇文章主要介紹了Vue項(xiàng)目pdf(base64)轉(zhuǎn)圖片遇到的問(wèn)題及解決方法,需要的朋友可以參考下2018-10-10羊了個(gè)羊通關(guān)腳本Vue?node實(shí)現(xiàn)版本
這篇文章主要為大家介紹了羊了個(gè)羊通關(guān)腳本Vue?node實(shí)現(xiàn)版本,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09vue中的vue-router?query方式和params方式詳解
這篇文章主要介紹了vue中的vue-router?query方式和params方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue實(shí)現(xiàn)數(shù)值型輸入框并限制長(zhǎng)度
這篇文章主要介紹了Vue實(shí)現(xiàn)數(shù)值型輸入框并限制長(zhǎng)度,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue自定義加載指令v-loading占位圖指令v-showimg
這篇文章主要為大家介紹了vue自定義加載指令和v-loading占位圖指令v-showimg的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08vue 循環(huán)加載數(shù)據(jù)并獲取第一條記錄的方法
今天小編就為大家分享一篇vue 循環(huán)加載數(shù)據(jù)并獲取第一條記錄的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue實(shí)現(xiàn)一個(gè)滾動(dòng)條樣式
滾動(dòng)條能夠給用戶帶來(lái)極好的體驗(yàn)效果,今天通過(guò)本文給大家分享vue實(shí)現(xiàn)一個(gè)滾動(dòng)條樣式,代碼簡(jiǎn)單易懂,需要的朋友參考下吧2021-07-07基于Vue的前端界面實(shí)現(xiàn)日期時(shí)間實(shí)時(shí)顯示簡(jiǎn)單代碼
今天在項(xiàng)目中有一個(gè)功能是要求顯示北京的實(shí)時(shí)時(shí)間,下面這篇文章主要給大家介紹了關(guān)于如何基于Vue的前端界面實(shí)現(xiàn)日期時(shí)間實(shí)時(shí)顯示的相關(guān)資料,需要的朋友可以參考下2024-01-01