uniapp录音功能

录音功能需要使用到uniapp官方文档里面的API

模版区

javascript 复制代码
<template>
	<view>
		<button @tap="startRecord">开始录音</button>
		<button @tap="endRecord">停止录音</button>
		<button @tap="playVoice">播放录音</button>
	</view>
</template>

js

javascript 复制代码
const recorderManager = uni.getRecorderManager();
const innerAudioContext = uni.createInnerAudioContext();
 
innerAudioContext.autoplay = true;
 
export default {
	data() {
		return {
			text: 'uni-app',
			voicePath: ''
		}
	},
	onLoad() {
		let self = this;
		recorderManager.onStop(function (res) {
			console.log('recorder stop' + JSON.stringify(res));
			self.voicePath = res.tempFilePath;
		});
	},
	methods: {
		startRecord() {
			console.log('开始录音');
 
			recorderManager.start();
		},
		endRecord() {
			console.log('录音结束');
			recorderManager.stop();
		},
		playVoice() {
			console.log('播放录音');
 
			if (this.voicePath) {
				innerAudioContext.src = this.voicePath;
				innerAudioContext.play();
			}
		}
	}
}

现在就是可以进行一个简单的录音,暂停以及播放,再根据需求进行更改即可

还有需要注意的就是在进行真机调试的时候需要判断用户是否打开了录音权限

javascript 复制代码
uni.authorize({
					scope: 'scope.record',
					success() {
						recorderManager.start({
							format: 'mp3'
						});
						t.getTimeInterval();
						uni.setInnerAudioOption({
							obeyMuteSwitch: false
						})
					},
					fail() {
						uni.showModal({
							content: '检测到您没打开录音功能权限,是否去设置打开?',
							confirmText: "确认",
							cancelText: '取消',
							success(res) {
								recorderManager.start();
								t.time = setInterval(this.timer, 50)
							}
						})
					}
				})
相关推荐
当时只道寻常5 分钟前
Vue3 集成 NProgress 进度条:从入门到精通
前端·vue.js
代码中介商6 分钟前
Linux 静态库与共享库完全指南:从制作到使用
linux·运维·服务器
kyriewen6 分钟前
React性能优化:从“卡成狗”到“丝般顺滑”的5个秘诀
前端·react.js·性能优化
米丘6 分钟前
Vue 3.x 单文件组件(SFC)模板编译过程解析
前端·vue.js·编译原理
"小夜猫&小懒虫&小财迷"的男人6 分钟前
【Linux v7.0 以太网驱动+协议栈】000 - 文章链接汇总
linux·网络
helloweilei9 分钟前
Web Streams 简介
前端·javascript
悟空瞎说9 分钟前
Flutter热更新 Shorebird CodePush 原理、实现细节及费用说明
前端·flutter
didadida2629 分钟前
从“不存在”的重复请求,聊到 Web 存储的深坑
前端
铭keny9 分钟前
【Ubuntu部署】人脸特征提取SDK完整部署教程(含Nginx代理+问题排查)
linux·nginx·ubuntu