handle.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import SoundStatus from '../const/soundStatus';
  2. import SoundRecording, {TriggerListener} from '$utils/tool/sound-recording';
  3. import popup from '$utils/tool/popup';
  4. import file from '$utils/tool/file';
  5. import AudioStatus from "$components/sound-recording/const/audio";
  6. import Status from "$mixins/status/const/status";
  7. export default <LibMixins>{
  8. data(){
  9. return {
  10. SoundStatus,
  11. soundStatus:SoundStatus.none,
  12. soundStatusMessage:{
  13. [SoundStatus.none]:'长按开始录音',
  14. [SoundStatus.sound]:'正在录音',
  15. [SoundStatus.jurisdiction]:'浏览器授权后,请点击重试',
  16. [SoundStatus.support]:'暂不支持'
  17. },
  18. countdown:0,
  19. support:true,
  20. assets:false
  21. }
  22. },
  23. methods:{
  24. // 设置当前录音状态
  25. setSoundStatus(status:SoundStatus){
  26. if (this.soundStatus !== status) {
  27. // 如果状态为 none 清楚资源
  28. if (status === SoundStatus.none) {
  29. this.clearAssets();
  30. }
  31. this.soundStatus = status;
  32. }
  33. },
  34. reRecording(){
  35. if (this.status === Status.loading) return;
  36. if (!this.soundRecording) this.installUseSoundRecording();
  37. return this.setSoundStatus(SoundStatus.none);
  38. },
  39. // 设置授权状态
  40. setJurisdictionStatus(config:Record<string, any>){
  41. if (config.support === false) {
  42. return this.setSoundStatus(SoundStatus.support);
  43. }
  44. if (config.jurisdiction === false ) {
  45. return this.setSoundStatus(SoundStatus.jurisdiction);
  46. }
  47. },
  48. start(){
  49. if (this.soundStatus === SoundStatus.none ) {
  50. return this.soundRecording.start();
  51. }
  52. },
  53. stop(){
  54. if (this.soundStatus === SoundStatus.sound ) {
  55. return this.soundRecording.stop();
  56. }
  57. },
  58. // 安装
  59. installSoundRecording(){
  60. if (this.soundRecording.initializationStatus) return;
  61. // 安装
  62. this.soundRecording.initialization().then(()=>{
  63. this.setJurisdictionStatus({
  64. jurisdiction:true,
  65. support:true
  66. });
  67. this.soundRecording
  68. .addListener(TriggerListener.fail,(message)=>{
  69. // 设置状态从头开始
  70. this.setSoundStatus(SoundStatus.none);
  71. message && popup.$toast(message);
  72. })
  73. .addListener(TriggerListener.speed,(duration)=>{
  74. if (this.countdown !== duration) {
  75. this.countdown = duration;
  76. }
  77. })
  78. .addListener(TriggerListener.soundStatus,(status:boolean)=>{
  79. this.setSoundStatus(status ? SoundStatus.sound : SoundStatus.end);
  80. })
  81. .addListener(TriggerListener.success,(data)=>{
  82. if (this.assets) return;
  83. // 设置资源
  84. this.setAssets(data.blob);
  85. this.setAudioStatus(AudioStatus.loading);
  86. // 设置路径
  87. file.createdPath(data.blob).then((data)=>{
  88. return this.switchSrc(data);
  89. }).catch((error)=>{
  90. return this.setAudioStatus(AudioStatus.noURL);
  91. });
  92. })
  93. }).catch((fail)=>{
  94. if (typeof fail === 'object') {
  95. this.setJurisdictionStatus(fail);
  96. }
  97. });
  98. },
  99. installUseSoundRecording(){
  100. // 创建录音对象
  101. this.soundRecording = new SoundRecording({
  102. min: this.min,
  103. max: this.max
  104. });
  105. this.installSoundRecording();
  106. },
  107. // 设置资源
  108. setAssets(blob){
  109. if (blob) {
  110. this.stroageBlob = blob;
  111. this.assets = true;
  112. this.stroageUpload = null;
  113. if (!this.upload) {
  114. this.$emit('update:value',blob);
  115. }
  116. }
  117. },
  118. // 清楚资源
  119. clearAssets(){
  120. if (this.assets) {
  121. this.assets = false;
  122. this.stroageBlob = null;
  123. this.stroageUpload = null;
  124. if (!this.upload) {
  125. this.$emit('update:value',undefined);
  126. }
  127. }
  128. }
  129. },
  130. created(){
  131. !this.src && this.installUseSoundRecording();
  132. },
  133. beforeUnmount(){
  134. this.soundRecording && this.soundRecording.destroy();
  135. this.soundRecording = null;
  136. }
  137. }