Table of Contents

Record dump files using your mini program

This article explains how to implement the recording and forwarding of AR Session dump data in a mini program.

Before you begin

Implementation method

Take using a Switch component as an example to control the start and end of recording, and call the native WeChat sharing function after completion.

Write WXML interface

Add a form component Switch to the mini-program page for controlling the start and end of recording and forwarding the records.

<switch class="switch" checked="{{dumpSessionFlag}}" bindchange="dumpSessionChange">Record data</switch>

Page logic control

In this example, a switch named "Record data" will appear on the WeChat Mini Program interface. Its state is bound to the dumpSessionFlag in the Mini Program, and it is bound to the callback function dumpSessionChange in the Mini Program. The following implementation is required in the Mini Program's ts code:

ar: null,
data: {
    //The initial state of the form component is off  
    dumpSessionFlag:false,
},
onReady() {
    //Get the xr-frame component in the scene  
    this.ar = this.selectComponent("#ar-scene");
}
dumpSessionChange(event) {
    //Toggle the display of the form component when the button is triggered  
    this.setData({
        "dumpSessionFlag":event.detail.value
    });
    if (this.ar) {
        //Call the method provided by the xr-frame component  
        this.ar.dumpSession(event.detail.value);
    }
},

Implement the core recording logic (inside the xr-frame component)

Control the recording process by calling the session.dumpSession(signal: boolean) interface:

  • Pass true: Start recording.
  • Pass false: Stop recording and return the generated temporary file path (tempFilePath).

When starting the recording, you can use the wx.showToast() method to prompt the start of recording. When the recording ends, use the wx.shareFileMessage() method to forward the recorded file via WeChat chat.

/**
 * Handle the session recording logic
 * @param signal true to start recording, false to stop recording and forward
 */
dumpSession(signal: boolean): void {
  // Call the interface to get the path
  const recordPath = session.dumpSession(signal);
  // When signal is true, the interface returns an empty string, indicating recording is in progress
  if (recordPath.length == 0) {
      wx.showToast({
          title: 'Start recording data',
          icon: 'success',
          duration: 2000
      });
      return;
  }
  // When signal is false, process the returned file path
  wx.shareFileMessage({
      filePath: recordPath,
      success() {
          wx.showToast({
              title: 'Recording forwarded successfully',
              icon: 'success',
              duration: 2000
          });
      },
      fail() {
          wx.showToast({
              title: 'Recording forwarding failed',
              icon: 'error',
              duration: 2000
          });
      }
  })
}
Note

Due to the limited local storage space of mini-programs (usually 200MB), it is recommended not to record for too long at once, and the maximum recording time should not exceed 10 minutes.