@lightsoft/js-sdk 1.2.3 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -306,6 +306,50 @@ declare class StreamRequest {
306
306
  private onError;
307
307
  }
308
308
 
309
+ interface AudioRecorderOptions {
310
+ media: MediaStreamConstraints;
311
+ mediaRecorder?: MediaRecorderOptions;
312
+ maxTime?: number;
313
+ }
314
+ interface TimerOptions {
315
+ next?: () => void;
316
+ end?: () => void;
317
+ }
318
+ interface AudioInfo {
319
+ sampleRate: number;
320
+ channels: number;
321
+ format: string | null;
322
+ bitDepth: number;
323
+ }
324
+ declare class AudioRecorder {
325
+ private options;
326
+ private stream;
327
+ private mediaRecorder;
328
+ private audioChunks;
329
+ private audioInfo;
330
+ private timer;
331
+ private timerCount;
332
+ private stopCallback;
333
+ private timerCallback;
334
+ private timerEndCallback;
335
+ constructor(options: AudioRecorderOptions);
336
+ start(): Promise<void>;
337
+ stop(): void;
338
+ reset(): void;
339
+ onStop(callback: (audioChunks: Blob[]) => void): void;
340
+ startTimer(options?: TimerOptions): void;
341
+ clearTimer(): void;
342
+ onTimer(callback: (count: number) => void): void;
343
+ onTimerEnd(callback: () => void): void;
344
+ getAudioBlob(): Blob | null;
345
+ getAudioUrl(): string;
346
+ getAudioBase64(blob: Blob): Promise<string>;
347
+ getAudioInfo(): Promise<unknown>;
348
+ getWavBitDepth(blob: Blob): Promise<number>;
349
+ getExtension(mimeType: string): string;
350
+ upload(url: string, options?: RequestInit): Promise<any>;
351
+ }
352
+
309
353
  /**
310
354
  * 匹配中文字母数字
311
355
  */
@@ -331,5 +375,5 @@ declare const email: RegExp;
331
375
  */
332
376
  declare const url: RegExp;
333
377
 
334
- export { HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, createWebSocket, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getState, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useClose, useCountDown, useDownload, useOnMessage, useSendMessage, zhLetterNumber };
335
- export type { ISectoralTable, Tag, typeIsNull };
378
+ export { AudioRecorder, HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, createWebSocket, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getState, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useClose, useCountDown, useDownload, useOnMessage, useSendMessage, zhLetterNumber };
379
+ export type { AudioInfo, AudioRecorderOptions, ISectoralTable, Tag, typeIsNull };
package/dist/index.js CHANGED
@@ -2977,6 +2977,239 @@ var StreamRequest = /** @class */ (function () {
2977
2977
  return StreamRequest;
2978
2978
  }());
2979
2979
 
