realtime.js

 1 var realtimeMoudles = {
 2     "system": {
 3         "tip": function() {
 4             alert("hello world!");
 5         },
 6         "test": function (parameter) {
 7             alert(parameter[0]);
 8             alert(parameter[1]);
 9         }
10     },
11     "execute": function(module, method) {
12         realtimeMoudles[module][method]();
13     },
14     "executeWithParameter": function(module, method, parameters) {
15         realtimeMoudles[module][method](JSON.parse(parameters));
16     }
17 };
18 var instance = null;
19 var Realtime = {
20     "Call" : function(module, moduleItem, method) {
21         $.post("Library/Realtime/Call.ashx", {"module" : module, "moduleItem": moduleItem, "method": method}, function (result) {
22         });
23     },
24     "CallForResult": function (module, moduleItem, method, func) {
25         $.post("Library/Realtime/CallForResult.ashx", { "module": module, "moduleItem": moduleItem, "method": method }, function (result) {
26             func(result);
27         });
28     },
29     "CallForJson": function (module, moduleItem, method, func) {
30         $.post("Library/Realtime/CallForJson.ashx", { "module": module, "moduleItem": moduleItem, "method": method }, function (result) {
31             func(result);
32         });
33     },
34     "CallWithParams": function (module, moduleItem, method, params) {
35         $.post("Library/Realtime/CallWithParams.ashx", { "module": module, "moduleItem": moduleItem, "method": method, "params": params.join("`") }, function (result) {
36         });
37     },
38     "CallWithParamsForResult": function (module, moduleItem, method, params, func) {
39         $.post("Library/Realtime/CallWithParamsForResult.ashx", { "module": module, "moduleItem": moduleItem, "method": method, "params": params.join("`") }, function (result) {
40             func(result);
41         });
42     },
43     "CallWithParamsForJson": function (module, moduleItem, method, params, func) {
44         $.post("Library/Realtime/CallWithParamsForJson.ashx", { "module": module, "moduleItem": moduleItem, "method": method, "params": params.join("`") }, function (result) {
45             func(result);
46         });
47     },
48     "AddClientFunction": function (module, moduleItem, func) {
49         if (realtimeMoudles[module] == null) {
50             realtimeMoudles[module] = {};
51         }
52         eval("realtimeMoudles[module]." + moduleItem + "= func");
53         if (instance != null) {
54             if (instance.client[module] == null) {
55                 instance.client[module] = {};
56             }
57             eval("instance.client[module]." + moduleItem + " = func");
58             if (Realtime[module] == null) {
59                 Realtime[module] = {};
60             }
61             eval("Realtime[module]." + moduleItem + " = func");
62         }
63     }
64 };
65 
66 $(function () {
67     instance = $.connection.realtimeInstance;
68     for (var module in realtimeMoudles) {
69         if (instance.client[module] == null) {
70             instance.client[module] = {};
71         }
72         instance.client[module] = realtimeMoudles[module];
73         if (Realtime[module] == null) {
74             Realtime[module] = {};
75         }
76         Realtime[module] = realtimeMoudles[module];
77     }
78     $.connection.hub.start();
79 });

 

转载于:https://www.cnblogs.com/radray/p/4142874.html

Html5网页纯JavaScript录制MP3音频 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Html5网页JavaScript录制MP3音频</title> <meta charset="utf-8" /> </head> <body> Html5网页JavaScript录制MP3音频 录制 停止 上传 调试信息: [removed][removed] [removed] var recorder = new MP3Recorder({ debug:true, funOk: function () { btnStart.disabled = false; log('初始化成功'); }, funCancel: function (msg) { log(msg); recorder = null; } }); var mp3Blob; function funStart(button) { btnStart.disabled = true; btnStop.disabled = false; btnUpload.disabled = true; log('录音开始...'); recorder.start(); } function funStop(button) { recorder.stop(); btnStart.disabled = false; btnStop.disabled = true; btnUpload.disabled = false; log('录音结束,MP3导出中...'); recorder.getMp3Blob(function (blob) { log('MP3导出成功'); mp3Blob = blob; var url = URL.createObjectURL(mp3Blob); var div = document.createElement('div'); var au = document.createElement('audio'); var hf = document.createElement('a'); au.controls = true; au.src = url; hf.href = url; hf.download = new Date().toISOString() + '.mp3'; hf[removed] = hf.download; div.appendChild(au); div.appendChild(hf); recordingslist.appendChild(div); }); } function log(str) { recordingslist[removed] += str + ''; } function funUpload() { var fd = new FormData(); var mp3Name = encodeURIComponent('audio_recording_' + new Date().getTime() + '.mp3'); fd.append('mp3Name', mp3Name); fd.append('file', mp3Blob); var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { recordingslist[removed] += '上传成功:' + mp3Name + ''; } }; xhr.open('POST', 'upload.ashx'); xhr.send(fd); } [removed] </body> </html> [javascript] view plain copy 在CODE上查看代码片派生到我的代码片 (function (exports) { var MP3Recorder = function (config) { var recorder = this; config = config || {}; config.sampleRate = config.sampleRate || 44100; config.bitRate = config.bitRate || 128; navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; if (navigator.getUserMedia) { navigator.getUserMedia({ audio: true }, function (stream) { var context = new AudioContext(), microphone = context.createMediaStreamSource(stream), processor = context.createScriptProcessor(16384, 1, 1),//bufferSize大小,输入channel数,输出channel数 mp3ReceiveSuccess, currentErrorCallback; config.sampleRate = context.sampleRate; processor.onaudioprocess = function (event) { //边录音边转换 var array = event.inputBuffer.getChannelData(0); realTimeWorker.postMessage({ cmd: 'encode', buf: array }); }; var realTimeWorker = new Worker('js/worker-realtime.js'); realTimeWorker.onmessage = function (e) { switch (e.data.cmd) { case 'init': log('初始化成功'); if (config.funOk) { config.funOk(); } break; case 'end': log('MP3大小:', e.data.buf.length); if (mp3ReceiveSuccess) { mp3ReceiveSuccess(new Blob(e.data.buf, { type: 'audio/mp3' })); } break; case 'error': log('错误信息:' + e.data.error); if (currentErrorCallback) { currentErrorCallback(e.data.error); } break; default: log('未知信息:', e.data); } }; recorder.getMp3Blob = function (onSuccess, onError) { currentErrorCallback = onError; mp3ReceiveSuccess = onSuccess; realTimeWorker.postMessage({ cmd: 'finish' }); }; recorder.start = function () { if (processor && microphone) { microphone.connect(processor); processor.connect(context.destination); log('开始录音'); } } recorder.stop = function () { if (processor && microphone) { microphone.disconnect(); processor.disconnect(); log('录音结束'); } } realTimeWorker.postMessage({ cmd: 'init', config: { sampleRate: config.sampleRate, bitRate: config.bitRate } }); }, function (error) { var msg; switch (error.code || error.name) { case 'PERMISSION_DENIED': case 'PermissionDeniedError': msg = '用户拒绝访问麦客风'; break; case 'NOT_SUPPORTED_ERROR': case 'NotSupportedError': msg = '浏览器不支持麦客风'; break; case 'MANDATORY_UNSATISFIED_ERROR': case 'MandatoryUnsatisfiedError': msg = '找不到麦客风设备'; break; default: msg = '无法打开麦克风,异常信息:' + (error.code || error.name); break; } if (config.funCancel) { config.funCancel(msg); } }); } else { if (config.funCancel) { config.funCancel('当前浏览器不支持录音功能'); } } function log(str) { if (config.debug) { console.log(str); } } } exports.MP3Recorder = MP3Recorder; })(window);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值