uni-app小程序录音上传的解决方案

 6424

能力依赖

RecorderManager 全局唯一的录音管理器


录音功能的要求与限制

与当前页面其他音频播放/录音功能互斥

是否在录音中状态显示

结束/不需要录音时,回收RecorderManager对象

材料

可以/结束 录音 录音中


Codeing(结果代码直接看最后)

构造一个简单的DOM结构

  1. <image @click="recordAction" :src="recordImg" class="record"/>

先实现小程序的录音功能

  1. import iconRecord from '../../static/images/icon_record.png'
  2. import iconRecording from '../../static/images/icon_recording.png'
  3. // ...
  4. data() {
  5.     recordImg: iconRecord, // 录音按钮的图标
  6.     rm: null, // 录音管理器
  7. },
  8. // ...
  9. mounted() {
  10.     if (this.rm === null) {
  11.         // 录音管理器如果没有初始化就先初始化
  12.         this.rm = uni.getRecorderManager()
  13.     }
  14.     // 绑定回调方法
  15.     this.rm.onStart((e) => this.onStart(e))
  16.     this.rm.onPause((e) => this.onPause(e))
  17.     this.rm.onResume((e) => this.onResume(e))
  18.     this.rm.onInterruptionBegin((e) => this.onInterruptionBegin(e))
  19.     this.rm.onInterruptionEnd((e) => this.onInterruptionEnd(e))
  20.     this.rm.onError((e) => this.onError(e))
  21. },
  22. // ...
  23. methods: {
  24.     // ...
  25.     recordAction() {
  26.         if (this.recordImg === iconRecord) {
  27.             // 设置格式为MP3,最长60S,采样率22050
  28.             this.rm.start({
  29.                 duration: 600000,
  30.                 format: 'mp3',
  31.                 sampleRate: 22050,
  32.             })
  33.             // 开始录音后绑定停止录音的回调方法
  34.             this.rm.onStop((e) => this.onStop(e))
  35.         } else if (this.recordImg === iconRecording) {
  36.             this.rm.stop()
  37.         },
  38.     },
  39.     onStart(e) {
  40.         console.log('开始录音', this.question, this.subQuesIndex)
  41.         this.recordImg = iconRecording
  42.         console.log(e)
  43.     },
  44.     onPause(e) {
  45.         console.log(e)
  46.         afterAudioRecord()
  47.     },
  48.     onResume(e) {
  49.         console.log(e)
  50.     },
  51.     onStop(e) {
  52.         console.log(e)
  53.         this.recordImg = iconRecord
  54.         // 结束录音之后上传录音
  55.         this.uploadMp3Action(e)
  56.     },
  57.     onInterruptionBegin(e) {
  58.       console.log(e)
  59.     },
  60.     onInterruptionEnd(e) {
  61.         console.log(e)
  62.     },
  63.     onError(e) {
  64.         console.log(e)
  65.     },
  66.     uploadMp3Action(e) {
  67.         // TODO uploadMp3
  68.     },
  69. },

只能同时有一个录音,与音频播放互斥

globalData中增加两个属性audioPlaying,audioRecording

  1. // src/App.vue
  2. export default {
  3.     globalData: {  
  4.         // 保证全局只有一个音频处于播放状态且录音与播放操作互斥
  5.         audioPlaying: false,
  6.         audioRecording: false,
  7.     },
  8.     // ...
  9. },

Util中增加判断方法

  1. // src/lib/Util.js
  2. // 结束录音之后释放录音能力
  3. export function afterAudioRecord() {
  4.     getApp().globalData.audioRecording = false
  5. }
  6. // 结束音频播放之后释放音频播放能力
  7. export function afterAudioPlay() {
  8.     getApp().globalData.audioPlaying = false
  9. }
  10. /**
  11.  * 判断是否可以录音或者播放
  12.  * @param {string} type play | record
  13.  */
  14. export function beforeAudioRecordOrPlay(type) {
  15.     const audioPlaying = getApp().globalData.audioPlaying
  16.     const audioRecording = getApp().globalData.audioRecording
  17.     if (audioPlaying ||audioRecording) {
  18.         uni.showToast({
  19.             title: audioPlaying ? '请先暂停其他音频播放' : '请先结束其他录音',
  20.             icon: 'none'
  21.         })
  22.         return false
  23.     } else {
  24.         if (type === 'play') {
  25.             getApp().globalData.audioPlaying = true
  26.         } else if (type === 'record') {
  27.             getApp().globalData.audioRecording = true
  28.         } else {
  29.             throw new Error('type Error', type)
  30.         }
  31.         return true
  32.     }
  33. }

改造原有recordAction方法

  1. import { beforeAudioRecordOrPlay, afterAudioRecord} from '../../lib/Utils';
  2. // ...
  3. recordAction() {
  4. -  if (this.recordImg === iconRecord) {
  5. +  if (this.recordImg === iconRecord && beforeAudioRecordOrPlay('record')) {
  6.         // 设置格式为MP3,最长60S,采样率22050
  7.         this.rm.start({
  8.             duration: 600000,
  9.             format: 'mp3',
  10.             sampleRate: 22050,
  11.         })
  12.         // 开始录音后绑定停止录音的回调方法
  13.         this.rm.onStop((e) => this.onStop(e))
  14.     } else if (this.recordImg === iconRecording) {
  15.         this.rm.stop()
  16. +       afterAudioRecord()
  17.     },
  18. },

这样就避免了多次录音


小程序录音上传

补全我们的uploadMp3Action方法,我们使用uni-app的uni.uploadFile()方法来上传录音文件

  1. uploadMp3Action(e) {
  2.     const filePath = e.tempFilePath
  3.     const option = {
  4.         url: 'xxx',
  5.         filePath,
  6.         header,
  7.         formData: {
  8.             filePath
  9.         },
  10.         name: 'audio',
  11.     }
  12.     uni.showLoading({
  13.         title: '录音上传中...'
  14.     })
  15.     return await uni.uploadFile(option)
  16.     uni.hideloading()
  17. }

最后在页面卸载的时候回收RecorderManager对象

  1. beforeDestroy() {
  2.     this.rm = null
  3. }

打完收工~



本文网址:https://www.zztuku.com/detail-7936.html
站长图库 - uni-app小程序录音上传的解决方案
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