voice.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/manageCompany'
  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. }
  58. },
  59. onLoad() {
  60. console.log('==onLoad==',this.recorderManager)
  61. // #ifndef H5
  62. this.recorderManager = uni.getRecorderManager();
  63. innerAudioContext = uni.createInnerAudioContext();
  64. let self = this;
  65. console.log('==recorderManager22222222==',this.recorderManager)
  66. if(this.recorderManager) {
  67. console.log('==recorderManager==')
  68. this.recorderManager.onStart(()=>{
  69. console.log('==start==')
  70. this.status = 'start'
  71. })
  72. this.recorderManager.onStop((res)=> {
  73. console.log('recorder stop' + JSON.stringify(res));
  74. this.status = 'end'
  75. self.voicePath = res.tempFilePath;
  76. });
  77. this.recorderManager.onError((res)=> {
  78. console.log('onError ==' + JSON.stringify(res));
  79. });
  80. }
  81. if(innerAudioContext) {
  82. // innerAudioContext.autoplay = true;
  83. innerAudioContext.onPlay(() => {
  84. // console.log('开始播放');
  85. this.isVoicePlay = true
  86. });
  87. innerAudioContext.onStop(() => {
  88. // console.log('停止播放');
  89. this.isVoicePlay = false
  90. });
  91. innerAudioContext.onEnded(() => {
  92. // console.log('播放结束');
  93. this.isVoicePlay = false
  94. });
  95. innerAudioContext.onError((res) => {
  96. // console.log('播放res',res);
  97. this.isVoicePlay = false
  98. });
  99. innerAudioContext.onPause(() => {
  100. // console.log('暂停播放');
  101. this.isVoicePlay = false
  102. });
  103. }
  104. // #endif
  105. },
  106. onReady() {
  107. const query = uni.createSelectorQuery().in(this);
  108. query
  109. .select(".voice-footer")
  110. .boundingClientRect((data) => {
  111. this.textHeight = `calc(${this.screenHeight} - ${data.height}px - ${this.statusBarHeight} - 45px - ${uni.upx2px(120)}px)`
  112. })
  113. .exec();
  114. },
  115. onHide() {
  116. if(innerAudioContext){
  117. innerAudioContext.pause();
  118. }
  119. },
  120. onUnload() {
  121. this.recorderManager = null
  122. if(innerAudioContext) {
  123. innerAudioContext.destroy()
  124. innerAudioContext = null
  125. }
  126. },
  127. methods: {
  128. handleRecord() {
  129. console.log('handleRecord',this.status)
  130. if(innerAudioContext){
  131. innerAudioContext.pause();
  132. }
  133. if(this.status == 'stop') {
  134. this.startRecord()
  135. } else if(this.status == 'start') {
  136. this.endRecord()
  137. } else if(this.status == 'end') {
  138. this.startRecord()
  139. }
  140. },
  141. startRecord() {
  142. console.log('this.recorderManager',this.recorderManager)
  143. // console.log('开始录音');
  144. // 声音采集限制10秒
  145. this.recorderManager.start({
  146. format: 'mp3',
  147. duration: 10000
  148. });
  149. },
  150. endRecord() {
  151. // console.log('录音结束');
  152. this.recorderManager.stop();
  153. },
  154. playVoice() {
  155. console.log(innerAudioContext)
  156. if(this.status == "start") return
  157. if (this.voicePath) {
  158. if(this.isVoicePlay == false) {
  159. innerAudioContext.src = this.voicePath;
  160. innerAudioContext.play();
  161. } else {
  162. innerAudioContext.stop();
  163. }
  164. }
  165. },
  166. onSubmit() {
  167. console.log("==onSubmit=",this.status)
  168. if(this.status == "start") return
  169. this.btnLoading = true
  170. uni.showLoading({
  171. title:"提交中..."
  172. })
  173. uni.uploadFile({
  174. url: uni.getStorageSync('requestPath')+'/app/common/uploadOSS', //仅为示例,非真实的接口地址
  175. filePath: this.voicePath,
  176. name: 'file',
  177. success: (uploadFileRes) => {
  178. console.log(JSON.parse(uploadFileRes.data).url)
  179. let voicePrintUrl = JSON.parse(uploadFileRes.data).url
  180. addVoicePrintUrl({voicePrintUrl: voicePrintUrl}).then(res=>{
  181. uni.hideLoading()
  182. this.btnLoading = false
  183. if(res.code==200){
  184. uni.showToast({
  185. icon:'none',
  186. title: '提交成功',
  187. });
  188. }else{
  189. uni.showToast({
  190. icon:'none',
  191. title: res.msg,
  192. });
  193. }
  194. }).catch(()=>{
  195. uni.hideLoading()
  196. this.btnLoading = false
  197. })
  198. },
  199. fail: ()=>{
  200. uni.hideLoading()
  201. this.btnLoading = false
  202. }
  203. });
  204. }
  205. }
  206. }
  207. </script>
  208. <style scoped lang="scss">
  209. @mixin u-flex($flexD, $alignI, $justifyC) {
  210. display: flex;
  211. flex-direction: $flexD;
  212. align-items: $alignI;
  213. justify-content: $justifyC;
  214. }
  215. .container {
  216. position: relative;
  217. .header-tips {
  218. padding-bottom: 24rpx;
  219. box-sizing: border-box;
  220. font-family: PingFang SC, PingFang SC;
  221. font-weight: 400;
  222. font-size: 24rpx;
  223. color: #757575;
  224. // color: $mainThemeHColor;
  225. text-align: center;
  226. }
  227. .textbox {
  228. padding: 24rpx 50rpx;
  229. box-sizing: border-box;
  230. &-con {
  231. padding: 32rpx;
  232. box-sizing: border-box;
  233. background-color: #fff;
  234. border-radius: 16rpx 16rpx 16rpx 16rpx;
  235. font-family: PingFang SC, PingFang SC;
  236. font-weight: 500;
  237. font-size: 40rpx;
  238. color: #222222;
  239. overflow-y: auto;
  240. line-height: 60rpx;
  241. letter-spacing: 4rpx;
  242. }
  243. }
  244. .voice-footer {
  245. position: fixed;
  246. bottom: 0;
  247. left: 0;
  248. width: 100%;
  249. box-sizing: border-box;
  250. &-tips {
  251. padding: 0 50rpx;
  252. box-sizing: border-box;
  253. font-family: PingFang SC, PingFang SC;
  254. font-weight: 400;
  255. font-size: 24rpx;
  256. color: #757575;
  257. margin-bottom: 10rpx;
  258. }
  259. }
  260. }
  261. .voice-footer-btnbox {
  262. @include u-flex(row,flex-end,space-evenly);
  263. margin-top: 100rpx;
  264. padding: 50rpx 24rpx;
  265. background-color: #fff;
  266. text-align: center;
  267. font-family: PingFang SC, PingFang SC;
  268. font-weight: 400;
  269. font-size: 30rpx;
  270. color: #333;
  271. .iconsbox {
  272. height: 110rpx;
  273. width: 110rpx;
  274. margin: 0;
  275. margin-bottom: 20rpx;
  276. box-sizing: border-box;
  277. @include u-flex(row,center,center);
  278. border-radius: 50%;
  279. background-color: #fff;
  280. border: 1rpx solid #CCCCCC;
  281. &::after {
  282. border: none;
  283. }
  284. &-voice {
  285. height: 150rpx;
  286. width: 150rpx;
  287. background-color: #FF5C03;
  288. border: 1rpx solid #FF5C03;
  289. image {
  290. height: 60rpx;
  291. width: 60rpx;
  292. }
  293. }
  294. }
  295. .btnbox-item {
  296. flex: 1;
  297. @include u-flex(column,center,center);
  298. }
  299. }
  300. </style>