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

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

<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
	};
}

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

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
			}
		  })
	  },
}

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

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

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值