This API is used via MediaRecorder wx.createMediaRecorder(Object canvas, 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 |
// 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 = 300canvas.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 textureconst 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 functionconst render = () => {mesh.rotation.x += 0.005mesh.rotation.y += 0.1renderer.render(scene, camera)}// Create a mediaRecorderconst fps = 30const recorder = wx.createMediaRecorder(canvas, {fps,})// Initiate mediaRecorderawait recorder.start()// Record a 5s videolet frames = fps * 5// Draw frame by framewhile (frames--) {await recorder.requestFrame()render()}// Upon completion of drawing, generate videoconst {tempFilePath} = await recorder.stop()// Clean up the mediaRecorderrecorder.destroy()
// Initiate mediaRecorderawait new Promise(resolve => {recorder.on('start', resolve)recorder.start()})// Draw frame by framewhile (frames--) {await new Promise(resolve => recorder.requestFrame(resolve))render()}// Upon completion of drawing, generate videoconst {tempFilePath} = await new Promise(resolve => {recorder.on('stop', resolve)recorder.stop()})
This method is used via Promise MediaRecorder.destroy().
This method is used via MediaRecorder.off(string eventName, function callback).
This method is used via MediaRecorder.on(string eventName, function callback).
Value | Description |
start | Recording start event |
stop | Recording ending event |
pause | Recording pause event |
resume | Recording continuation event |
timeupdate | Recording update event |
This method is used via Promise MediaRecorder.pause().
This method is used via Promise MediaRecorder.requestFrame(function callback).
This method is used via Promise MediaRecorder.resume().
This method is used via Promise MediaRecorder.start().
This method is used via Promise MediaRecorder.stop().
Was this page helpful?