当前位置: 首页 > article >正文

使用uniapp制作录音功能(VUE3)

       制作录音功能需要用到uniapp中API uni.getRecorderManager()  获取全局唯一的录音管理器recorderManager,然后就能够开始进行录音,播放。具体方法可以去uniapp官网上查看。

代码实现:

1、在html中添加按钮、点击弹出弹窗:

				<view class=""
					style="width: 50%;display: flex;justify-content: center;align-items: center;font-size: 20px;height:60px;"
					@click="showaPopup('bottom')">
					添加录音
				</view>		
<!-- 录音弹窗弹窗 -->
		<wd-popup ref="audioPopup" background-color="#fff" v-model="show" position="bottom">
			<view style="width: 100%;padding-top: 40rpx;display: flex;flex-wrap: wrap;justify-content: center;padding-bottom: 20px;">
				<audio :src="audioUrl" v-show="audioUrl != ''" controls name="病患说明"></audio>
				<view class="audio_func" style="width: 100%;text-align: center;">
					<view class="" style="height: 80px;line-height: 80px;display: flex;justify-content: center;
					align-items: center;">
						<uv-icon name="pause-circle" color="#2979ff" size="56" @click="stopRecording"
							v-if="audioStatus"></uv-icon>
						<uv-icon name="play-circle" color="#2979ff" size="56" @click="startRecording" v-else></uv-icon>

					</view>
					<text class="record_time"
						style="width: 50px;height: 30px;line-height: 30px;">{{formattedDuration}}</text>
					<view class=""
						style="width: 80%;margin-left: 10%;display: flex;justify-content: space-between;height: 40px;align-items: center;">
						<!-- <view style="line-height: 3;color: #ff4242;width: 60px;" @click="resetRecording()">重置</view> -->
						<uv-button type="error" text="重置" @click="resetRecording()"></uv-button>
						<uv-button type="primary" text="上传" @click="onClosea()"></uv-button>
						<!-- <view style="line-height: 3;color: #8f8f8f;width: 60px;" @click="onClosea()">上传</view> -->
					</view>
				</view>

			</view>
		</wd-popup>

这里用到的组件是Wot Design uni

2、事件:

	const recorderManager = ref(null);
	
	const duration = ref(0);
	// 音频时间
	const timer = ref(null);
	// 状态
	const audioStatus = ref(false);
	// 音频地址
	const audioUrl = ref("");
	// 重置
	const reset = ref(false);
	// 打开录音弹窗
	const show = ref(false)
	const showaPopup = (type) => {
		show.value = true
	}

	// 上传录音弹窗
	const onClosea = () => {
		if (audioUrl.value == '') {
			uni.showToast({
				title: '请输传入录音',
				icon: 'none'
			});
			return
		}
		show.value = false
	}

	// 监听录音状态变化  
	const handleStart = () => {
		console.log('录音开始');
		audioStatus.value = true;
		duration.value = 0;
		if (timer.value) clearInterval(timer.value);
		timer.value = setInterval(() => {
			duration.value++;
		}, 1000);
	};

	// 监听录音停止  
	const handleStop = (res) => {
		console.log('录音停止', res.tempFilePath);
		if (!reset.value) {
			audioUrl.value = res.tempFilePath;
		} else {
			audioUrl.value = '';
		}
		audioStatus.value = false;
		if (timer.value) clearInterval(timer.value);
	};

	// 开始录音  
	const startRecording = () => {
		recorderManager.value = uni.getRecorderManager();
		recorderManager.value.start({
			format: 'mp3'
		});
		recorderManager.value.onStart(handleStart);
		recorderManager.value.onStop(handleStop);
	};

	// 停止录音  
	const stopRecording = () => {
		reset.value = false;
		duration.value = 0;
		if (recorderManager.value) recorderManager.value.stop();
	};

	// 重置录音  
	const resetRecording = () => {
		if (recorderManager.value) recorderManager.value.stop();
		reset.value = true;
		audioUrl.value = '';
		duration.value = 0;
	};

	// 格式化录音时长  
	const formattedDuration = computed(() => {
		let minutes = Math.floor(duration.value / 60);
		let seconds = duration.value % 60;
		return `${minutes}:${seconds < 10 ? '0' + seconds : seconds}`;
	});

这里使用的语法主要是vue3的,主要流程为点击添加音频弹出弹窗,然后点击开始即可开始录音,点击重置将之前的录音地址删除,并清除路由实例。

拜拜ヾ(•ω•`)o


http://www.kler.cn/news/284305.html

相关文章:

  • 鸿蒙OS试题(4)
  • DSP48E2使用以及FIR滤波器定点设计实现与优化
  • 小琳AI课堂:生成对抗网络(GANs)
  • HarmonyOS开发实战( Beta5版)Web组件开发性能提升指导
  • 处理.NET Core中的时区转换问题
  • 帕金森患者在运动时有哪些类型的运动推荐?
  • SpringWeb后端开发-登录认证
  • CSS中的`z-index`属性是如何工作(注意事项)
  • (苍穹外卖)day03菜品管理
  • 5Kg负重30分钟长航时多旋翼无人机详解
  • MySQL简介和管理
  • 为什么我的手机卡需要快递员给激活?这到底安全吗?
  • Web攻击-XSS、CSRF、SQL注入
  • SimpleTranslationAIAgent:基于C#与LLM的翻译AI Agent
  • 国产游戏行业的技术进步与未来展望
  • Java | Leetcode Java题解之第385题迷你语法分析器
  • Linux 配置wireshark 分析thread 使用nRF-Sniffer dongle
  • kafak推送消息。
  • jenkins安装k8s插件发布服务
  • 项目中Redis常见的一些问题(缓存穿透,缓存雪崩,内存耗尽等)
  • Elasticsearch - SpringBoot 查询 es 相关示例
  • Linux Debian12安装flameshot火焰截图工具
  • 线段树维护更多类型的信息
  • c++ 分布式服务器 1
  • Linux | 进程池技术解析:利用无名管道实现并发任务处理(含实现代码)
  • NTP时间服务器是什么?功能是什么?京准电钟
  • 今日(2024年8月30日)科技新闻(本周)
  • Git之2.5版本重要特性及用法实例(五十七)
  • 《机器学习》【项目】 爬虫爬取数据、数据分词、贝叶斯算法、判断分类 <完整实战详解> (全篇完结)
  • ajax学习笔记