Uniapp小程序通过camera组件实现视频拍摄

uni中可以通过调用api的方式去拍摄或者是选择相册的视频,但是在这里我们不采取这种方式,因为调用api的方式,必须跳转,而我们需要在页面中实现,下面看下具体步骤吧...

javascript 复制代码
<camera v-if="!srcUrl && showCamera" device-position="back" flash="auto" binderror="onCameraError" style="width: 100%; height: 400rpx;"></camera>
<video v-if="srcUrl" id="myVideo" :src="srcUrl" controls></video>
<view @click="startShoot">
	开始
</view>
<view>
	------------------------------------------
	------------------------------------------
</view>
<view @click="stopShoot">
	结束
</view>


data() {
	return {
		cameraContext: null,
		showCamera: false,
		srcUrl: null,
		timer: null,
		totalSeconds: 0
	};
}

接下来看下,怎么样实现拍摄

javascript 复制代码
onReady() {
	this.cameraContext = uni.createCameraContext()
}

methods: {
	// 开始拍摄
	startShoot() {
		this.totalSeconds = 0
		this.showCamera = true
		this.cameraContext.startRecord({
		timeoutCallback: () => {
			console.log(this.totalSeconds,'超出限制时长');
		},
		timeout: 300,
		success: (res) => {
			this.timer = setInterval(() => {
				this.totalSeconds++
			}, 1000)
		    console.log(res, '开始拍摄');
		},
		fail: (err) => {
				this.showCamera = false
				uni.showToast({
				title: '录制视频失败',
				icon: 'none',
				mask: true
			    })
		    }
		})
	},

    // 结束拍摄
    stopShoot() {
		if(this.timer) clearInterval(this.timer)
		    this.cameraContext.stopRecord({
			compressed: true,
			success: (res) => {
				this.srcUrl = res.tempVideoPath
				// TODO 获取数据帧
				console.log(res, '结束拍摄');
			},
			fail: (err) => {
				uni.showToast({
				title: '录制视频失败',
				icon: 'none',
				mask: true
				})
				console.log(err, '录制视频失败');
			},
			complete: () => {
				this.showCamera = false
			}
		  })
	  },
}

到这里已经基本实现了所需的功能,但是还需要处理一下拍摄超时的情况

javascript 复制代码
watch: {
	totalSeconds: {
		handler(newVal){
			if(newVal >= 270) {
				console.log(newVal, 'newVal');
				this.stopShoot()
			}
		}
	}
}

感觉对你有帮助的小伙伴可以留个star...

相关推荐
说私域1 小时前
基于开源链动2+1模式、AI智能名片与S2B2C商城小程序的运营创新研究
人工智能·小程序
我很苦涩的2 小时前
原生小程序使用echarts
前端·小程序·echarts
小小王app小程序开发2 小时前
盲盒小程序避坑指南:开发运营全流程风险规避策略
小程序
快起来搬砖了2 小时前
UniApp 路由配置实战:从全局守卫到 404 页面优雅处理
uni-app
5335ld3 小时前
uniapp-APP端table列表左侧第一列固定、头部固定
windows·uni-app
风月歌3 小时前
基于微信小程序的学习资料销售平台源代码(源码+文档+数据库)
java·数据库·mysql·微信小程序·小程序·毕业设计·源码
半兽先生4 小时前
微信小程序与web-view页面双向通信
前端·微信小程序·小程序
小明记账簿4 小时前
微信小程序中Crypto.js加密解密
微信小程序·小程序·加密·解密
一个假的前端男4 小时前
uni-app App 端长按录音的工程级实现
uni-app
无名前端小白4 小时前
uniapp 安卓离线打包, 无法调起系统安装应用
uni-app