voice.vue 8.7 KB

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