2980
+ var AudioRecorder = /** @class */ (function () {
2981
+ function AudioRecorder(options) {
2982
+ this.options = {
2983
+ media: {
2984
+ audio: {
2985
+ channelCount: { ideal: 2 },
2986
+ sampleRate: { ideal: 44100 },
2987
+ echoCancellation: true,
2988
+ }
2989
+ },
2990
+ mediaRecorder: {
2991
+ mimeType: 'audio/mp4;codecs=mp4a.40.2',
2992
+ },
2993
+ maxTime: 0,
2994
+ };
2995
+ this.stream = null;
2996
+ this.mediaRecorder = null;
2997
+ this.audioChunks = [];
2998
+ this.audioInfo = {
2999
+ sampleRate: 0,
3000
+ channels: 0,
3001
+ format: null,
3002
+ bitDepth: 0,
3003
+ };
3004
+ this.timer = null;
3005
+ this.timerCount = 0;
3006
+ this.stopCallback = null;
3007
+ this.timerCallback = null;
3008
+ this.timerEndCallback = null;
3009
+ this.audioChunks = [];
3010
+ this.options = options;
3011
+ }
3012
+ // 开始录音
3013
+ AudioRecorder.prototype.start = function () {
3014
+ return __awaiter(this, void 0, void 0, function () {
3015
+ var _a, media, mediaRecorder, _b, maxTime, _c, error_1;
3016
+ var _this = this;
3017
+ return __generator(this, function (_d) {
3018
+ switch (_d.label) {
3019
+ case 0:
3020
+ _d.trys.push([0, 2, , 3]);
3021
+ _a = this.options, media = _a.media, mediaRecorder = _a.mediaRecorder, _b = _a.maxTime, maxTime = _b === void 0 ? 0 : _b;
3022
+ _c = this;
3023
+ return [4 /*yield*/, navigator.mediaDevices.getUserMedia({ audio: media.audio })];
3024
+ case 1:
3025
+ _c.stream = _d.sent();
3026
+ this.mediaRecorder = new MediaRecorder(this.stream, mediaRecorder);
3027
+ this.mediaRecorder.ondataavailable = function (event) {
3028
+ _this.audioInfo.format = event.data.type;
3029
+ _this.audioChunks.push(event.data);
3030
+ };
3031
+ this.mediaRecorder.onstop = function () {
3032
+ var _a;
3033
+ (_a = _this.stopCallback) === null || _a === void 0 ? void 0 : _a.call(_this, _this.audioChunks);
3034
+ _this.reset();
3035
+ };
3036
+ this.mediaRecorder.start();
3037
+ if (maxTime > 0) {
3038
+ this.startTimer({
3039
+ end: function () {
3040
+ _this.stop();
3041
+ }
3042
+ });
3043
+ }
3044
+ return [3 /*break*/, 3];
3045
+ case 2:
3046
+ error_1 = _d.sent();
3047
+ throw new Error(error_1);
3048
+ case 3: return [2 /*return*/];
3049
+ }
3050
+ });
3051
+ });
3052
+ };
3053
+ // 停止录音
3054
+ AudioRecorder.prototype.stop = function () {
3055
+ if (this.mediaRecorder) {
3056
+ this.audioChunks = [];
3057
+ this.mediaRecorder.stop();
3058
+ }
3059
+ };
3060
+ AudioRecorder.prototype.reset = function () {
3061
+ var _a, _b;
3062
+ this.audioChunks = [];
3063
+ (_b = (_a = this.stream) === null || _a === void 0 ? void 0 : _a.getTracks()) === null || _b === void 0 ? void 0 : _b.forEach(function (track) { return track.stop(); });
3064
+ };
3065
+ AudioRecorder.prototype.onStop = function (callback) {
3066
+ this.stopCallback = callback;
3067
+ };
3068
+ AudioRecorder.prototype.startTimer = function (options) {
3069
+ var _this = this;
3070
+ var _a = options || {}, next = _a.next, end = _a.end;
3071
+ this.timer = setInterval(function () {
3072
+ var _a, _b;
3073
+ next === null || next === void 0 ? void 0 : next();
3074
+ (_a = _this.timerCallback) === null || _a === void 0 ? void 0 : _a.call(_this, _this.timerCount);
3075
+ console.log('this.timerCount2: ', _this.timerCount);
3076
+ if (_this.timerCount === _this.options.maxTime) {
3077
+ _this.clearTimer();
3078
+ end === null || end === void 0 ? void 0 : end();
3079
+ (_b = _this.timerEndCallback) === null || _b === void 0 ? void 0 : _b.call(_this);
3080
+ }
3081
+ _this.timerCount++;
3082
+ }, 1000);
3083
+ };
3084
+ // 清除定时器
3085
+ AudioRecorder.prototype.clearTimer = function () {
3086
+ if (this.timer)
3087
+ clearInterval(this.timer);
3088
+ this.timer = null;
3089
+ this.timerCount = 0;
3090
+ };
3091
+ AudioRecorder.prototype.onTimer = function (callback) {
3092
+ this.timerCallback = callback;
3093
+ };
3094
+ AudioRecorder.prototype.onTimerEnd = function (callback) {
3095
+ this.timerEndCallback = callback;
3096
+ };
3097
+ AudioRecorder.prototype.getAudioBlob = function () {
3098
+ if (!this.mediaRecorder)
3099
+ return null;
3100
+ var type = this.getExtension(this.mediaRecorder.mimeType);
3101
+ return new Blob(this.audioChunks, { type: "audio/".concat(type) });
3102
+ };
3103
+ AudioRecorder.prototype.getAudioUrl = function () {
3104
+ var blob = this.getAudioBlob();
3105
+ if (!blob)
3106
+ return '';
3107
+ return URL.createObjectURL(blob);
3108
+ };
3109
+ AudioRecorder.prototype.getAudioBase64 = function (blob) {
3110
+ return new Promise(function (resolve, reject) {
3111
+ var reader = new FileReader();
3112
+ reader.onerror = reject;
3113
+ reader.onload = function () { return resolve(reader.result); };
3114
+ reader.readAsDataURL(blob);
3115
+ });
3116
+ };
3117
+ AudioRecorder.prototype.getAudioInfo = function () {
3118
+ var _this = this;
3119
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
3120
+ var audioContext, source, analyser, sampleRate, channels, bitDepth, error_2;
3121
+ var _a;
3122
+ return __generator(this, function (_b) {
3123
+ switch (_b.label) {
3124
+ case 0:
3125
+ _b.trys.push([0, 2, , 3]);
3126
+ audioContext = new (window.AudioContext || window.webkitAudioContext)();
3127
+ source = audioContext.createMediaStreamSource(this.stream);
3128
+ analyser = audioContext.createAnalyser();
3129
+ source.connect(analyser);
3130
+ sampleRate = audioContext.sampleRate;
3131
+ channels = ((_a = this.stream) === null || _a === void 0 ? void 0 : _a.getAudioTracks()[0].getSettings().channelCount) || 0;
3132
+ return [4 /*yield*/, this.getWavBitDepth(this.getAudioBlob())];
3133
+ case 1:
3134
+ bitDepth = _b.sent();
3135
+ resolve(__assign({ sampleRate: sampleRate, channels: channels, bitDepth: bitDepth }, this.audioInfo));
3136
+ return [3 /*break*/, 3];
3137
+ case 2:
3138
+ error_2 = _b.sent();
3139
+ reject(error_2);
3140
+ return [3 /*break*/, 3];
3141
+ case 3: return [2 /*return*/];
3142
+ }
3143
+ });
3144
+ }); });
3145
+ };
3146
+ AudioRecorder.prototype.getWavBitDepth = function (blob) {
3147
+ return new Promise(function (resolve, reject) {
3148
+ var reader = new FileReader();
3149
+ reader.onload = function (event) {
3150
+ var _a;
3151
+ var arrayBuffer = (_a = event.target) === null || _a === void 0 ? void 0 : _a.result;
3152
+ if (!arrayBuffer) {
3153
+ reject();
3154
+ throw new Error('未能读取 Blob 数据');
3155
+ }
3156
+ var data = new DataView(arrayBuffer);
3157
+ // 检查 "fmt " 区域,WAV文件的音频格式信息通常在此
3158
+ var fmtChunkOffset = 20; // "fmt " chunk 在 WAV 文件头的位置
3159
+ var bitDepth = data.getUint16(fmtChunkOffset + 14, true); // 16位表示小端存储
3160
+ resolve(bitDepth);
3161
+ };
3162
+ reader.readAsArrayBuffer(blob);
3163
+ });
3164
+ };
3165
+ AudioRecorder.prototype.getExtension = function (mimeType) {
3166
+ var mimeToExt = {
3167
+ 'audio/webm': 'webm',
3168
+ 'audio/webm;codecs=opus': 'webm',
3169
+ 'audio/ogg': 'ogg',
3170
+ 'audio/ogg;codecs=opus': 'ogg',
3171
+ 'audio/mp4': 'mp4',
3172
+ 'audio/mpeg': 'mp3',
3173
+ 'audio/wav': 'wav',
3174
+ };
3175
+ if (mimeToExt[mimeType])
3176
+ return mimeToExt[mimeType];
3177
+ var mainType = mimeType.split(';')[0];
3178
+ return mimeToExt[mainType] || 'audio';
3179
+ };
3180
+ // 上传音频到服务器
3181
+ AudioRecorder.prototype.upload = function (url, options) {
3182
+ return __awaiter(this, void 0, void 0, function () {
3183
+ var audioBlob, formData, response, data, error_3;
3184
+ return __generator(this, function (_a) {
3185
+ switch (_a.label) {
3186
+ case 0:
3187
+ audioBlob = this.getAudioBlob();
3188
+ if (!audioBlob)
3189
+ return [2 /*return*/, null];
3190
+ formData = new FormData();
3191
+ formData.append('audio', audioBlob, 'recording.mp4');
3192
+ _a.label = 1;
3193
+ case 1:
3194
+ _a.trys.push([1, 4, , 5]);
3195
+ return [4 /*yield*/, fetch(url, options)];
3196
+ case 2:
3197
+ response = _a.sent();
3198
+ return [4 /*yield*/, response.json()];
3199
+ case 3:
3200
+ data = _a.sent();
3201
+ return [2 /*return*/, data];
3202
+ case 4:
3203
+ error_3 = _a.sent();
3204
+ throw new Error(error_3);
3205
+ case 5: return [2 /*return*/];
3206
+ }
3207
+ });
3208
+ });
3209
+ };
3210
+ return AudioRecorder;
3211
+ }());
3212
+
2980
3213
  /**
2981
3214
  * 匹配中文字母数字
2982
3215
  */
