ARCameraContext.startRecord

解释:开始录像

方法参数

Object object

object参数说明

参数类型必填默认值说明
progressFunction录制进度更新的回调函数。
timeoutFunction超过 10s 或页面 onHide 时会结束录像。
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

progress 返回参数说明

参数名类型说明
progressString视频的临时路径

timeout 返回参数说明

参数名类型说明
tempVideoPathString视频的临时路径

示例

在开发者工具中预览效果

扫码体验

ARCameraContext.startRecord - 图1请使用百度APP扫码

图片示例

ARCameraContext.startRecord - 图2

ARCameraContext.startRecord - 图3

ARCameraContext.startRecord - 图4

代码示例

  • 在 swan 文件中
  1. <ar-camera ar-key="10298931" ar-type="5" flash="{{flashState}}" class="camera" bindload="loadCameraSuccess" bindmessage="message" binderror="error">
  2. <cover-view s-if="ifRecord" class="cameraState" bindtap="startRecord"> 开始录像 </cover-view>
  3. <cover-view s-else class="cameraState" bindtap="stopRecord"> 结束录像 </cover-view>
  4. <cover-view>
  5. <video src="{{VideoPath}}"></video>
  6. </cover-view>
  7. <!--重拍,保存的按钮-->
  8. <cover-view class="action-complete" s-if="isRecordComplete">
  9. <cover-image class="giveup-img" src="https://b.bdstatic.com/miniapp_cl_ar_back.png" bindtap="reset" />
  10. <cover-image class="save-img" src="https://b.bdstatic.com/miniapp_cl_ar_save.png" bindtap="save" />
  11. </cover-view>
  12. </ar-camera>
  • 在 js 文件中
  1. Page({
  2. data: {
  3. photoSrc: '',
  4. ifRecord: true,
  5. VideoPath: '',
  6. isRecordComplete: false
  7. },
  8. onReady() {
  9. // 初始化ar相机
  10. this.ARCameraContext = swan.createARCameraContext();
  11. },
  12. startRecord() {
  13. let that = this;
  14. this.ARCameraContext.startRecord({
  15. success: res => {
  16. that.setData({'ifRecord': false});
  17. }
  18. });
  19. },
  20. stopRecord() {
  21. let that = this;
  22. this.ARCameraContext.stopRecord({
  23. success: res => {
  24. that.setData({'ifRecord': true});
  25. swan.showModal({
  26. title: 'success',
  27. content: JSON.stringify(res)
  28. });
  29. that.setData({
  30. VideoPath: res.tempVideoPath,
  31. isRecordComplete: true
  32. })
  33. }
  34. });
  35. },
  36. reset() {
  37. this.ARCameraContext.reset({
  38. success: res => {
  39. this.setData({
  40. isRecordComplete: false,
  41. isUpload: false,
  42. photoSrc: '',
  43. videoSrc: ''
  44. }, () => {
  45. this.changeAction(this.data.cameraState);
  46. });
  47. },
  48. fail: e => {
  49. swan.showToast({
  50. title: JSON.stringify(e),
  51. icon: 'none'
  52. });
  53. }
  54. });
  55. },
  56. save() {
  57. swan.saveVideoToPhotosAlbum({
  58. filePath: this.data.VideoSrc,
  59. success: handleSaveSuccess.bind(this),
  60. fail: handleSaveFail.bind(this)
  61. });
  62. function handleSaveSuccess(res) {
  63. swan.showToast({
  64. title: '保存成功',
  65. success: () => {
  66. this.reTake();
  67. }
  68. });
  69. this.setData({
  70. isRecordComplete: true,
  71. isUpload: false
  72. });
  73. }
  74. function handleSaveFail(e) {
  75. swan.showToast({title: '保存失败'});
  76. }
  77. }
  78. });