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

相关推荐

  1. Uniapp程序通过camera实现视频拍摄

    2023-12-16 05:22:03       57 阅读
  2. uniapp,微信程序,在map外与中的代码区别

    2023-12-16 05:22:03       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-16 05:22:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-16 05:22:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-16 05:22:03       82 阅读
  4. Python语言-面向对象

    2023-12-16 05:22:03       91 阅读

热门阅读

  1. Ansible

    Ansible

    2023-12-16 05:22:03      43 阅读
  2. 【置顶】 本博博文汇总

    2023-12-16 05:22:03       51 阅读
  3. cfa一级考生复习经验分享系列(五)

    2023-12-16 05:22:03       60 阅读
  4. 《小聪明》

    2023-12-16 05:22:03       46 阅读