当前位置:   article > 正文

Uniapp小程序通过camera组件实现视频拍摄_uniapp cameracontext.startrecord

uniapp cameracontext.startrecord

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

  1. <camera v-if="!srcUrl && showCamera" device-position="back" flash="auto" binderror="onCameraError" style="width: 100%; height: 400rpx;"></camera>
  2. <video v-if="srcUrl" id="myVideo" :src="srcUrl" controls></video>
  3. <view @click="startShoot">
  4. 开始
  5. </view>
  6. <view>
  7. ------------------------------------------
  8. ------------------------------------------
  9. </view>
  10. <view @click="stopShoot">
  11. 结束
  12. </view>
  13. data() {
  14. return {
  15. cameraContext: null,
  16. showCamera: false,
  17. srcUrl: null,
  18. timer: null,
  19. totalSeconds: 0
  20. };
  21. }

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

  1. onReady() {
  2. this.cameraContext = uni.createCameraContext()
  3. }
  4. methods: {
  5. // 开始拍摄
  6. startShoot() {
  7. this.totalSeconds = 0
  8. this.showCamera = true
  9. this.cameraContext.startRecord({
  10. timeoutCallback: () => {
  11. console.log(this.totalSeconds,'超出限制时长');
  12. },
  13. timeout: 300,
  14. success: (res) => {
  15. this.timer = setInterval(() => {
  16. this.totalSeconds++
  17. }, 1000)
  18. console.log(res, '开始拍摄');
  19. },
  20. fail: (err) => {
  21. this.showCamera = false
  22. uni.showToast({
  23. title: '录制视频失败',
  24. icon: 'none',
  25. mask: true
  26. })
  27. }
  28. })
  29. },
  30. // 结束拍摄
  31. stopShoot() {
  32. if(this.timer) clearInterval(this.timer)
  33. this.cameraContext.stopRecord({
  34. compressed: true,
  35. success: (res) => {
  36. this.srcUrl = res.tempVideoPath
  37. // TODO 获取数据帧
  38. console.log(res, '结束拍摄');
  39. },
  40. fail: (err) => {
  41. uni.showToast({
  42. title: '录制视频失败',
  43. icon: 'none',
  44. mask: true
  45. })
  46. console.log(err, '录制视频失败');
  47. },
  48. complete: () => {
  49. this.showCamera = false
  50. }
  51. })
  52. },
  53. }

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

  1. watch: {
  2. totalSeconds: {
  3. handler(newVal){
  4. if(newVal >= 270) {
  5. console.log(newVal, 'newVal');
  6. this.stopShoot()
  7. }
  8. }
  9. }
  10. }

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/928167
推荐阅读
相关标签
  

闽ICP备14008679号