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...

相关推荐
yuanmenglxb20042 分钟前
微信小程序核心技术栈
前端·javascript·vue.js·笔记·微信小程序·小程序
编程毕设1 小时前
【含文档+PPT+源码】基于微信小程序连锁药店商城
微信小程序·小程序
幽络源小助理1 小时前
微信小程序鲜花销售系统设计与实现
微信小程序·小程序
性野喜悲1 小时前
uniapp返回上一页接口数据更新了,页面未更新
uni-app
冰镇生鲜3 小时前
小程序·安全·胶囊·容器组件
前端·vue.js·uni-app
盛夏绽放4 小时前
uni-app云开发总结
uni-app·云开发
halo14164 小时前
uni-app 小程序中的定位问题 以及 页面安全距离
小程序·uni-app
资深前端之路4 小时前
iphonex uniapp textarea标签兼容性处理过程梳理
uni-app
努力成为包租婆14 小时前
微信小程序 van-dropdown-menu
微信·微信小程序·小程序
thinkQuadratic16 小时前
微信小程序动态设置高度,添加动画等常用操作
微信小程序·小程序