answerDetail.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <view class="answerdetail-page">
  3. <view class="detail-title">
  4. <text>{{ questionItem.text }}</text>
  5. <text>{{ questionItem.time }}</text>
  6. </view>
  7. <view class="recode">
  8. 麦克风
  9. {{audioTime}}
  10. </view>
  11. <view class="recode-btn">
  12. <view @click="handleAudio(1)">开始录音</view>
  13. <view @click="handleAudio(2)">暂停录音</view>
  14. <view @click="handleAudio(3)">停止录音</view>
  15. <view @click="handleAudio(4)">播放录音</view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. const moment=require('@/utils/moment-with-locales.min')
  21. moment.locale('zh-cn');
  22. export default {
  23. data() {
  24. return {
  25. questionItem: {
  26. id: 1,
  27. text: '疫情下全球苯乙烯市场有什么动荡和影响',
  28. time: '2022.5.23 14:40',
  29. source: 'xxx.mp3',
  30. isAnswer: 1,//1未回答,2已回答
  31. status: 1,//1:未录音;2:正在录音;3:已暂停;4:完成录音
  32. },
  33. innerAudio: null,//该页面的音频
  34. audioCount:0,
  35. audioTime:'00:00',
  36. timer:null
  37. };
  38. },
  39. onLoad() {
  40. this.initAudio()
  41. },
  42. onUnload(){
  43. this.destoryAudio()
  44. },
  45. methods: {
  46. //初始化audio
  47. initAudio() {
  48. this.innerAudio = uni.createInnerAudioContext()
  49. this.handleAudioFun()
  50. this.handleRecorderFun()
  51. },
  52. //销毁audio
  53. destoryAudio() {
  54. if (this.innerAudio) {
  55. this.innerAudio.destroy()
  56. }
  57. },
  58. handleAudioFun(){
  59. this.innerAudio.onPlay(()=>{
  60. console.log('播放录音了')
  61. })
  62. },
  63. handleRecorderFun(){
  64. this.globalRecorder.onStart(()=>{
  65. console.log('开始录音')
  66. })
  67. this.globalRecorder.onPause(()=>{
  68. console.log('暂停录音')
  69. this.cleanTime()
  70. })
  71. this.globalRecorder.onStop((res)=>{
  72. console.log('录音完成')
  73. console.log('res',JSON.stringify(res))
  74. this.innerAudio.src = res.tempFilePath
  75. this.cleanTime()
  76. })
  77. this.globalRecorder.onFrameRecorded((res)=>{
  78. console.log('?',res)
  79. })
  80. },
  81. handleAudio(type) {
  82. //根据questionItem.status
  83. if (type === 1) {
  84. this.globalRecorder.start({ duration: 180000,frameSize:'1' })
  85. this.clockTime()
  86. } else if (type === 2) {
  87. this.globalRecorder.pause()
  88. } else if (type === 3) {
  89. this.globalRecorder.stop()
  90. this.cleanTime()
  91. }else{
  92. this.innerAudio.play()
  93. }
  94. },
  95. clockTime(){
  96. console.log('开始计时')
  97. this.timer = setInterval(()=>{
  98. if(this.timer){
  99. this.audioCount++
  100. this.audioTime = moment(this.audioCount*1000).format('mm:ss')
  101. }
  102. },1000)
  103. },
  104. cleanTime(){
  105. this.timer = null
  106. this.audioTime = moment(this.audioCount*1000).format('mm:ss')
  107. }
  108. },
  109. };
  110. </script>
  111. <style scoped lang="scss">
  112. </style>