-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wangxue08
committed
Feb 22, 2024
1 parent
09b3c73
commit 102c410
Showing
11 changed files
with
497 additions
and
38 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/cw/cw_audio_library/components/cw-audio-record/api.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
- name: cw-audio-record | ||
title: 音频录制组件 | ||
type: both | ||
belong: component | ||
labels: [Runtime] | ||
attrs: | ||
- name: value | ||
type: string | ||
default: 请在这里编写代码 | ||
description: 需要传入的值 |
5 changes: 5 additions & 0 deletions
5
packages/cw/cw_audio_library/components/cw-audio-record/docs/blocks.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
### 基本用法 | ||
|
||
``` html | ||
<cw-audio-record></cw-audio-record> | ||
``` |
5 changes: 5 additions & 0 deletions
5
packages/cw/cw_audio_library/components/cw-audio-record/docs/examples.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
### 基本用法 | ||
|
||
``` html | ||
<cw-audio-record></cw-audio-record> | ||
``` |
3 changes: 3 additions & 0 deletions
3
packages/cw/cw_audio_library/components/cw-audio-record/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import CwAudioRecord from "./index.vue" | ||
|
||
export default CwAudioRecord |
120 changes: 120 additions & 0 deletions
120
packages/cw/cw_audio_library/components/cw-audio-record/index.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<template> | ||
<div> | ||
<canvas ref="audioCanvas"></canvas> | ||
<u-button @click="startRecord">开始录音</u-button> | ||
|
||
</div> | ||
|
||
</template> | ||
|
||
<script> | ||
import Recorder from 'js-audio-recorder'; | ||
export default { | ||
name: "cw-audio-record", | ||
props: { | ||
sampleRateOptions: { | ||
type: Number, | ||
default: 16000 | ||
}, | ||
sampleBitOptions: { | ||
type: Number, | ||
default: 16 | ||
}, | ||
channelOptions: { | ||
type: Number, | ||
default: 1 | ||
}, | ||
}, | ||
data() { | ||
return { | ||
isRecording: false, // 是否正在录音 | ||
duration: 0, // 录音时长 | ||
}; | ||
}, | ||
methods: { | ||
startRecord() { | ||
const config = { | ||
sampleRate: this.sampleRateOptions, | ||
sampleBit: this.sampleBitOptions, | ||
channelCount: this.channelOptions | ||
}; | ||
if (!this.recorder) { | ||
this.recorder = new Recorder(config); | ||
this.recorder.start().then(() => { | ||
console.log('开始录音'); | ||
}, (error) => { | ||
console.log(`异常了,${error.name}:${error.message}`); | ||
}); | ||
this.drawRecord(); | ||
} | ||
}, | ||
pauseRecord() { | ||
if (this.recorder) { | ||
this.recorder.pause(); | ||
console.log('暂停录音'); | ||
this.drawRecordId && cancelAnimationFrame(this.drawRecordId); | ||
this.drawRecordId = null; | ||
} | ||
}, | ||
resumeRecord() { | ||
this.recorder && this.recorder.resume(); | ||
console.log('恢复录音'); | ||
this.drawRecord(); | ||
}, | ||
stopRecord() { | ||
if (this.recorder) { | ||
this.recorder.stop().then((blob) => { | ||
console.log('停止录音'); | ||
console.log('录音时长:' + this.recorder.duration + 'ms'); | ||
this.duration = this.recorder.duration; | ||
this.recorder = null; | ||
this.drawRecordId && cancelAnimationFrame(this.drawRecordId); | ||
this.drawRecordId = null; | ||
}); | ||
} | ||
}, | ||
drawRecord() { | ||
const oCanvas = this.$refs.imgCanvas; | ||
// 获取音频数据 | ||
let dataArr = this.recorder.getRecordAnalyseData(); | ||
let bufferLength = dataArr.length; | ||
const sliceWidth = oCanvas.width * 1.0 / bufferLength; | ||
let x = 0; | ||
// 用requestAnimationFrame稳定60fps绘制 | ||
this.drawRecordId = requestAnimationFrame(this.drawRecord); | ||
// 填充背景色 | ||
ctx.fillStyle = 'rgb(200, 200, 200)'; | ||
ctx.fillRect(0, 0, oCanvas.width, oCanvas.height); | ||
// 设定波形绘制颜色 | ||
ctx.lineWidth = 2; | ||
ctx.strokeStyle = 'rgb(0, 0, 0)'; | ||
ctx.beginPath(); | ||
for (var i = 0; i < bufferLength; i++) { | ||
var v = dataArray[i] / 128.0; | ||
var y = v * oCanvas.height / 2; | ||
if (i === 0) { | ||
// 第一个点 | ||
ctx.moveTo(x, y); | ||
} else { | ||
// 剩余的点 | ||
ctx.lineTo(x, y); | ||
} | ||
// 依次平移,绘制所有点 | ||
x += sliceWidth; | ||
} | ||
ctx.lineTo(oCanvas.width, oCanvas.height / 2); | ||
ctx.stroke(); | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
packages/cw/cw_audio_library/components/cw-audio-view/index.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import CwAudioView from './components/cw-audio-view'; | ||
import CwAudioRecord from './components/cw-audio-record'; | ||
// COMPONENT IMPORTS | ||
export { | ||
CwAudioView, | ||
CwAudioRecord, | ||
// COMPONENT EXPORTS | ||
}; |
Oops, something went wrong.