• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > uni-app小程序录音上传的解决方案

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

作者:站长图库 字体:[增加 减小] 来源:互联网 时间:2022-04-29

站长图库向大家介绍了uni-app,小程序,录音上传,解决方案等相关知识,希望对您有所帮助

能力依赖

RecorderManager 全局唯一的录音管理器


录音功能的要求与限制

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

是否在录音中状态显示

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

材料

可以/结束 录音 录音中


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

构造一个简单的DOM结构

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

先实现小程序的录音功能

import iconRecord from '../../static/images/icon_record.png'import iconRecording from '../../static/images/icon_recording.png'// ...data() {    recordImg: iconRecord, // 录音按钮的图标    rm: null, // 录音管理器},// ...mounted() {    if (this.rm === null) {        // 录音管理器如果没有初始化就先初始化        this.rm = uni.getRecorderManager()    }    // 绑定回调方法    this.rm.onStart((e) => this.onStart(e))    this.rm.onPause((e) => this.onPause(e))    this.rm.onResume((e) => this.onResume(e))    this.rm.onInterruptionBegin((e) => this.onInterruptionBegin(e))    this.rm.onInterruptionEnd((e) => this.onInterruptionEnd(e))    this.rm.onError((e) => this.onError(e))},// ...methods: {    // ...    recordAction() {        if (this.recordImg === iconRecord) {            // 设置格式为MP3,最长60S,采样率22050            this.rm.start({                duration: 600000,                format: 'mp3',                sampleRate: 22050,            })            // 开始录音后绑定停止录音的回调方法            this.rm.onStop((e) => this.onStop(e))        } else if (this.recordImg === iconRecording) {            this.rm.stop()        },    },    onStart(e) {        console.log('开始录音', this.question, this.subQuesIndex)        this.recordImg = iconRecording        console.log(e)    },    onPause(e) {        console.log(e)        afterAudioRecord()    },    onResume(e) {        console.log(e)    },    onStop(e) {        console.log(e)        this.recordImg = iconRecord        // 结束录音之后上传录音        this.uploadMp3Action(e)    },    onInterruptionBegin(e) {      console.log(e)    },    onInterruptionEnd(e) {        console.log(e)    },    onError(e) {        console.log(e)    },    uploadMp3Action(e) {        // TODO uploadMp3    },},

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

globalData中增加两个属性audioPlaying,audioRecording

// src/App.vueexport default {    globalData: {          // 保证全局只有一个音频处于播放状态且录音与播放操作互斥        audioPlaying: false,        audioRecording: false,    },    // ...},

Util中增加判断方法

// src/lib/Util.js// 结束录音之后释放录音能力export function afterAudioRecord() {    getApp().globalData.audioRecording = false}// 结束音频播放之后释放音频播放能力export function afterAudioPlay() {    getApp().globalData.audioPlaying = false}/** * 判断是否可以录音或者播放 * @param {string} type play | record */export function beforeAudioRecordOrPlay(type) {    const audioPlaying = getApp().globalData.audioPlaying    const audioRecording = getApp().globalData.audioRecording    if (audioPlaying ||audioRecording) {        uni.showToast({            title: audioPlaying ? '请先暂停其他音频播放' : '请先结束其他录音',            icon: 'none'        })        return false    } else {        if (type === 'play') {            getApp().globalData.audioPlaying = true        } else if (type === 'record') {            getApp().globalData.audioRecording = true        } else {            throw new Error('type Error', type)        }        return true    }}

改造原有recordAction方法

import { beforeAudioRecordOrPlay, afterAudioRecord} from '../../lib/Utils';// ...recordAction() {-  if (this.recordImg === iconRecord) {+  if (this.recordImg === iconRecord && beforeAudioRecordOrPlay('record')) {        // 设置格式为MP3,最长60S,采样率22050        this.rm.start({            duration: 600000,            format: 'mp3',            sampleRate: 22050,        })        // 开始录音后绑定停止录音的回调方法        this.rm.onStop((e) => this.onStop(e))    } else if (this.recordImg === iconRecording) {        this.rm.stop()+       afterAudioRecord()    },},

这样就避免了多次录音


小程序录音上传

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

uploadMp3Action(e) {    const filePath = e.tempFilePath    const option = {        url: 'xxx',        filePath,        header,        formData: {            filePath        },        name: 'audio',    }    uni.showLoading({        title: '录音上传中...'    })    return await uni.uploadFile(option)    uni.hideloading()}

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

beforeDestroy() {    this.rm = null}

打完收工~



分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 如何快速搭建uni-app项目?两种搭建方法分享
  • 移动uni-app项目怎么实现发送位置的地图交互
  • uni-app中怎么开发一个全局弹层组件(代码示例)
  • 遇到的uni-app的坑(uni-easyinput清空值,datetimerange置空)
  • uni-app介绍全局样式引入和底部导航栏开发
  • 解决uni-app入坑集合的一种方案
  • 聊聊怎么将小程序项目转为uni-app项目
  • 浅析uni-app中怎么提交form表单?(代码解析)
  • 一起分析uni-app怎么实现上传图片
  • 看看使用uni-app如何编写一个五子棋小游戏

相关文章

  • 2022-04-29node.js 基于 STMP 协议和 EWS 协议发送邮件
  • 2022-04-29HTTP 返回状态码汇总
  • 2022-04-29详解thinkphp5.1/5.0定时任务的实现步骤
  • 2022-04-29帝国cms中常用标签(总结)
  • 2022-04-29TP6+vue-element-admin实现后台登录验证码
  • 2022-04-29CorelDraw制作可爱的彩色塑料字教程
  • 2022-04-29小程序如何获取input标签的值
  • 2022-04-29Illustrator设计个性时尚风格的名片教程
  • 2022-04-29如何将label标签与input标签进行绑定
  • 2022-04-29详解php中函数的引用传递和返回 (附代码)

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • Photoshop快速制作漂亮的花朵浮雕字
    • 浅谈bootstrapTable如何动态设置行和列的颜色
    • 趣味讲解Node.js中的回调函数(附示例)
    • 解决PHPstudy V8.0打开phpMyAdmin显示错误问题
    • 完美实现CSS垂直居中的11种方法
    • 纯CSS实现心形加载动画(附源码)
    • Workerman中你不得不知道的属性reusePort
    • 五步搞定Laravel Migrations的使用
    • Photoshop制作高光梦幻效果的艺术字教程
    • Photoshop手工制作精美的格子背景教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有