recording.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. export default{
  41. props:{
  42. },
  43. computed:{},
  44. data(){
  45. return{
  46. isRecorder:false,
  47. rec: null,
  48. powerLevel:0,
  49. time : 0,
  50. isPopup : false,
  51. recorder:uni.getRecorderManager(),
  52. recordTimer:0
  53. }
  54. },
  55. created () {
  56. this.recOpen();
  57. },
  58. methods:{
  59. recOpen(){
  60. //开始录音
  61. this.recorder.onStart(()=>{
  62. this.time = 0;
  63. this.powerLevel = 0;
  64. this.isRecorder = true;
  65. this.recordTimer = setInterval(()=>{
  66. this.time = this.time + 100;
  67. this.powerLevel = Math.floor(Math.random() * 100 );
  68. },100)
  69. });
  70. //录音结束
  71. this.recorder.onStop( (res)=> {
  72. if(this.recordTimer > 0){
  73. clearInterval(this.recordTimer);
  74. }
  75. this.isRecorder = false;
  76. if(this.time < 1000) {
  77. this.utils.Tip("说话时间太短了");
  78. return;
  79. }
  80. uni.getFileSystemManager().readFile({
  81. filePath: res.tempFilePath,
  82. encoding: 'base64',
  83. success: res2=>{
  84. console.log(res2);
  85. }
  86. });
  87. //this.$emit('end',{data:(/.+;\s*base64\s*,\s*(.+)$/i.exec(reader.result)||[])[1],duration:duration});
  88. console.log('recorder stop' + JSON.stringify(res));
  89. this.voicePath = res.tempFilePath;
  90. });
  91. //
  92. this.recorder.onError(()=>{
  93. this.utils.Tip("录音失败");
  94. });
  95. },
  96. open(){
  97. this.isPopup = true;
  98. },
  99. hide(){
  100. this.isPopup = false;
  101. },
  102. //打开录音
  103. recStart(){
  104. this.recorder.start({ sampleRate:16000,format:"mp3"});
  105. },
  106. /**
  107. * 停止录音
  108. */
  109. recStop(){
  110. this.recorder.stop();
  111. },
  112. //关闭录音
  113. recClose(){
  114. },
  115. getTime:function(){
  116. let seconds = parseInt(this.time / 1000);
  117. if(seconds <= 60) {
  118. return "00:" + (seconds < 10 ? ("0" + seconds) : seconds );
  119. }
  120. let seconds1 = parseInt(seconds / 60);
  121. let seconds2 = parseInt(seconds % 60);
  122. return (seconds1 < 10 ? ("0" + seconds1) : seconds1 ) + ":" + (seconds2 < 10 ? ("0" + seconds2) : seconds2 );
  123. }
  124. }
  125. }
  126. </script>