voice.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <view class="container">
  3. <view class="textbox">
  4. <view class="header-tips">请朗读以下文字</view>
  5. <view class="textbox-con" :style="{height: textHeight}">
  6. 在这片神奇的森林里,小鸟轻快地唱着歌,仿佛在诉说着春天的故事。
  7. </view>
  8. </view>
  9. <view class="voice-footer">
  10. <view class="voice-footer-tips">1、选择安静的录音环境,可在房间或车内录音。</view>
  11. <view class="voice-footer-tips">2、保持20cm距离,避免手机太远录音不清晰。</view>
  12. <view class="voice-footer-tips">3、使用普通话朗读,语速适中,吐字清晰。</view>
  13. <view class="voice-footer-btnbox">
  14. <view class="btnbox-item" :style="{visibility: voicePath ? 'visible' : 'hidden',color: status=='start' ? '#ccc !important':''}">
  15. <view class="iconsbox" @click="playVoice" :style="{borderColor: status=='start' ? '#ccc' :isVoicePlay ? 'red':''}">
  16. <u-icon name="volume-fill" size="30" :color="status=='start' ? '#ccc' :isVoicePlay ? 'red':'#757575'"></u-icon>
  17. <!-- <uni-icons type="sound-filled" size="30" :color="status=='start' ? '#ccc' :isVoicePlay ? 'red':'#757575'"></uni-icons> -->
  18. </view>
  19. <view>试听录音</view>
  20. </view>
  21. <view class="btnbox-item">
  22. <view class="iconsbox iconsbox-voice" :style="{backgroundColor: status=='end' ? 'red':''}" @click="handleRecord">
  23. <!-- <uni-icons v-show="status=='stop'|| status=='end'" type="mic-filled" size="35" color="#fff"></uni-icons> -->
  24. <u-icon name="mic" v-show="status=='stop'|| status=='end'" size="35" color="#fff"></u-icon>
  25. <image v-show="status=='start'" src="https://obs.jnmyunl.com/fs/20250813/1755047925090.png" mode="aspectFill"></image>
  26. </view>
  27. <view v-show="status=='stop'">点击录制</view>
  28. <view v-show="status=='start'">点击停止</view>
  29. <view v-show="status=='end'">重新录制</view>
  30. </view>
  31. <view class="btnbox-item" :style="{visibility: voicePath ? 'visible' : 'hidden',color: status=='start' ? '#ccc !important':''}">
  32. <button class="iconsbox" :disabled="btnLoading" @click="onSubmit">
  33. <!-- <uni-icons type="checkmarkempty" size="30" :color="status=='start' ? '#ccc' : '#757575'"></uni-icons> -->
  34. <u-icon name="checkmark" size="30" :color="status=='start' ? '#ccc' : '#757575'"></u-icon>
  35. </button>
  36. <view>提交</view>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import {addVoicePrintUrl} from '@/api/companyUser'
  44. let innerAudioContext = null
  45. export default {
  46. data() {
  47. return {
  48. recorderManager: null,
  49. // innerAudioContext: null,
  50. textHeight: '',
  51. statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
  52. screenHeight: uni.getSystemInfoSync().windowHeight + 'px',
  53. voicePath: '',
  54. status: "stop",
  55. isVoicePlay: false,
  56. btnLoading: false,
  57. companyUserId:null
  58. }
  59. },
  60. onLoad(option) {
  61. console.log('==onLoad==',this.recorderManager)
  62. // #ifndef H5
  63. this.recorderManager = uni.getRecorderManager();
  64. innerAudioContext = uni.createInnerAudioContext();
  65. this.companyUserId=option.companyUserId
  66. let self = this;
  67. console.log('==recorderManager22222222==',this.recorderManager)
  68. if(this.recorderManager) {
  69. console.log('==recorderManager==')
  70. this.recorderManager.onStart(()=>{
  71. console.log('==start==')
  72. this.status = 'start'
  73. })
  74. this.recorderManager.onStop((res)=> {
  75. console.log('recorder stop' + JSON.stringify(res));
  76. this.status = 'end'
  77. self.voicePath = res.tempFilePath;
  78. });
  79. this.recorderManager.onError((res)=> {
  80. console.log('onError ==' + JSON.stringify(res));
  81. });
  82. }
  83. if(innerAudioContext) {
  84. // innerAudioContext.autoplay = true;
  85. innerAudioContext.onPlay(() => {
  86. // console.log('开始播放');
  87. this.isVoicePlay = true
  88. });
  89. innerAudioContext.onStop(() => {
  90. // console.log('停止播放');
  91. this.isVoicePlay = false
  92. });
  93. innerAudioContext.onEnded(() => {
  94. // console.log('播放结束');
  95. this.isVoicePlay = false
  96. });
  97. innerAudioContext.onError((res) => {
  98. // console.log('播放res',res);
  99. this.isVoicePlay = false
  100. });
  101. innerAudioContext.onPause(() => {
  102. // console.log('暂停播放');
  103. this.isVoicePlay = false
  104. });
  105. }
  106. // #endif
  107. },
  108. onReady() {
  109. const query = uni.createSelectorQuery().in(this);
  110. query
  111. .select(".voice-footer")
  112. .boundingClientRect((data) => {
  113. this.textHeight = `calc(${this.screenHeight} - ${data.height}px - ${this.statusBarHeight} - 45px - ${uni.upx2px(120)}px)`
  114. })
  115. .exec();
  116. },
  117. onHide() {
  118. if(innerAudioContext){
  119. innerAudioContext.pause();
  120. }
  121. },
  122. onUnload() {
  123. this.recorderManager = null
  124. if(innerAudioContext) {
  125. innerAudioContext.destroy()
  126. innerAudioContext = null
  127. }
  128. },
  129. methods: {
  130. handleRecord() {
  131. console.log('handleRecord',this.status)
  132. if(innerAudioContext){
  133. innerAudioContext.pause();
  134. }
  135. if(this.status == 'stop') {
  136. this.startRecord()
  137. } else if(this.status == 'start') {
  138. this.endRecord()
  139. } else if(this.status == 'end') {
  140. this.startRecord()
  141. }
  142. },
  143. startRecord() {
  144. console.log('this.recorderManager',this.recorderManager)
  145. // console.log('开始录音');
  146. // 声音采集限制10秒
  147. this.recorderManager.start({
  148. format: 'mp3',
  149. duration: 10000
  150. });
  151. },
  152. endRecord() {
  153. // console.log('录音结束');
  154. this.recorderManager.stop();
  155. },
  156. playVoice() {
  157. console.log(innerAudioContext)
  158. if(this.status == "start") return
  159. if (this.voicePath) {
  160. if(this.isVoicePlay == false) {
  161. innerAudioContext.src = this.voicePath;
  162. innerAudioContext.play();
  163. } else {
  164. innerAudioContext.stop();
  165. }
  166. }
  167. },
  168. onSubmit() {
  169. console.log("==onSubmit=",this.status)
  170. if(this.status == "start") return
  171. this.btnLoading = true
  172. uni.showLoading({
  173. title:"提交中..."
  174. })
  175. uni.uploadFile({
  176. url: uni.getStorageSync('requestPath')+'/app/common/uploadOSS', //仅为示例,非真实的接口地址
  177. filePath: this.voicePath,
  178. name: 'file',
  179. success: (uploadFileRes) => {
  180. console.log(JSON.parse(uploadFileRes.data).url)
  181. console.log(111,this.companyUserId)
  182. let voicePrintUrl = JSON.parse(uploadFileRes.data).url
  183. addVoicePrintUrl({voicePrintUrl: voicePrintUrl,companyUserId:this.companyUserId}).then(res=>{
  184. uni.hideLoading()
  185. this.btnLoading = false
  186. if(res.code==200){
  187. console.log(200,this.companyUserId)
  188. uni.showToast({
  189. icon:'none',
  190. title: '提交成功',
  191. });
  192. }else{
  193. uni.showToast({
  194. icon:'none',
  195. title: res.msg,
  196. });
  197. }
  198. }).catch(()=>{
  199. uni.hideLoading()
  200. this.btnLoading = false
  201. })
  202. },
  203. fail: ()=>{
  204. uni.hideLoading()
  205. this.btnLoading = false
  206. }
  207. });
  208. }
  209. }
  210. }
  211. </script>
  212. <style scoped lang="scss">
  213. @mixin u-flex($flexD, $alignI, $justifyC) {
  214. display: flex;
  215. flex-direction: $flexD;
  216. align-items: $alignI;
  217. justify-content: $justifyC;
  218. }
  219. .container {
  220. position: relative;
  221. .header-tips {
  222. padding-bottom: 24rpx;
  223. box-sizing: border-box;
  224. font-family: PingFang SC, PingFang SC;
  225. font-weight: 400;
  226. font-size: 24rpx;
  227. color: #757575;
  228. // color: $mainThemeHColor;
  229. text-align: center;
  230. }
  231. .textbox {
  232. padding: 24rpx 50rpx;
  233. box-sizing: border-box;
  234. &-con {
  235. padding: 32rpx;
  236. box-sizing: border-box;
  237. background-color: #fff;
  238. border-radius: 16rpx 16rpx 16rpx 16rpx;
  239. font-family: PingFang SC, PingFang SC;
  240. font-weight: 500;
  241. font-size: 40rpx;
  242. color: #222222;
  243. overflow-y: auto;
  244. line-height: 60rpx;
  245. letter-spacing: 4rpx;
  246. }
  247. }
  248. .voice-footer {
  249. position: fixed;
  250. bottom: 0;
  251. left: 0;
  252. width: 100%;
  253. box-sizing: border-box;
  254. &-tips {
  255. padding: 0 50rpx;
  256. box-sizing: border-box;
  257. font-family: PingFang SC, PingFang SC;
  258. font-weight: 400;
  259. font-size: 24rpx;
  260. color: #757575;
  261. margin-bottom: 10rpx;
  262. }
  263. }
  264. }
  265. .voice-footer-btnbox {
  266. @include u-flex(row,flex-end,space-evenly);
  267. margin-top: 100rpx;
  268. padding: 50rpx 24rpx;
  269. background-color: #fff;
  270. text-align: center;
  271. font-family: PingFang SC, PingFang SC;
  272. font-weight: 400;
  273. font-size: 30rpx;
  274. color: #333;
  275. .iconsbox {
  276. height: 110rpx;
  277. width: 110rpx;
  278. margin: 0;
  279. margin-bottom: 20rpx;
  280. box-sizing: border-box;
  281. @include u-flex(row,center,center);
  282. border-radius: 50%;
  283. background-color: #fff;
  284. border: 1rpx solid #CCCCCC;
  285. &::after {
  286. border: none;
  287. }
  288. &-voice {
  289. height: 150rpx;
  290. width: 150rpx;
  291. background-color: #FF5C03;
  292. border: 1rpx solid #FF5C03;
  293. image {
  294. height: 60rpx;
  295. width: 60rpx;
  296. }
  297. }
  298. }
  299. .btnbox-item {
  300. flex: 1;
  301. @include u-flex(column,center,center);
  302. }
  303. }
  304. </style>