RecorderManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // #ifdef H5
  2. import Recorder from '../lib/recorder.mp3.min.js';
  3. // #endif
  4. export default class RecorderManager {
  5. recorder = null;
  6. //录音开始时间
  7. uniStartTime = null;
  8. //语音录音中
  9. recording = false;
  10. onRecordingComplete = null;
  11. constructor() {
  12. // #ifdef H5
  13. this.recorder = Recorder({
  14. type : 'mp3',
  15. sampleRate:16000,//录音的采样率,越大细节越丰富越细腻
  16. bitRate:16,//录音的比特率,越大音质越好
  17. onProcess : function () {}
  18. });
  19. // #endif
  20. // #ifndef H5
  21. this.recorder = uni.getRecorderManager();
  22. // #endif
  23. }
  24. onRecordComplete(callBack) {
  25. this.onRecordingComplete = callBack;
  26. // #ifndef H5
  27. this.initUniRecorder();
  28. // #endif
  29. }
  30. initUniRecorder() {
  31. //录音结束后,发送
  32. this.recorder.onStop((res) => {
  33. const duration = Date.now() - this.uniStartTime;
  34. res.duration = duration;
  35. this.onRecordingComplete(res, duration);
  36. });
  37. }
  38. authorize() {
  39. return new Promise((resolve,reject) => {
  40. // #ifdef H5
  41. this.recorder.open(() => {
  42. resolve();
  43. }, (e) => {
  44. this.recorder.close();
  45. reject(e);
  46. });
  47. // #endif
  48. // #ifdef APP
  49. if (plus.os.name === "iOS") {
  50. var avaudiosession = plus.ios.import("AVAudioSession");
  51. var avaudio = avaudiosession.sharedInstance();
  52. var permissionStatus = avaudio.recordPermission();
  53. if (permissionStatus === 1684369017 || permissionStatus === 1970168948) {
  54. console.log("麦克风权限没有开启");
  55. }
  56. plus.ios.deleteObject(avaudiosession);
  57. resolve()
  58. } else {
  59. plus.android.requestPermissions(
  60. ['android.permission.RECORD_AUDIO'],
  61. (resultObj) => {
  62. resultObj.granted.length ? resolve() : reject("已拒绝");
  63. },
  64. (error) => {
  65. console.error(`申请权限错误:${error.code} = ${error.message}`);
  66. reject();
  67. }
  68. );
  69. }
  70. // #endif
  71. // #ifdef MP
  72. if (uni.authorize) {
  73. uni.authorize({
  74. scope: 'scope.record',
  75. success() {
  76. resolve()
  77. },
  78. fail: (e) => {
  79. reject(e)
  80. }
  81. });
  82. }
  83. // #endif
  84. });
  85. }
  86. start() {
  87. this.recording = true;
  88. // #ifdef H5
  89. this.recorder.start();
  90. // #endif
  91. // #ifndef H5
  92. try {
  93. // 更多配置参考uniapp:https://uniapp.dcloud.net.cn/api/media/record-manager.html#getrecordermanager
  94. this.recorder.start({
  95. duration: 600000 // 指定录音的时长,单位 ms
  96. });
  97. this.uniStartTime = Date.now();
  98. } catch (e) {
  99. console.log(e);
  100. }
  101. // #endif
  102. }
  103. stop() {
  104. this.recording = false;
  105. // #ifdef H5
  106. this.recorder.stop((blob, duration) => {
  107. const file = new File([blob], 'audio.mp3', {type: blob.type});
  108. file.tempFilePath = URL.createObjectURL(blob);
  109. file.duration = duration;
  110. this.onRecordingComplete(file, duration);
  111. }, (msg) => {
  112. console.log('录音失败:',msg)
  113. })
  114. // #endif
  115. // #ifndef H5
  116. try {
  117. this.recorder.stop();
  118. } catch (e) {
  119. console.log(e);
  120. }
  121. // #endif
  122. }
  123. }