forked from xiangyuecn/Recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
239 lines (221 loc) · 6.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>Recorder测试</title>
</head>
<body>
<script src="recorder.js"></script>
<style>
body{
word-wrap: break-word;
}
pre{
white-space:pre-wrap;
}
.pd{
padding:0 0 6px 0;
}
</style>
<script>
//兼容环境
var PageLM="2018-12-2 16:22:34";
function BuildHtml(html,o,notEncode,loop){return o||(o={}),html=(null==html?"":html+"").replace(/\{(fn:)?(:)?(.+?)\}/gi,function(a,b,c,code){try{var v=eval(b?code:"o."+code);return v=void 0===v?"":v+"",c||notEncode?v:v}catch(e){return console.log("BuildHtml Fail",a+"\n"+e.stack),""}}),loop&&/\{(fn:)?(.+?)\}/.test(html)&&(html=BuildHtml(html,o,notEncode,loop)),html};
function RandomKey(){
return "randomkey"+(RandomKey.idx++);
};
RandomKey.idx=0;
</script>
<script src="https://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
<div class="pd">
GitHub:<a href="https://github.com/xiangyuecn/Recorder">https://github.com/xiangyuecn/Recorder</a>
</div>
<div class="pd">
类型:
<label><input type="radio" name="type" value="mp3" checked>MP3</label>
<label><input type="radio" name="type" value="wav">WAV</label>
</div>
<div class="pd">
比特率:<input type="text" class="bit" value="16">(kbps mp3) (wav取值8、16)
</div>
<div class="pd">
采样率:<input type="text" class="sample" value="16000">wav专用,mp3无效
</div>
<div class="pd">
<button onclick="recopen()">打开录音</button>
<button onclick="recclose()">关闭录音</button>
</div>
<div class="pd">
<button onclick="recstart()">录制</button>
<button onclick="recstop()">停止</button>
<button onclick="recpause()" style="margin-left:60px">暂停</button>
<button onclick="recresume()">继续</button>
</div>
<div class="pd recpower">
<div style="height:40px;width:300px;background:#999;position:relative;">
<div class="recpowerx" style="height:40px;background:#0B1;position:absolute;"></div>
<div class="recpowert" style="padding-left:50px; line-height:40px; position: relative;"></div>
</div>
</div>
<div class="pd">
<button onclick="recstop2()">批量编码</button>
<input type="text" class="bits" value="8 to 96 step 8">kbps,测试音质用的(wav填了也只有8、16)
</div>
<div class="reclog"></div>
<div class="recinfo"></div>
<audio class="recPlay"></audio>
</div>
<script type="text/template" class="tp_recinfo">
<hr/>
浏览器环境情况:
<pre>
AudioContext:{fn:!!window.AudioContext}
webkitAudioContext:{fn:!!window.webkitAudioContext}
mediaDevices:{fn:!!navigator.mediaDevices}
mediaDevices.getUserMedia:{fn:!!(navigator.mediaDevices&&navigator.mediaDevices.getUserMedia)}
navigator.getUserMedia:{fn:!!navigator.getUserMedia}
navigator.webkitGetUserMedia:{fn:!!navigator.webkitGetUserMedia}
UA:{fn:navigator.userAgent}
Recorder库修改时间(有可能修改了忘改):{fn:RecorderLM}
本页面修改时间(有可能修改了忘改):{fn:PageLM}
注:录音时注意观察灰色区域是否有绿色音量跳动,没有绿色跳动说明Recorder没有获取到声音数据。如果测试发现mp3没有声音,可以试一下wav格式,如果wav格式有声音,说明内置lamejs mp3编码器有问题。如果都没有,下载下来播放看看有没有。下载下来也没有声音可以反馈一下。
</pre>
</script>
<script>
var rec
function reclog(s){
$(".reclog").prepend('<div>['+new Date().toLocaleTimeString()+']'+s+'</div>');
};
function recopen(){
var type=$("[name=type]:checked").val();
var bit=+$(".bit").val();
var sample=+$(".sample").val();
rec=Recorder({
type:type
,bitRate:bit
,sampleRate:sample
,onProcess:function(a,level,time){
$(".recpowerx").css("width",level+"%");
$(".recpowert").html(time+"/"+level);
}});
rec.open(function(){
reclog("已打开:"+type+" "+bit+"kbps");
},function(e){
reclog("打开失败:"+e);
});
};
function recclose(){
if(rec){
rec.close(function(){
reclog("已关闭");
});
}
};
function recstart(){
if(rec){
rec.start();
reclog("录制中...");
};
};
function recpause(){
if(rec){
rec.pause();
reclog("已暂停");
};
};
function recresume(){
if(rec){
rec.resume();
reclog("继续录音中...");
};
};
var recblob={};
function recstop(call){
if(rec){
var t1=Date.now();
rec.stop(function(blob,time){
var id=RandomKey(16);
recblob[id]={blob:blob,set:$.extend({},rec.set),time:time};
reclog("已录制:编码耗时"+(Date.now()-t1)+"ms 比特率"+rec.set.bitRate+"kbps 文件大小"+blob.size+"b "+rec.set.type+"音频时长"+time+'ms <button class="Btn BtnMinMin" onclick="recdown(\''+id+'\')">下载</button> <button class="Btn BtnMinMin" onclick="recplay(\''+id+'\')">播放</button>');
call&&call();
},function(s){
reclog("失败:"+s);
call&&call();
});
};
};
function recstop2(){
if(!rec||!rec.buffer){
reclog("需先录个音");
return;
};
var bits=/(\d+)\s+to\s+(\d+)\s+step\s+(\d+)\s*/i.exec($(".bits").val());
if(!bits){
reclog("码率列表有误,需要? to ? step ?结构");
return;
};
var list=[];
for(var i=+bits[1];i<+bits[2]+1;i+=+bits[3]){
list.push(i);
};
if(rec.set.type=="wav"){
list=[8,16];
};
var i=-1;
var bak=rec.set.bitRate;
var run=function(){
i++;
if(i>=list.length){
rec.set.bitRate=bak;
reclog("批量编码完成");
return;
};
rec.set.bitRate=list[i];
rec.state=1;
recstop(run);
};
run();
};
function recplay(key){
var o=recblob[key];
if(o){
var audio=$(".recPlay")[0];
if(!(audio.ended || audio.paused)){
audio.pause();
};
audio.src=URL.createObjectURL(o.blob);
audio.play();
reclog("已播放"+o.blob.size+"b "+o.time+'ms '+o.set.type+' '+o.set.bitRate+'kbps');
};
};
function recdown(key){
var o=recblob[key];
if(o){
var cls=RandomKey(16);
var name="rec-"+o.time+"ms."+o.set.type;
reclog('<span class="'+cls+'"> 没弹下载?试一下链接或复制文本<button class="Btn BtnMinMin" onclick="recdown64(\''+key+'\',\''+cls+'\')">生成Base64文本</button></span>');
var downA=document.createElement("A");
downA.innerHTML="下载 "+name;
downA.href=URL.createObjectURL(o.blob);
downA.download=name;
$("."+cls).prepend(downA);
downA.click();
};
};
function recdown64(key, cls){
var o=recblob[key];
var reader = new FileReader();
reader.readAsDataURL(o.blob);
reader.onloadend = function() {
var id=RandomKey(16);
$("."+cls).append('<textarea class="'+id+'"></textarea>');
$("."+id).val(reader.result);
};
};
$(".recinfo").html(BuildHtml($(".tp_recinfo").html()));
reclog("点击打开录音开始哦");
</script>
</body>
</html>