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)
							}
						})
					}
				})
相关推荐
xixixin_1 小时前
【Vite】前端开发服务器的配置
服务器·前端·网络
.生产的驴1 小时前
Vue3 加快页面加载速度 使用CDN外部库的加载 提升页面打开速度 服务器分发
运维·服务器·前端·vue.js·分布式·前端框架·vue
程序员JerrySUN1 小时前
Linux 内核核心知识热点题分析:10 个连环打通的难点
linux·运维·服务器
史迪仔01121 小时前
Python生成器:高效处理大数据的秘密武器
前端·数据库·python
蓝婷儿2 小时前
前端面试每日三题 - Day 34
前端·面试·职场和发展
CopyLower2 小时前
苹果计划将AI搜索集成至Safari:谷歌搜索下降引发的市场变革
前端·人工智能·safari
R_.L2 小时前
Linux : 线程【同步与互斥】
linux
再睡一夏就好2 小时前
从硬件角度理解“Linux下一切皆文件“,详解用户级缓冲区
linux·服务器·c语言·开发语言·学习笔记
我是Superman丶4 小时前
【技巧】前端VUE用中文方法名调用没效果的问题
前端·javascript·vue.js
斯~内克4 小时前
Vue 3 中 watch 的使用与深入理解
前端·javascript·vue.js