@@ -3002,4 +3235,4 @@ var email = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$
3002
3235
  */
3003
3236
  var url = /^https?:\/\/[^\s/$.?#].[^\s]*$/;
3004
3237
 
3005
- export { HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useCountDown, useDownload, zhLetterNumber };
3238
+ export { AudioRecorder, HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useCountDown, useDownload, zhLetterNumber };
package/dist/index.umd.js CHANGED
@@ -1184,6 +1184,239 @@
1184
1184
  return StreamRequest;
1185
1185
  }());
1186
1186
 
1187
+ var AudioRecorder = /** @class */ (function () {
1188
+ function AudioRecorder(options) {
1189
+ this.options = {
1190
+ media: {
1191
+ audio: {
1192
+ channelCount: { ideal: 2 },
1193
+ sampleRate: { ideal: 44100 },
1194
+ echoCancellation: true,
1195
+ }
1196
+ },
1197
+ mediaRecorder: {
1198
+ mimeType: 'audio/mp4;codecs=mp4a.40.2',
1199
+ },
1200
+ maxTime: 0,
1201
+ };
1202
+ this.stream = null;
1203
+ this.mediaRecorder = null;
1204
+ this.audioChunks = [];
1205
+ this.audioInfo = {
1206
+ sampleRate: 0,
1207
+ channels: 0,
1208
+ format: null,
1209
+ bitDepth: 0,
1210
+ };
1211
+ this.timer = null;
1212
+ this.timerCount = 0;
1213
+ this.stopCallback = null;
1214
+ this.timerCallback = null;
1215
+ this.timerEndCallback = null;
1216
+ this.audioChunks = [];
1217
+ this.options = options;
1218
+ }
1219
+ // 开始录音
1220
+ AudioRecorder.prototype.start = function () {
1221
+ return __awaiter(this, void 0, void 0, function () {
1222
+ var _a, media, mediaRecorder, _b, maxTime, _c, error_1;
1223
+ var _this = this;
1224
+ return __generator(this, function (_d) {
1225
+ switch (_d.label) {
1226
+ case 0:
1227
+ _d.trys.push([0, 2, , 3]);
1228
+ _a = this.options, media = _a.media, mediaRecorder = _a.mediaRecorder, _b = _a.maxTime, maxTime = _b === void 0 ? 0 : _b;
1229
+ _c = this;
1230
+ return [4 /*yield*/, navigator.mediaDevices.getUserMedia({ audio: media.audio })];
1231
+ case 1:
1232
+ _c.stream = _d.sent();
1233
+ this.mediaRecorder = new MediaRecorder(this.stream, mediaRecorder);
1234
+ this.mediaRecorder.ondataavailable = function (event) {
1235
+ _this.audioInfo.format = event.data.type;
1236
+ _this.audioChunks.push(event.data);
1237
+ };
1238
+ this.mediaRecorder.onstop = function () {
1239
+ var _a;
1240
+ (_a = _this.stopCallback) === null || _a === void 0 ? void 0 : _a.call(_this, _this.audioChunks);
1241
+ _this.reset();
1242
+ };
1243
+ this.mediaRecorder.start();
1244
+ if (maxTime > 0) {
1245
+ this.startTimer({
1246
+ end: function () {
1247
+ _this.stop();
1248
+ }
1249
+ });
1250
+ }
1251
+ return [3 /*break*/, 3];
1252
+ case 2:
1253
+ error_1 = _d.sent();
1254
+ throw new Error(error_1);
1255
+ case 3: return [2 /*return*/];
1256
+ }
1257
+ });
1258
+ });
1259
+ };
1260
+ // 停止录音
1261
+ AudioRecorder.prototype.stop = function () {
1262
+ if (this.mediaRecorder) {
1263
+ this.audioChunks = [];
1264
+ this.mediaRecorder.stop();
1265
+ }
1266
+ };
1267
+ AudioRecorder.prototype.reset = function () {
1268
+ var _a, _b;
1269
+ this.audioChunks = [];
1270
+ (_b = (_a = this.stream) === null || _a === void 0 ? void 0 : _a.getTracks()) === null || _b === void 0 ? void 0 : _b.forEach(function (track) { return track.stop(); });
1271
+ };
1272
+ AudioRecorder.prototype.onStop = function (callback) {
1273
+ this.stopCallback = callback;
1274
+ };
1275
+ AudioRecorder.prototype.startTimer = function (options) {
1276
+ var _this = this;
1277
+ var _a = options || {}, next = _a.next, end = _a.end;
1278
+ this.timer = setInterval(function () {
1279
+ var _a, _b;
1280
+ next === null || next === void 0 ? void 0 : next();
1281
+ (_a = _this.timerCallback) === null || _a === void 0 ? void 0 : _a.call(_this, _this.timerCount);
1282
+ console.log('this.timerCount2: ', _this.timerCount);
1283
+ if (_this.timerCount === _this.options.maxTime) {
1284
+ _this.clearTimer();
1285
+ end === null || end === void 0 ? void 0 : end();
1286
+ (_b = _this.timerEndCallback) === null || _b === void 0 ? void 0 : _b.call(_this);
1287
+ }
1288
+ _this.timerCount++;
1289
+ }, 1000);
1290
+ };
1291
+ // 清除定时器
1292
+ AudioRecorder.prototype.clearTimer = function () {
1293
+ if (this.timer)
1294
+ clearInterval(this.timer);
1295
+ this.timer = null;
1296
+ this.timerCount = 0;
1297
+ };
1298
+ AudioRecorder.prototype.onTimer = function (callback) {
1299
+ this.timerCallback = callback;
1300
+ };
1301
+ AudioRecorder.prototype.onTimerEnd = function (callback) {
1302
+ this.timerEndCallback = callback;
1303
+ };
1304
+ AudioRecorder.prototype.getAudioBlob = function () {
1305
+ if (!this.mediaRecorder)
1306
+ return null;
1307
+ var type = this.getExtension(this.mediaRecorder.mimeType);
1308
+ return new Blob(this.audioChunks, { type: "audio/".concat(type) });
1309
+ };
1310
+ AudioRecorder.prototype.getAudioUrl = function () {
1311
+ var blob = this.getAudioBlob();
1312
+ if (!blob)
1313
+ return '';
1314
+ return URL.createObjectURL(blob);
1315
+ };
1316
+ AudioRecorder.prototype.getAudioBase64 = function (blob) {
1317
+ return new Promise(function (resolve, reject) {
1318
+ var reader = new FileReader();
1319
+ reader.onerror = reject;
1320
+ reader.onload = function () { return resolve(reader.result); };
1321
+ reader.readAsDataURL(blob);
1322
+ });
1323
+ };
1324
+ AudioRecorder.prototype.getAudioInfo = function () {
1325
+ var _this = this;
1326
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
1327
+ var audioContext, source, analyser, sampleRate, channels, bitDepth, error_2;
1328
+ var _a;
1329
+ return __generator(this, function (_b) {
1330
+ switch (_b.label) {
1331
+ case 0:
1332
+ _b.trys.push([0, 2, , 3]);
1333
+ audioContext = new (window.AudioContext || window.webkitAudioContext)();
1334
+ source = audioContext.createMediaStreamSource(this.stream);
1335
+ analyser = audioContext.createAnalyser();
1336
+ source.connect(analyser);
1337
+ sampleRate = audioContext.sampleRate;
1338
+ channels = ((_a = this.stream) === null || _a === void 0 ? void 0 : _a.getAudioTracks()[0].getSettings().channelCount) || 0;
1339
+ return [4 /*yield*/, this.getWavBitDepth(this.getAudioBlob())];
1340
+ case 1:
1341
+ bitDepth = _b.sent();
1342
+ resolve(__assign({ sampleRate: sampleRate, channels: channels, bitDepth: bitDepth }, this.audioInfo));
1343
+ return [3 /*break*/, 3];
1344
+ case 2:
1345
+ error_2 = _b.sent();
1346
+ reject(error_2);
1347
+ return [3 /*break*/, 3];
1348
+ case 3: return [2 /*return*/];
1349
+ }
1350
+ });
1351
+ }); });
1352
+ };
1353
+ AudioRecorder.prototype.getWavBitDepth = function (blob) {
1354
+ return new Promise(function (resolve, reject) {
1355
+ var reader = new FileReader();
1356
+ reader.onload = function (event) {
1357
+ var _a;
1358
+ var arrayBuffer = (_a = event.target) === null || _a === void 0 ? void 0 : _a.result;
1359
+ if (!arrayBuffer) {
1360
+ reject();
1361
+ throw new Error('未能读取 Blob 数据');
1362
+ }
1363
+ var data = new DataView(arrayBuffer);
1364
+ // 检查 "fmt " 区域,WAV文件的音频格式信息通常在此
1365
+ var fmtChunkOffset = 20; // "fmt " chunk 在 WAV 文件头的位置
1366
+ var bitDepth = data.getUint16(fmtChunkOffset + 14, true); // 16位表示小端存储
1367
+ resolve(bitDepth);
1368
+ };
1369
+ reader.readAsArrayBuffer(blob);
1370
+ });
1371
+ };
1372
+ AudioRecorder.prototype.getExtension = function (mimeType) {
1373
+ var mimeToExt = {
1374
+ 'audio/webm': 'webm',
1375
+ 'audio/webm;codecs=opus': 'webm',
1376
+ 'audio/ogg': 'ogg',
1377
+ 'audio/ogg;codecs=opus': 'ogg',
1378
+ 'audio/mp4': 'mp4',
1379
+ 'audio/mpeg': 'mp3',
1380
+ 'audio/wav': 'wav',
1381
+ };
1382
+ if (mimeToExt[mimeType])
1383
+ return mimeToExt[mimeType];
1384
+ var mainType = mimeType.split(';')[0];
1385
+ return mimeToExt[mainType] || 'audio';
1386
+ };
1387
+ // 上传音频到服务器
1388
+ AudioRecorder.prototype.upload = function (url, options) {
1389
+ return __awaiter(this, void 0, void 0, function () {
1390
+ var audioBlob, formData, response, data, error_3;
1391
+ return __generator(this, function (_a) {
1392
+ switch (_a.label) {
1393
+ case 0:
1394
+ audioBlob = this.getAudioBlob();
1395
+ if (!audioBlob)
1396
+ return [2 /*return*/, null];
1397
+ formData = new FormData();
1398
+ formData.append('audio', audioBlob, 'recording.mp4');
1399
+ _a.label = 1;
1400
+ case 1:
1401
+ _a.trys.push([1, 4, , 5]);
1402
+ return [4 /*yield*/, fetch(url, options)];
1403
+ case 2:
1404
+ response = _a.sent();
1405
+ return [4 /*yield*/, response.json()];
1406
+ case 3:
1407
+ data = _a.sent();
1408
+ return [2 /*return*/, data];
1409
+ case 4:
1410
+ error_3 = _a.sent();
1411
+ throw new Error(error_3);
1412
+ case 5: return [2 /*return*/];
1413
+ }
1414
+ });
1415
+ });
1416
+ };
1417
+ return AudioRecorder;
1418
+ }());
1419
+
1187
1420
  /**
1188
1421
  * 匹配中文字母数字
1189
1422
  */
