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

相关推荐
奶糖 肥晨3 小时前
解决 UniApp 自定义弹框被图片或 Canvas 覆盖的 Bug
uni-app·bug
荷花微笑4 小时前
HBuilderX升级,Vue2 scss 预编译器默认已由 node-sass 更换为 dart-sass
uni-app·css3
weixin_lynhgworld4 小时前
从闲置到珍宝:旧物回收小程序系统重塑物品价值
小程序·旧物回收
2501_916007479 小时前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
anyup11 小时前
🔥 🔥 为什么我建议你使用 uView Pro 来开发 uni-app 项目?
前端·vue.js·uni-app
小小怪下士_---_14 小时前
uniapp开发微信小程序自定义导航栏
前端·vue.js·微信小程序·小程序·uni-app
anyup17 小时前
🔥🔥 uView Pro:Vue3+TS重构的uni-app开源组件库,文档免费无广告!
前端·vue.js·uni-app
样子20181 天前
Uniapp 之renderjs解决swiper+多个video卡顿问题
前端·javascript·css·uni-app·html
fakaifa1 天前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
Bug改不动了1 天前
React Native 与 UniApp 对比
react native·react.js·uni-app