voiceItem.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <template>
  2. <view class="container">
  3. <view class="textbox">
  4. <view class="header-tips">请朗读以下文字</view>
  5. <view class="textbox-con" :style="{height: textHeight}">{{voiceText || ''}}</view>
  6. </view>
  7. <view class="voice-footer">
  8. <view class="voice-footer-tips">1、选择安静的录音环境,可在房间或车内录音。</view>
  9. <view class="voice-footer-tips">2、保持20cm距离,避免手机太远录音不清晰。</view>
  10. <view class="voice-footer-tips">3、使用普通话朗读,语速适中,吐字清晰。</view>
  11. <view class="voice-footer-btnbox">
  12. <view class="tabs" v-if="type!=2">
  13. <u-tabs
  14. :scrollable="false"
  15. :list="tabs"
  16. :current="current"
  17. lineColor="#FF5C03"
  18. @change="tabChange">
  19. </u-tabs>
  20. <view class="mask" v-if="status=='start'||status=='end'" @click.stop>
  21. <view @click.stop="tabclick(0)"></view>
  22. <view @click.stop="tabclick(1)"></view>
  23. </view>
  24. </view>
  25. <view class="voice-footer-con">
  26. <view class="btnbox-item" :style="{visibility: voicePath ? 'visible' : 'hidden',color: status=='start' ? '#ccc !important':''}">
  27. <view class="iconsbox" @click="playVoice" :style="{borderColor: status=='start' ? '#ccc' :isVoicePlay ? 'red':''}">
  28. <u-icon name="volume-fill" size="30" :color="status=='start' ? '#ccc' :isVoicePlay ? 'red':'#757575'"></u-icon>
  29. <!-- <uni-icons type="sound-filled" size="30" :color="status=='start' ? '#ccc' :isVoicePlay ? 'red':'#757575'"></uni-icons> -->
  30. </view>
  31. <view>试听录音</view>
  32. </view>
  33. <view class="btnbox-item">
  34. <view class="iconsbox iconsbox-voice" :style="{backgroundColor: status=='end'|| status=='ok' ? 'red':''}" @click="handleRecord">
  35. <view v-show="status=='stop'|| status=='end' || status=='ok'">
  36. <view v-show="current==1" class="smallcircle-filled"></view>
  37. <u-icon v-show="current!=1" name="mic" size="35" color="#fff"></u-icon>
  38. </view>
  39. <!-- <uni-icons v-show="status=='stop'|| status=='end' || status=='ok'" :type="current==1?'smallcircle-filled':'mic-filled'" size="35" color="#fff"></uni-icons> -->
  40. <image v-show="current!=1&&status=='start'" src="https://obs.jnmyunl.com/fs/20250813/1755047925090.png" mode="aspectFill"></image>
  41. <view v-show="current==1&&status=='start'"><u-loading-icon color="#fff"></u-loading-icon></view>
  42. </view>
  43. <view v-show="status=='stop'">{{current==1?'点击生成':'点击录制'}}</view>
  44. <view v-show="status=='start'">{{current==1?'生成中':'点击停止'}}</view>
  45. <view v-show="status=='end'|| status=='ok'">{{current==1?'重新生成':'重新录制'}}</view>
  46. </view>
  47. <view class="btnbox-item" :style="{visibility: voicePath&&status!='ok'&&current!=1 ? 'visible' : 'hidden',color: status=='start' ? '#ccc !important':''}">
  48. <button class="iconsbox" :disabled="btnLoading" @click="onSubmit">
  49. <u-icon name="checkmark" size="30" :color="status=='start' ? '#ccc' : '#757575'"></u-icon>
  50. <!-- <uni-icons type="checkmarkempty" size="30" :color="status=='start' ? '#ccc' : '#757575'"></uni-icons> -->
  51. </button>
  52. <view>提交</view>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. import {companyUserVoiceNew,companyUserVoice,queryDetail} from '@/api/manageCompany'
  61. let innerAudioContext = null
  62. export default {
  63. data() {
  64. return {
  65. recorderManager: null,
  66. // innerAudioContext: null,
  67. textHeight: '',
  68. statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
  69. screenHeight: uni.getSystemInfoSync().windowHeight + 'px',
  70. voicePath: '',
  71. status: "stop",
  72. isVoicePlay: false,
  73. btnLoading: false,
  74. tabs:[
  75. {
  76. id:1,
  77. name:'自己录制'
  78. },
  79. {
  80. id:2,
  81. name:'AI生成'
  82. }
  83. ],
  84. current: 0,
  85. id: null,
  86. voiceText: '',
  87. recordType: 0, // 是否已采集
  88. type: 0, // 2表示只能自己录制
  89. }
  90. },
  91. onLoad(option) {
  92. this.type = option.type || 0
  93. this.recordType = option.recordType || 0
  94. this.id = option.id || null
  95. this.getDetail()
  96. this.initDate()
  97. },
  98. onHide() {
  99. if(innerAudioContext){
  100. innerAudioContext.pause();
  101. }
  102. },
  103. onUnload() {
  104. this.recorderManager = null
  105. if(innerAudioContext) {
  106. innerAudioContext.destroy()
  107. innerAudioContext = null
  108. }
  109. },
  110. methods: {
  111. tabclick(type) {
  112. if(type == this.current) return
  113. if(this.btnLoading || this.status == 'start') {
  114. uni.showToast({
  115. title: '生成中,请勿进行其他操作!',
  116. icon: 'none'
  117. })
  118. } else if(this.status == 'end') {
  119. const that = this
  120. uni.showModal({
  121. title: '提示',
  122. content: '当亲录制已完成,切换类型需要重新录制,确认切换吗?',
  123. success: function (res) {
  124. if (res.confirm) {
  125. const item = {
  126. index: type,
  127. ...that.tabs[type]
  128. }
  129. that.tabChange(item)
  130. } else if (res.cancel) {
  131. console.log('用户点击取消');
  132. }
  133. }
  134. });
  135. }
  136. },
  137. tabChange(item){
  138. console.log("tabChange")
  139. if(this.current == item.index) return
  140. if(this.btnLoading || this.status == 'start') {
  141. uni.showToast({
  142. title: '生成中,请勿进行其他操作!',
  143. icon: 'none'
  144. })
  145. }
  146. this.current = item.index
  147. this.recorderManager = null
  148. if(innerAudioContext) {
  149. innerAudioContext.stop();
  150. innerAudioContext.destroy()
  151. innerAudioContext = null
  152. }
  153. this.voicePath = this.recordType == 1 ? this.voicePath : ''
  154. this.status = this.recordType == 1 ? this.status : "stop"
  155. this.isVoicePlay = false
  156. this.btnLoading = false
  157. this.initDate()
  158. },
  159. initDate() {
  160. // #ifndef H5
  161. this.recorderManager = uni.getRecorderManager();
  162. innerAudioContext = uni.createInnerAudioContext();
  163. let self = this;
  164. if(this.recorderManager) {
  165. this.recorderManager.onStart(()=>{
  166. this.status = 'start'
  167. this.voicePath = ''
  168. })
  169. this.recorderManager.onStop((res)=> {
  170. // console.log('recorder stop' + JSON.stringify(res));
  171. this.status = 'end'
  172. self.voicePath = res.tempFilePath;
  173. });
  174. }
  175. if(innerAudioContext) {
  176. // innerAudioContext.autoplay = true;
  177. innerAudioContext.onPlay(() => {
  178. // console.log('开始播放');
  179. this.isVoicePlay = true
  180. });
  181. innerAudioContext.onStop(() => {
  182. // console.log('停止播放');
  183. this.isVoicePlay = false
  184. });
  185. innerAudioContext.onEnded(() => {
  186. // console.log('播放结束');
  187. this.isVoicePlay = false
  188. });
  189. innerAudioContext.onError((res) => {
  190. this.isVoicePlay = false
  191. });
  192. innerAudioContext.onPause(() => {
  193. // console.log('暂停播放');
  194. this.isVoicePlay = false
  195. });
  196. }
  197. // #endif
  198. },
  199. handleRecord() {
  200. if(innerAudioContext){
  201. innerAudioContext.pause();
  202. }
  203. if(this.current == 1) {
  204. if(this.status == 'stop') {
  205. this.creatVoice()
  206. } else if(this.status == 'start') {
  207. uni.showToast({
  208. title: '生成中,请勿进行其他操作!',
  209. icon: 'none'
  210. })
  211. } else if(this.status == 'end'||this.status == 'ok') {
  212. this.creatVoice()
  213. }
  214. } else {
  215. if(this.status == 'stop') {
  216. this.startRecord()
  217. } else if(this.status == 'start') {
  218. this.endRecord()
  219. } else if(this.status == 'end'||this.status == 'ok') {
  220. this.startRecord()
  221. }
  222. }
  223. },
  224. startRecord() {
  225. // console.log('开始录音');
  226. this.recorderManager.start({
  227. format: 'mp3',
  228. });
  229. },
  230. endRecord() {
  231. // console.log('录音结束');
  232. this.recorderManager.stop();
  233. },
  234. playVoice() {
  235. if(this.status == "start") return
  236. if (this.voicePath) {
  237. if(this.isVoicePlay == false) {
  238. innerAudioContext.src = this.voicePath;
  239. innerAudioContext.play();
  240. } else {
  241. innerAudioContext.stop();
  242. }
  243. }
  244. },
  245. onSubmit() {
  246. if(this.status == "start") return
  247. this.btnLoading = true
  248. uni.showLoading({
  249. title:"提交中..."
  250. })
  251. if(this.current == 1) {
  252. this.creatVoice()
  253. return
  254. }
  255. uni.uploadFile({
  256. url: uni.getStorageSync('requestPath')+'/app/common/uploadOSS', //仅为示例,非真实的接口地址
  257. filePath: this.voicePath,
  258. name: 'file',
  259. success: (uploadFileRes) => {
  260. console.log("companyUserVoiceNew==",JSON.parse(uploadFileRes.data).url)
  261. let voicePrintUrl = JSON.parse(uploadFileRes.data).url
  262. companyUserVoiceNew({userVoiceUrl: voicePrintUrl,id:this.id}).then(res=>{
  263. uni.hideLoading()
  264. this.btnLoading = false
  265. if(res.code==200){
  266. uni.showToast({
  267. icon:'none',
  268. title: '提交成功',
  269. });
  270. uni.$emit('refreshVoiceList')
  271. setTimeout(()=>{
  272. uni.navigateBack()
  273. },2000)
  274. }else{
  275. uni.showToast({
  276. icon:'none',
  277. title: res.msg,
  278. duration: 2000
  279. });
  280. }
  281. }).catch(()=>{
  282. uni.hideLoading()
  283. this.btnLoading = false
  284. })
  285. },
  286. fail: ()=>{
  287. uni.hideLoading()
  288. this.btnLoading = false
  289. }
  290. });
  291. },
  292. getDetail() {
  293. queryDetail(this.id).then(res=>{
  294. if(res.code==200){
  295. this.voiceText = res.data.voiceTxt
  296. this.recordType = res.data.recordType
  297. if(this.recordType == 1) {
  298. this.voicePath = res.data.wavUrl||''
  299. this.status = 'ok'
  300. if(this.voicePath&&this.voicePath.match(/\.mp3$/)) {
  301. this.current = 0
  302. } else {
  303. this.current = 1
  304. }
  305. }
  306. this.$nextTick(()=>{
  307. const query = uni.createSelectorQuery().in(this);
  308. query
  309. .select(".voice-footer")
  310. .boundingClientRect((data) => {
  311. this.textHeight = `calc(${this.screenHeight} - ${data.height}px - ${this.statusBarHeight} - 45px - ${uni.upx2px(120)}px)`
  312. })
  313. .exec();
  314. })
  315. }else{
  316. uni.showToast({
  317. icon:'none',
  318. title: res.msg,
  319. });
  320. }
  321. })
  322. },
  323. // 智能生成
  324. creatVoice() {
  325. if(this.status == "start") return
  326. this.btnLoading = true
  327. this.status = "start"
  328. this.voicePath = ''
  329. uni.showLoading({
  330. title:"生成中..."
  331. })
  332. companyUserVoice({
  333. id: this.id,
  334. }).then(res=>{
  335. uni.hideLoading()
  336. this.btnLoading = false
  337. if(res.code==200){
  338. this.status = "end"
  339. this.voicePath = res.data.wavUrl
  340. uni.showToast({
  341. icon:'none',
  342. title: '生成成功',
  343. });
  344. uni.$emit('refreshVoiceList')
  345. setTimeout(()=>{
  346. uni.navigateBack()
  347. },2000)
  348. }else{
  349. this.status = "stop"
  350. uni.showToast({
  351. icon:'none',
  352. title: res.msg,
  353. duration: 2000
  354. });
  355. }
  356. }).catch(()=>{
  357. uni.hideLoading()
  358. this.btnLoading = false
  359. this.status = "stop"
  360. })
  361. }
  362. }
  363. }
  364. </script>
  365. <style scoped lang="scss">
  366. @mixin u-flex($flexD, $alignI, $justifyC) {
  367. display: flex;
  368. flex-direction: $flexD;
  369. align-items: $alignI;
  370. justify-content: $justifyC;
  371. }
  372. .smallcircle-filled {
  373. background-color: #fff;
  374. width: 70rpx;
  375. height: 70rpx;
  376. border-radius: 50%;
  377. }
  378. .tabs {
  379. position: relative;
  380. width: 100%;
  381. .mask {
  382. position: absolute;
  383. top: 0;
  384. left: 0;
  385. width: 100%;
  386. height: 100%;
  387. z-index: 3;
  388. @include u-flex(row,flex-start,flex-start);
  389. view {
  390. flex: 1;
  391. height: 100%;
  392. }
  393. }
  394. }
  395. .container {
  396. position: relative;
  397. .header-tips {
  398. padding-bottom: 24rpx;
  399. box-sizing: border-box;
  400. font-family: PingFang SC, PingFang SC;
  401. font-weight: 400;
  402. font-size: 24rpx;
  403. color: #757575;
  404. // color: $mainThemeHColor;
  405. text-align: center;
  406. }
  407. .textbox {
  408. padding: 24rpx 50rpx;
  409. box-sizing: border-box;
  410. &-con {
  411. padding: 32rpx;
  412. box-sizing: border-box;
  413. background-color: #fff;
  414. border-radius: 16rpx 16rpx 16rpx 16rpx;
  415. font-family: PingFang SC, PingFang SC;
  416. font-weight: 500;
  417. font-size: 40rpx;
  418. color: #222222;
  419. overflow-y: auto;
  420. line-height: 60rpx;
  421. letter-spacing: 4rpx;
  422. }
  423. }
  424. .voice-footer {
  425. position: fixed;
  426. bottom: 0;
  427. left: 0;
  428. width: 100%;
  429. box-sizing: border-box;
  430. &-tips {
  431. padding: 0 50rpx;
  432. box-sizing: border-box;
  433. font-family: PingFang SC, PingFang SC;
  434. font-weight: 400;
  435. font-size: 24rpx;
  436. color: #757575;
  437. margin-bottom: 10rpx;
  438. }
  439. }
  440. }
  441. .voice-footer-btnbox {
  442. margin-top: 100rpx;
  443. background-color: #fff;
  444. text-align: center;
  445. font-family: PingFang SC, PingFang SC;
  446. font-weight: 400;
  447. font-size: 30rpx;
  448. color: #333;
  449. .voice-footer-con {
  450. padding: 50rpx 24rpx;
  451. box-sizing: border-box;
  452. @include u-flex(row,flex-end,space-evenly);
  453. }
  454. .iconsbox {
  455. height: 110rpx;
  456. width: 110rpx;
  457. margin: 0;
  458. margin-bottom: 20rpx;
  459. box-sizing: border-box;
  460. @include u-flex(row,center,center);
  461. border-radius: 50%;
  462. background-color: #fff;
  463. border: 1rpx solid #CCCCCC;
  464. &::after {
  465. border: none;
  466. }
  467. &-voice {
  468. height: 150rpx;
  469. width: 150rpx;
  470. background-color: #FF5C03;
  471. border: 1rpx solid #FF5C03;
  472. image {
  473. height: 60rpx;
  474. width: 60rpx;
  475. }
  476. }
  477. }
  478. .btnbox-item {
  479. flex: 1;
  480. @include u-flex(column,center,center);
  481. }
  482. }
  483. </style>