uni-app利用renderjs+webrtc 实现App音视频通话功能

<template>
	<view class="moveArea">
		<movable-area class="movablearea" style="right: 0rpx;">
			<movable-view :x="x" :y="y" :direction="direction" class="movableview" :animation="false" :disabled="!telMinShow"
				@touchend="handleTouchEnd" @change="handleChange" style="width:100vw;" :style="{'padding-left':telMinShow?'200rpx':'0rpx'}">
				<view class="frame" :style="{'--background-image':'url' +'(' + avatar + ')'}" @click="telMax" :class="telMinShow? 'frame-min':''">
					<image :src="udata.Avatar"
						style="position: fixed;filter: blur(10px);top: 0rpx;left: 0rpx;width: 100%;height: 100vh;">
					</image>
					
					<u-navbar :is-back="false" :title="`${(teltype>0?'视频':'语音')}通话`"
						:background="{'background' :'rgba(255, 255, 255, 0)'}" :title-color="'#ffffff'">
						<view class="slot-wrap" style="margin-left: 30rpx;" @click.stop="telMin">
								<image src="/https/blog.csdn.net/static/message/68997b56a4789c4230ebedc8aa7ff95.png" style="width: 40rpx;height: 40rpx;"></image>
						</view>
					</u-navbar>
					<!-- <image :src="avatar" class="img"></image> -->

					<!-- 群聊视频/语音通话 -->
					<view class="frame-content flexcumal" v-if="RoomType == 1">
						<view class="avatar">
							<view :class="[computedAvatar(telusers)]" :id="`viewVideo_${items.ID}`"
								v-for="(items,indexs) in telusers" :key="indexs" v-if="items.state<2">
								<!-- <image :src="items.Avatar" :class="[computedAvatar(list)]" /> -->
							</view>
						</view>
						<!-- items.ID!=udata.ID&& -->
						<!-- <view v-else>
					<view style="border: 2rpx solid black;" :id="`viewVideo_${udata.ID}`" ></view>
					<view style="border: 2rpx solid black;position: fixed;top:10%;right: 10%;" :id="`viewVideo_${userId}`" >
					</view>
			</view> -->
						<!-- <view style="position: fixed;top: 60%;"> -->
						<view class="name">{
  {udata.UserName}}</view>
						<view class="text" v-if="answer">{
  {formatTime}}</view>
						<view class="text" v-else>等待对方接受邀请...</view>
						<!-- </view> -->
					</view>

					<!-- 单聊视频/语音通话 -->
					<view class="frame-content flexcumal" style="margin-left: 0rpx;" v-else>
						<view v-if="teltype">
							<!-- <view>1122</view> -->
							<view class="myVideoClass" :id="`viewVideo_${udata.ID}`" v-if="myOtherIs == 0"></view>
							<view class="myVideoClass" :id="`viewVideo_${userId}`" v-if="myOtherIs == 1"></view>
							<view class="otherVideoClass" :id="`viewVideo_${userId}`" v-if="myOtherIs == 0"
								@click.stop="Toggle(1)">
							</view>
							<view class="otherVideoClass" :id="`viewVideo_${udata.ID}`" v-if="myOtherIs == 1"
								@click.stop="Toggle(0)"></view>
						</view>
						<view v-else>
							<view style="margin-top: 25%;width: 100vw;" class="flexcumal">
								<image :src="userAvatar" style="height: 300rpx;width: 300rpx;"></image>
							</view>
							<view class="myVideoClass" :id="`viewVideo_${udata.ID}`" v-show="false"></view>
							<view class="myVideoClass" :id="`viewVideo_${userId}`" v-show="false"></view>
							<!-- <view class="otherVideoClass" :id="`viewVideo_${userId}`"></view> -->
						</view>

						<view style="position: fixed;top: 60%;width: 100vw;" class="flexcumal">
							<view class="name">{
  {udata.UserName}}</view>
							<view class="text" v-if="answer">{
  {formatTime}}</view>
							<view class="text" v-else>等待对方接受邀请...</view>
						</view>
					</view>

					<!-- 接通 -->
					<view class="frame-buttom flexal" style="padding-left: 23rpx;justify-content: center;"
						v-if="answer">

						<!-- 麦克风 -->
						<view>
							<view class="flexcumal" style="margin: 0rpx 47rpx;" v-if="isMicrophone"
								@click="isMicrophone = !isMicrophone">
								<image src="/https/blog.csdn.net/static/video/f88d404003adc4cef0bd294e108a8b0.png"
									style="width: 134rpx;height: 134rpx;">
								</image>
								<view style="font-weight: 400;font-size: 30rpx;color: #FFFFFF;margin-top: 20rpx;">麦克风
								</view>
							</view>
							<view class="flexcumal" style="margin: 0rpx 47rpx;" v-else
								@click="isMicrophone = !isMicrophone">
								<view class="frame-icon flexcen">
									<image src="/https/blog.csdn.net/static/video/96807520ac0c78fbe419e3ac0a95443.png"
										style="width: 40rpx;height: 48rpx;">
									</image>
								</view>
								<view style="font-weight: 400;font-size: 30rpx;color: #FFFFFF;margin-top: 20rpx;">麦克风已关
								</view>
							</view>
						</view>



						<!-- 取消 -->
						<view class="flexcumal" style="margin: 0rpx 47rpx;" @click="overTelFun()">
							<image src="/https/blog.csdn.net/static/video/a90daeaeaaf3ef48bc0685042f66e00.png"
								style="width: 134rpx;height: 134rpx;">
							</image>
							<view style="font-weight: 400;font-size: 30rpx;color: #FFFFFF;margin-top: 20rpx;">取消</view>
						</view>

						<!-- 摄像头 -->
						<view v-if="teltype == 1">
							<view class="flexcumal" style="margin: 0rpx 47rpx;" v-if="isCamera"
								@click="isCamera = !isCamera">
								<image src="/https/blog.csdn.net/static/video/b69f4bca24df9641bd3b5d13b4b7887.png"
									style="width: 134rpx;height: 134rpx;">
								</image>
								<view style="font-weight: 400;font-size: 30rpx;color: #FFFFFF;margin-top: 20rpx;">摄像头
								</view>
							</view>
							<view class="flexcumal" style="margin: 0rpx 47rpx;" v-else @click="isCamera = !isCamera">
								<view class="frame-icon flexcen">
									<image src="/https/blog.csdn.net/static/video/5be3dcd235de1bacace1a3db17b8cfb.png"
										style="width: 48rpx;height: 48rpx;">
									</image>
								</view>
								<view style="font-weight: 400;font-size: 30rpx;color: #FFFFFF;margin-top: 20rpx;">摄像头已关
								</view>
							</view>
						</view>

						<!-- @click="isCamera = !isCamera" -->
						<view v-if="teltype == 0">
							<view class="flexcumal" style="margin: 0rpx 47rpx;" v-if="loudspeaker"
								@click="loudspeaker = !loudspeaker">
								<!-- <view class="frame-icon flexcen"> -->
								<image src=&#
