使用方式
npm
安裝:npm i js-audio-recorder
調用:在需要錄音的地方引入
import Recorder from 'js-audio-recorder'
let recorder = new Recorder()
API
1 // 開始錄音 2 3 recorder.start(); 4 5 // 暫停錄音 6 7 recorder.pause(); 8 9 // 繼續錄音 10 11 recorder.resume() 12 13 // 結束錄音 14 15 recorder.stop(); 16 17 // 錄音播放 18 19 recorder.play(); 20 21 // 銷毀錄音實例,釋放資源,fn為回調函數, 22 23 recorder.destroy(fn); 24 25 recorder = null; 26 27 下載功能 28 29 // 下載pcm文件 30 31 recorder.downloadPCM(); 32 33 // 下載wav文件 34 35 recorder.downloadWAV(); 36 37 // 重命名pcm文件,wav也支持 38 39 recorder.downloadPCM('重命名'); 40 41 獲取錄音時長 42 43 // 回調持續輸出時長 44 45 recorder.onprocess = function(duration) { 46 47 console.log(duration); 48 49 } 50 51 // 手動獲取錄音時長 52 53 console.log(recorder.duration);
使用示例:
1 <template> 2 <div class="home"> 3 <h1 @click="handleclick()">開始錄音</h1> 4 <h1 @click="handleclickl()">繼續錄音</h1> 5 <h1 @click="handleclicks()">結束錄音</h1> 6 <h1 @click="handleclickp()">錄音播放</h1> 7 </div> 8 </template> 9 10 <script> 11 import Recorder from 'js-audio-recorder' 12 let recorder = new Recorder() 13 export default { 14 name: 'home', 15 methods: { 16 handleclick () { 17 console.log(1) 18 recorder.start()// 開始錄音 19 }, 20 handleclickl () { 21 console.log(2) 22 recorder.resume()// 繼續錄音 23 }, 24 handleclickt () { 25 console.log(3) 26 recorder.stop() // 結束錄音 27 }, 28 handleclickb () { 29 console.log(4) 30 recorder.play() // 錄音播放 31 } 32 } 33 } 34 </script>
本文轉載自:https://www.cnblogs.com/lljun/p/11535807.html