recordingH5.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view class="app">
  3. <view class="sub-recording fx-h fx-bc fx-ac" v-if="isPopup">
  4. <view class="title">按住说话</view>
  5. <image @longpress="recStart" @touchend="recStop" class="icon" src="/static/img/hold_talk.png"></image>
  6. </view>
  7. <view class="app-win fx-h fx-bc fx-ac" v-if="isRecorder">
  8. <view class="recording-wav">
  9. <view class="hr f-h">
  10. <view class="top" v-if="powerLevel >= 100">
  11. <view class="vc"></view>
  12. </view>
  13. <view class="v" :style="'height:' + (22 * (powerLevel / 100)) + 'px'"></view>
  14. <view class="foot" v-if="powerLevel > 0">
  15. <view class="vc"></view>
  16. </view>
  17. </view>
  18. <image src="/static/img/zeffect_recordbutton__recordview_bottom.png"></image>
  19. </view>
  20. <view class="time">{{getTime()}}</view>
  21. </view>
  22. </view>
  23. </template>
  24. <style>
  25. .sub-recording{padding: 20px 0;}
  26. .sub-recording .title{color: #787878;padding: 10px 0;font-size: 14px;}
  27. .sub-recording .icon{width: 60px;height: 60px;}
  28. .app-win{background: rgba(0, 0, 0, 0.8);width: 40vw;height: 40vw;position: fixed;top: calc(50% - 30vw);left: calc(50% - 20vw);border-radius: 6px;}
  29. .app-win .recording-wav{width: 60px;height: 60px;position: relative;}
  30. .app-win .recording-wav image{width: 60px;height: 60px;}
  31. .app-win .recording-wav .hr{width: 25px;left:17px;bottom: 20px;position: absolute;z-index: 9;}
  32. .app-win .recording-wav .hr .v{background: #2fbec0;}
  33. .app-win .recording-wav .hr .foot{height: 6px;overflow: hidden;position: relative}
  34. .app-win .recording-wav .hr .foot .vc{width:26px;height: 26px; border-radius:0px 0px 26px 26px;background: #2fbec0;position: absolute;bottom: 0;}
  35. .app-win .recording-wav .hr .top{height: 6px;overflow: hidden;position: relative;}
  36. .app-win .recording-wav .hr .top .vc{width:26px;height: 26px; border-radius:26px 26px 0px 0px;background: #2fbec0;position: absolute;top: 0;}
  37. .app-win .time{margin:10px 0;color: #fff;}
  38. </style>
  39. <script>
  40. import Recorder from 'recorder-core';
  41. import 'recorder-core/src/engine/mp3';
  42. import 'recorder-core/src/engine/mp3-engine';
  43. export default{
  44. props:{},
  45. name:"recordingH5",
  46. computed:{},
  47. data(){
  48. return{
  49. isRecorder:false,
  50. rec: null,
  51. powerLevel:0,
  52. time : 0,
  53. isPopup : false
  54. }
  55. },
  56. created () {
  57. this.recOpen();
  58. },
  59. methods:{
  60. recOpen(){
  61. },
  62. open(){
  63. this.isPopup = true;
  64. },
  65. hide(){
  66. this.isPopup = false;
  67. },
  68. //打开录音
  69. recStart(){
  70. this.rec = this.rec=Recorder({
  71. type:"mp3",
  72. bitRate:16,
  73. sampleRate:16000,
  74. onProcess:(buffers,powerLevel,duration,sampleRate)=>{
  75. //console.log(powerLevel);
  76. this.powerLevel = powerLevel;
  77. this.time = duration;
  78. }
  79. });
  80. this.rec.open(()=>{
  81. console.log("已打开:mp3 16000hz 16kbps");
  82. this.time = 0;
  83. this.powerLevel = 0;
  84. this.isRecorder = true;
  85. this.rec.start();
  86. },(msg,isUserNotAllow)=>{
  87. console.log((isUserNotAllow?"UserNotAllow,":"")+"打开失败:"+msg);
  88. });
  89. },
  90. /**
  91. * 停止录音
  92. */
  93. recStop(){
  94. if(!(this.rec&&Recorder.IsOpen())){
  95. return false;
  96. }
  97. this.isRecorder = false;
  98. if(this.time < 1000) {
  99. this.utils.Tip("说话时间太短了");
  100. return;
  101. }
  102. this.rec.stop((blob,duration)=>{
  103. var reader=new FileReader();
  104. reader.onloadend=() => {
  105. this.$emit('end',{data:(/.+;\s*base64\s*,\s*(.+)$/i.exec(reader.result)||[])[1],duration:duration});
  106. };
  107. reader.readAsDataURL(blob);
  108. this.recClose();
  109. //new window.File([blob, "recording.mp3", {type: ""}]);
  110. //this.$emit('end',{data:(window.URL||webkitURL).createObjectURL(blob),duration:duration})
  111. },()=>{
  112. this.utils.Tip("按住说话失败");
  113. });
  114. },
  115. //关闭录音
  116. recClose(){
  117. if(this.rec != null) {
  118. this.rec.close();
  119. this.rec = null;
  120. }
  121. },
  122. getTime:function(){
  123. let seconds = parseInt(this.time / 1000);
  124. if(seconds <= 60) {
  125. return "00:" + (seconds < 10 ? ("0" + seconds) : seconds );
  126. }
  127. let seconds1 = parseInt(seconds / 60);
  128. let seconds2 = parseInt(seconds % 60);
  129. return (seconds1 < 10 ? ("0" + seconds1) : seconds1 ) + ":" + (seconds2 < 10 ? ("0" + seconds2) : seconds2 );
  130. }
  131. }
  132. }
  133. </script>