作者:谜乱凡_134 | 来源:互联网 | 2020-09-02 20:30
本文通过实例代码给大家介绍了HTML5声音录制/播放功能,非常不错,具有参考借鉴价值,需要的朋友跟随脚本之家小编一起学习吧
html代码:
HZRecorder.js
(function (window) {
//兼容
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
var HZRecorder = function (stream, config) {
cOnfig= config || {};
config.sampleBits = config.sampleBits || 8 //采样数位 8, 16
config.sampleRate = config.sampleRate || (44100 / 6); //采样率(1/6 44100)
var cOntext= new (window.webkitAudioContext || window.AudioContext)();
var audioInput = context.createMediaStreamSource(stream);
var createScript = context.createScriptProcessor || context.createJavascriptNode;
var recorder = createScript.apply(context, [4096, 1, 1]);
var mp3ReceiveSuccess, currentErrorCallback;
var audioData = {
size: 0 //录音文件长度
, buffer: [] //录音缓存
, inputSampleRate: context.sampleRate //输入采样率
, inputSampleBits: 16 //输入采样数位 8, 16
, outputSampleRate: config.sampleRate //输出采样率
, oututSampleBits: config.sampleBits //输出采样数位 8, 16
, input: function (data) {
this.buffer.push(new Float32Array(data));
this.size += data.length;
}
, compress: function () { //合并压缩
//合并
var data = new Float32Array(this.size);
var offset = 0;
for (var i = 0; i 以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!