Skip to content

Video

saveVideoToPhotosAlbum

TIP

The API usage is as follows: wx.saveVideoToPhotosAlbum(Object object)

  • Functional description: Save the video system album. Supports mp4 video format.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    filePathstring-YesVideo file path, which can be a temporary file path or a permanent file path
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.saveVideoToPhotosAlbum({
  filePath: 'wxfile://xxx',
  success(res) {
    console.log(res.errMsg)
  },
})

createVideoContext

TIP

The API usage is as follows: VideoContext wx.createVideoContext(string id, Object this)

  • Functional description: Create a video context VideoContext Object
  • Parameters and descriptions:
    • string id: <video> component id.
    • Under the custom component, this of the current component instance, to operate the component <video> Component。
  • Return value: VideoContext

chooseVideo

TIP

The API usage is as follows: wx.chooseVideo(Object object)

  • Functional description: Shoot a video or select a video from the mobile phone album.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    sourceTypeArray.<string>['album', 'camera']NoSelect video source
    compressedbooleantrueNoWhether to compress the selected video file
    maxDurationnumber60NoShoot video, maximum shooting time, unit: seconds
    camerastring'back'NoBy default, the front or rear camera is pulled up, which cannot take effect on some Android phones because the system ROM does not support it
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Legal values

    ValueDescription
    albumSelect video from the album
    cameraShoot video with camera
  • Legal values

    ValueDescription
    backBy default, the rear camera is pulled up
    frontBy default, the front camera is pulled up
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    tempFilePathstringTemporary file path of the selected video
    durationnumberThe duration of the selected video
    sizenumberThe data size of the selected video, in B
    heightnumberReturn the height of the selected video
    widthnumberReturn the width of the selected video
  • Sample code:

js
wx.chooseVideo({
  sourceType: ['album', 'camera'],
  maxDuration: 60,
  camera: 'back',
  success(res) {
    console.log(res.tempFilePath)
  },
})

compressVideo

TIP

The API usage is as follows: wx.compressVideo(Object object)

  • Functional description: Compressed video interface, developers can specify the compression quality for compression. When more precise control is required, bitrate, fps, and resolution can be specified. When quality is passed in, these three parameters will be ignored.

  • Parameters and descriptions: Object object

    PropertiesTypeRequiredDescription
    srcstringYesVideo file path, which can be a temporary file path or a permanent file path
    qualitystringYesCompression quality, legal values
    low:Low
    medium:Medium
    high:High
    bitratenumberYesBitrate, in kbps
    fpsnumberYesFrame Rate
    resolutionnumberYesRatio relative to the resolution of the original video, value range (0, 1]
    successFunctionNoCallback function for successful interface call
    failFunctionNoCallback function for failed interface call
    completeFunctionNoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    tempFilePathstringCompressed temporary file address
    sizenumberCompressed size, in kB

chooseMedia

TIP

The API usage is as follows: wx.chooseMedia(Object object)

  • Functional description: Take or select pictures or videos from the mobile phone album.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    countnumber15NoThe maximum number of files that can be selected, up to 15 files can be supported
    mediaTypeArray.<string>['image','video']NoFile type, legal values
    image:Can only take pictures or select pictures from the album
    video:Can only take or select videos from the album
    sourceTypeArray.<string>['album', 'camera']NoSources for picture and video selection, legal values
    album:Select from the album
    camera:Shoot with the camera
    maxDurationnumber10NoThe maximum shooting time for shooting videos, in seconds. The time range is between 3s and 60s. There is no limit on the album.
    sizeTypeArray.<string>['original', 'compressed']NoOnly valid when mediaType is image, whether to compress the selected file
    camerastring'back'NoOnly valid when sourceType is camera, use the front or rear camera, legal values
    album:Use rear camera
    camera:Use front camera
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    tempFilesArray.<Object>Local temporary file list
    typestringFile type, valid values

    tempFiles structure attribute

    Structure propertiesTypeDescription
    tempFilePathstringLocal temporary file path (local directory)
    sizenumberLocal temporary file size, unit B
    durationnumberVideo duration
    heightnumberVideo height
    widthnumberVideo width
    thumbTempFilePathstringVideo thumbnail temporary file path
    fileTypestringFile type, legal values
    image:Image
    video:Video
  • Sample code:

js
wx.chooseMedia({
  count: 9,
  mediaType: ['image','video'],
  sourceType: ['album', 'camera'],
  maxDuration: 30,
  camera: 'back',
  success(res) {
    console.log(res.tempFiles.tempFilePath)
    console.log(res.tempFiles.size)
  }
})

VideoContext

  • Functional description: VideoContext instance, can be obtained through wx.createVideoContext , videoContext is bound to a <video> component through id, and the corresponding operation is <video> Component。

  • Sample code:

    Corresponding WXML file

js
<view class="section tc">
  <video
    id="myVideo"
    src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"
    enable-danmu
    danmu-btn
    controls
  ></video>
  <view class="btn-area">
    <input bindblur="bindInputBlur" />
    <button bindtap="bindSendDanmu">Dispatch the barrage</button>
  </view>
</view>
Corresponding js file:
js
function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

Page({
  onReady(res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  inputValue: '',
  bindInputBlur(e) {
    this.inputValue = e.detail.value
  },
  bindSendDanmu() {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: getRandomColor(),
    })
  },
})

Method set

exitFullScreen

TIP

The API usage is as follows: VideoContext.exitFullScreen()

  • Functional description: Exit full screen.
exitBackgroundPlayback

TIP

The API usage is as follows: VideoContext.exitBackgroundPlayback()

  • Functional description: Exit background audio playback mode.
hideStatusBar

TIP

The API usage is as follows: VideoContext.hideStatusBar()

  • Functional description: Hide the status bar, only valid in iOS full screen.
pause

TIP

The API usage is as follows: VideoContext.pause()

  • Functional description: Pause the video
play

TIP

The API usage is as follows: VideoContext.play()

  • Functional description: Play the video
requestFullScreen

TIP

The API usage is as follows: VideoContext.requestFullScreen(Object object)

  • Functional description: Enter full screen. If there is custom content that needs to be displayed in full screen, the content node needs to be placed in the video node.
  • Parameters and descriptions: Object object
    PropertiesTypeDefault valueRequiredDescription
    directionnumber-NoSet the direction of the video when in full screen. If not specified, it will be automatically determined based on the aspect ratio
  • Legal value of object.direction
    ValueDescription
    0Normal vertical
    9094 degrees counterclockwise of the screen
    -9094 degrees clockwise of the screen
requestBackgroundPlayback

TIP

The API usage is as follows: VideoContext.requestBackgroundPlayback()

  • Functional description: Enter background audio playback mode.
seek

TIP

The API usage is as follows: VideoContext.seek(number position)

  • Functional description: Jump to the specified position
  • Parameters and descriptions: number position, The position to jump to, unit: s.
sendDanmu

TIP

The API usage is as follows: VideoContext.sendDanmu(Object data)

  • Functional description: Send bullet screen
  • Parameter: Object data,Bullet screen content
    PropertiesTypeDefault valueRequiredDescription
    textstring-YesBullet screen text
    colorstring-NoBullet screen color
showStatusBar

TIP

The API usage is as follows: VideoContext.showStatusBar()

  • Functional description: Show the status bar, only valid in iOS full screen.
stop

TIP

The API usage is as follows: VideoContext.stop()

  • Functional description: Stop video.