### Uni-app音视频功能实现 #### 音视频播放支持 Uni-app 支持多种方式来处理音频和视频文件的播放。对于视频播放遇到不同平台兼容性问题,如安卓正常而 iOS 出现异常的情况[^1],建议开发者检查并确保使用的组件遵循苹果公司的审核指南以及技术文档。 针对跨平台开发框架中的媒体控件,在 `uni-app` 下可以通过 `<video>` 和 `<audio>` 原生标签或者借助第三方库来进行操作。这些 HTML5 元素提供了基本的功能集,包括播放、暂停、调整进度条等交互行为,并且能够很好地适配主流移动操作系统。 ```html <template> <view class="container"> <!-- 视频播放 --> <video id="myVideo" :src="videoSrc" controls></video> <!-- 音频播放 --> <audio id="myAudio" :src="audioSrc" controls></audio> </view> </template> <script> export default { data() { return { videoSrc: 'path_to_video_file', audioSrc: 'path_to_audio_file' }; } }; </script> ``` 为了增强用户体验或满足特定需求,还可以考虑集成一些成熟的开源项目作为插件使用[^3]。例如 AnyRTC 提供了一个基于 WebRTC 技术构建的应用程序实例 ARCall,其中包含了丰富的实时通信特性演示,可供学习借鉴。 #### 应用生命周期管理下的录制逻辑设计 当涉及到录音录像这类长时间运行的任务时,合理利用uni-app 的应用生命周期事件就显得尤为重要了[^4]。通过监听页面加载完成后的时机启动服务端口连接;而在即将卸载前停止采集流并将已获取的数据保存下来,从而保证整个过程的安全性和稳定性。 具体来说就是: - 使用 `onLoad()` 方法初始化必要的参数设置; - 利用 `onUnload()` 或者其他合适的钩子函数释放资源; - 如果有必要的话,也可以配合后台任务 API 来延长某些关键流程的有效期。 ```javascript // Page.vue 文件内的 script 标签部分 import { createRecorderManager } from '@weixin-js-sdk/recorder-manager'; const recorderManager = createRecorderManager(); export default { onLoad(options) { // 初始化录音机配置... recorderManager.start({ duration: 60 * 1000, sampleRate: 16000, numberOfChannels: 1, encodeBitRate: 96000, format: 'mp3', // 可选 aac/mp3/amr/wav frameSize: 50 }); }, onUnload() { recorderManager.stop(); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

acycwf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值