2 Star 46 Fork 17

涛走云散 / recorder

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

recorder

js audio recorder plugin.

主要用于Web端录制短音频。

  • 支持录音,暂停,恢复,和录音播放。
  • 支持音频数据的压缩,支持单双通道录音。
  • 支持录音时长显示。
  • 支持导出录音文件,格式为pcm或wav。
  • 支持录音波形显示,可自己定制。

使用

在线演示地址

recorder

demo使用

npm ci (推荐) 或 npm install
npm run dev

调试移动端

npm run https

编译

npm run build

使用方法

引入方式

  • npm方式:

安装:

npm i js-audio-recorder

调用:

import Recorder from 'js-audio-recorder';

let recorder = new Recorder();
  • script标签方式
<script type="text/javascript" src="./dist/recorder.js"></script>

let recorder = new Recorder();

API

初始化实例

可以配置输出数据参数,

let recorder = new Recorder({
    sampleBits: 16,         // 采样位数,支持 8 或 16,默认是16
    sampleRate: 16000,      // 采样率,支持 11025、16000、22050、24000、44100、48000,根据浏览器默认值,我的chrome是48000
    numChannels: 1,         // 声道,支持 1 或 2, 默认是1
});
  • 返回: <Recorder>

开始录音

recorder.start().then(function() {
    // 开始录音
});
  • 返回: Promise<{}>

录音暂停

// 暂停录音
recorder.pause();
  • 返回: void

继续录音

// 继续录音
recorder.resume()
  • 返回: void

仅支持暂停后,恢复录音。

结束录音

// 结束录音
recorder.stop();
  • 返回: void

录音播放

// 录音播放
recorder.play();
  • 返回: void

支持不结束直接调用录音播放。

销毁实例

// 销毁录音实例,置为null释放资源,fn为回调函数,
recorder.destroy().then(function() {
    recorder = null;
});
  • 返回: Promise<{}>

直接获取录音数据

获取 PCM 数据

// 获取 PCM 数据(Blob)
recorder.getPCMBlob();
  • 返回: <Blob>

获取 WAV 数据

// 获取 WAV 数据(Blob)
recorder.getWAVBlob();
  • 返回: <Blob>

下载录音文件

下载 PCM 格式

// 下载pcm文件
recorder.downloadPCM(fileName ?);
  • fileName <String> 重命名文件
  • 返回: <Blob>

下载 WAV 格式

// 下载wav文件
recorder.downloadWAV(fileName ?);
  • fileName <String> 重命名文件
  • 返回: <Blob>

获取录音时长

// 回调持续输出时长(当收集的栈满时触发)
recorder.onprocess = function(duration) {
    console.log(duration);
}
// 手动获取录音时长
console.log(recorder.duration);

录音波形显示

let dataArray = recorder.getRecordAnalyseData();

返回的是一个1024长的,0-255大小的Uint8Array类型。用户可以根据这些数据自定义录音波形。

播放外部音频文件

Recorder.playAudio(/* 放入blob数据 */);

支持的音乐格式由浏览器的audio支持的类型决定。

任务列表

  • 拆分recorder到各个功能模块。
  • 增加test代码。
  • promise,支持async, await。
  • 功能完善。
  • 兼容性测试。

注意

  1. 使用127.0.0.1或localhost尝试,因为getUserMedia在高版本的chrome下需要使用https。

兼容性

以下为测试结果,低于以下版本并不表示不支持,可能是未测试到,增加或标注请查看:issues6

window pc端

Chrome Firefox Edge Safari Opera IE
38+ 30+ 42+ 11+ 21+ 不支持

移动端

Chrome Firefox Safari Opera UC QQ 猎豹 搜狗 华为 小米
42+ 40+ 11+ 不支持 不支持 9.2+ 不支持 不支持 不支持 不支持

需要打开浏览器录音权限,在设置-权限中可以配置。

其他资源

MIT License Copyright (c) 2019 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

原生js实现的web端录音 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/2fps/recorder.git
git@gitee.com:2fps/recorder.git
2fps
recorder
recorder
master

搜索帮助