@@ -1209,6 +1442,7 @@
1209
1442
  */
1210
1443
  var url = /^https?:\/\/[^\s/$.?#].[^\s]*$/;
1211
1444
 
1445
+ exports.AudioRecorder = AudioRecorder;
1212
1446
  exports.InputLegalityValidate = InputLegalityValidate;
1213
1447
  exports.InputLengthValidate = InputLengthValidate;
1214
1448
  exports.Mask = Mask;
@@ -1,20 +1,42 @@
1
1
  interface AudioRecorderOptions {
2
- channelCount: number;
2
+ media: MediaStreamConstraints;
3
+ mediaRecorder?: MediaRecorderOptions;
4
+ maxTime?: number;
5
+ }
6
+ interface TimerOptions {
7
+ next?: () => void;
8
+ end?: () => void;
9
+ }
10
+ interface AudioInfo {
3
11
  sampleRate: number;
4
- echoCancellation: boolean;
5
- mimeType: string;
12
+ channels: number;
13
+ format: string | null;
14
+ bitDepth: number;
6
15
  }
7
16
  declare class AudioRecorder {
8
17
  private options;
9
18
  private stream;
10
19
  private mediaRecorder;
11
20
  private audioChunks;
21
+ private audioInfo;
22
+ private timer;
23
+ private timerCount;
24
+ private timerCallback;
25
+ private timerEndCallback;
12
26
  constructor(options: AudioRecorderOptions);
13
27
  start(): Promise<void>;
14
28
  stop(): void;
15
29
  reset(): void;
16
- getAudioBlob(): Blob;
30
+ onStop(): void;
31
+ startTimer(options?: TimerOptions): void;
32
+ clearTimer(): void;
33
+ onTimer(callback: () => void): void;
34
+ onTimerEnd(callback: () => void): void;
35
+ getAudioBlob(): Blob | null;
17
36
  getAudioUrl(): string;
18
- getAudioBase64(): string;
19
- uploadAudio(): Promise<void>;
37
+ getAudioBase64(blob: Blob): Promise<string>;
38
+ getAudioInfo(): Promise<unknown>;
39
+ getWavBitDepth(blob: Blob): Promise<number>;
40
+ getExtension(mimeType: string): string;
41
+ upload(url: string, options?: RequestInit): Promise<any>;
20
42
  }
@@ -9,3 +9,4 @@ export * from './number';
9
9
  export * from './array';
10
10
  export * from './url';
11
11
  export * from './sse';
12
+ export * from './recorder';
@@ -0,0 +1,44 @@
1
+ export interface AudioRecorderOptions {
2
+ media: MediaStreamConstraints;
3
+ mediaRecorder?: MediaRecorderOptions;
4
+ maxTime?: number;
5
+ }
6
+ interface TimerOptions {
7
+ next?: () => void;
8
+ end?: () => void;
9
+ }
10
+ export interface AudioInfo {
11
+ sampleRate: number;
12
+ channels: number;
13
+ format: string | null;
14
+ bitDepth: number;
15
+ }
16
+ export declare class AudioRecorder {
17
+ private options;
18
+ private stream;
19
+ private mediaRecorder;
20
+ private audioChunks;
21
+ private audioInfo;
22
+ private timer;
23
+ private timerCount;
24
+ private stopCallback;
25
+ private timerCallback;
26
+ private timerEndCallback;
27
+ constructor(options: AudioRecorderOptions);
28
+ start(): Promise<void>;
29
+ stop(): void;
30
+ reset(): void;
31
+ onStop(callback: (audioChunks: Blob[]) => void): void;
32
+ startTimer(options?: TimerOptions): void;
33
+ clearTimer(): void;
34
+ onTimer(callback: (count: number) => void): void;
35
+ onTimerEnd(callback: () => void): void;
36
+ getAudioBlob(): Blob | null;
37
+ getAudioUrl(): string;
38
+ getAudioBase64(blob: Blob): Promise<string>;
39
+ getAudioInfo(): Promise<unknown>;
40
+ getWavBitDepth(blob: Blob): Promise<number>;
41
+ getExtension(mimeType: string): string;
42
+ upload(url: string, options?: RequestInit): Promise<any>;
43
+ }
44
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightsoft/js-sdk",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "",
5
5
  "main": "dist/index.umd.js",
6
6
  "module": "dist/index.js",