tencent cloud

Feedback

Screen Recorder

Last updated: 2024-03-05 15:07:43

    createMediaRecorder

    This API is used via MediaRecorder wx.createMediaRecorder(Object canvas, Object options).
    Feature Description: Creates a WebGL screen recorder, capable of frame-by-frame recording of images rendered on WebGL and exporting them as video files.
    Parameter and Description:
    Object canvas, a WebGL object, is obtained either through the node object via SelectorQuery or through the offscreen WebGL Canvas object created by wx.createOffscreenCanvas.
    Object options。
    Attribute
    Type
    Default value
    Required
    Description
    duration
    number
    600
    No
    Specify the duration of the recording (in seconds), which will automatically stop when the limit is reached. The maximum limit is 7200 and the minimum is 5.
    videoBitsPerSecond
    number
    1000
    No
    Video bitrate (in kbps), with a minimum value of 600 and a maximum of 3,000.
    gop
    number
    12
    No
    Video keyframe interval
    fps
    number
    24
    No
    Video fps
    width
    number
    canvas.width
    No
    Canvas recording width
    height
    number
    canvas.height
    No
    Canvas recording height
    Return value: MediaRecorder
    Sample Code
    // Prepare the canvas object, which can be a node object declared in wxml.
    const canvas = await new Promise(resolve => {
    wx.createSelectQuery().select('#canvas').node(res => resolve(res.node)).exec()
    })
    // It can also be an offscreen canvas created by wx.createOffscreenCanvas.
    const canvas = wx.createOffscreenCanvas()
    canvas.width = 300
    canvas.height = 150
    
    // Prepare a canvas drawing function (here we use three.js)
    const THREE = require('threejs-miniprogram').createScopedThreejs(canvas)
    const camera = new THREE.PerspectiveCamera(70, canvas.width / canvas.height, 1, 1000)
    const scene = new THREE.Scene()
    const texture = await new Promise(resolve => new THREE.TextureLoader().load('./test.png', resolve)) // Prepare an image to be loaded as a texture
    const geometry = new THREE.BoxBufferGeometry(200, 200, 200)
    const material = new THREE.MeshBasicMaterial({ map: texture })
    const mesh = new THREE.Mesh(geometry, material)
    camera.position.z = 400;
    scene.add(mesh)
    const renderer = new THREE.WebGLRenderer({ antialias: false })
    renderer.setPixelRatio(1)
    renderer.setSize(canvas.width, canvas.height)
    
    // Canvas drawing function
    const render = () => {
    mesh.rotation.x += 0.005
    mesh.rotation.y += 0.1
    renderer.render(scene, camera)
    }
    
    // Create a mediaRecorder
    const fps = 30
    const recorder = wx.createMediaRecorder(canvas, {
    fps,
    })
    
    // Initiate mediaRecorder
    await recorder.start()
    
    // Record a 5s video
    let frames = fps * 5
    // Draw frame by frame
    while (frames--) {
    await recorder.requestFrame()
    render()
    }
    
    // Upon completion of drawing, generate video
    const {tempFilePath} = await recorder.stop()
    // Clean up the mediaRecorder
    recorder.destroy()
    Compatibility with asynchronous interfaces of earlier versions: For mediaRecorder prior to the base library version 2.16.1, none of the interfaces return a Promise object. If compatibility with earlier versions is required, the following method can be adopted:
    // Initiate mediaRecorder
    await new Promise(resolve => {
    recorder.on('start', resolve)
    recorder.start()
    })
    
    // Draw frame by frame
    while (frames--) {
    await new Promise(resolve => recorder.requestFrame(resolve))
    render()
    }
    
    // Upon completion of drawing, generate video
    const {tempFilePath} = await new Promise(resolve => {
    recorder.on('stop', resolve)
    recorder.stop()
    })

    MediaRecorder

    Feature Description: It can be created through wx.createMediaRecorder. MediaRecorder is a WebGL screen recorder that can perform recording-related operations and export video files at the end of the recording.

    .destroy

    This method is used via Promise MediaRecorder.destroy().
    Feature Description: Cleans up the recorder.
    Return Value: Promise.

    .off

    This method is used via MediaRecorder.off(string eventName, function callback).
    Feature Description: Cancels monitoring of recording events. The callback function will no longer execute when the corresponding event is triggered.
    Parameter and Description:
    string eventName, the name of the event.
    function callback, the callback function executed when the event is triggered.

    .on

    This method is used via MediaRecorder.on(string eventName, function callback).
    Feature Description: Registers a callback function to monitor recording events. The callback function will be executed when the corresponding event is triggered.
    Parameter and Description: string eventName, the name of the event. The valid values are as follows.
    Value
    Description
    start
    Recording start event
    stop
    Recording ending event
    pause
    Recording pause event
    resume
    Recording continuation event
    timeupdate
    Recording update event

    .pause

    This method is used via Promise MediaRecorder.pause().
    Feature Description: Pauses recording.
    Return Value: Promise.

    .requestFrame

    This method is used via Promise MediaRecorder.requestFrame(function callback).
    Feature Description: Requests the next frame recording, starting recording the current frame after completing a frame rendering in the callback.
    Parameter and Description: Function callback.
    Return Value: Promise.

    .resume

    This method is used via Promise MediaRecorder.resume().
    Feature Description: Resumes recording.
    Return Value: Promise.

    .start

    This method is used via Promise MediaRecorder.start().
    Feature Description: Starts recording.
    Return Value: Promise.

    .stop

    This method is used via Promise MediaRecorder.stop().
    Feature Description: Pauses recording.
    Return Value: Promise.
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support