目录

Recording

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "system.record"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import record from '@system.record'

Or

var record = require("@system.record")

Application Scope

Item

Description

Applicable devices

Mobile phones, tablets, and telematics devices

Applicable regions

Global

Definition

API

Description

record.start(OBJECT)

Starts recording.

record.stop(OBJECT)

Stops recording.

[h2]record.start(OBJECT)

Description

Starts recording.

OBJECT parameters

Parameter

Type

Default Value

Mandatory

Description

duration (1010+)

number

-1

No

Recording duration, in milliseconds. If a valid value of duration is passed, the recording automatically stops when the specified duration expires.

sampleRate (1010+)

number

-

No

Sampling rate. The options are 8000, 16000, and 44100.

numberOfChannels (1010+)

number

-

No

Number of recording channels. The options are 1 and 2.

encodeBitRate (1010+)

number

-

No

Encoding bit rate. If format is set to aac, the mapping between sampling rates and bit rates is described in the following table.

format (1010+)

string

3gpp

No

Audio format. The options are 3gpp, amr, and aac. The default value is 3gpp.

success

function

-

No

Callback upon success.

fail

function

-

No

Callback upon failure.

complete

function

-

No

Callback upon completion.

Mapping between sampling rates and bit rates (1010+)

Sampling Rate

Bit Rate

8000

16000-48000

11025

16000-48000

12000

24000-64000

16000

24000-64000

22050

32000-128000

24000

32000-128000

32000

48000-192000

44100

64000-320000

48000

64000-320000

Parameters returned by success

Parameter

Type

Description

uri

string

Path for storing recordings, which is in the cache of the app. Recording files are in 3GPP format.

Result codes returned by fail

Result Code

Description

201

The user has rejected the request for the recording permission.

Sample code

record.start({
        duration: 20000,
        sampleRate:8000,
        numberOfChannels:1,
        encodeBitRate:16000,
        format:'aac',

        success: function (ret) {
          that.pathRecordSave = ret.uri;
        },
        fail: function (erromsg, errocode) {
          console.log('record.startRecord----------' + errocode + ': ' + erromsg)
        },
         complete: function () {
           console.log('record.startRecord----------complete')
         }
})

[h2]record.stop(OBJECT)

Description

Stops recording.

Sample code

record.stop()

Demo

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <div class="mlr-container">
      <text class="txt">RecordPath:{{audioUri}}</text>
    </div>
    <div class="mlr-container">
      <input onclick="updateRecord" type="button" value="{{isRecording ? 'Stop Record' : 'Start Record'}}" class="btn-blue"/>
    </div>
  </div>
</template>

<style lang="sass">
.container {
  flex-direction: column;
  justify-content: center;
  padding-bottom: 50px;
}
.mlr-container {
  margin-right:32px;
  margin-left:32px;
}
.btn-blue {
    background-color: #0faeff;
    width: 100%;
    height: 80px;
    border-radius: 40px;
    color: #fff;
    font-size: 32px;
    font-weight: 500;
  }
</style>

<script>
  import record from '@system.record';
  import audio from '@system.audio';
  module.exports = {
    public: {
        isRecording:false,
        audioUri:''
    },
    onInit: function () {
      this.$page.setTitleBar({ text: 'record' });
    },
    updateRecord(){
      let that = this;
      if(!this.isRecording)
      {
        this.isRecording = true;
        record.start({
          duration:20000,
          sampleRate: 44100,
          numberOfChannels: 1,
          encodeBitRate: 192000,
          format: '3gpp',//3gpp/amr/aac
          success: function (data) {
            that.audioUri = data.uri;
          }
        })
      }else{
        record.stop();
        this.isRecording = false;
      }
    }
  }
</script>

The following figure shows the final effect.

Version Change History

Version

Date

Description

1010

2018-04-20

Added parameters to the record.start API to set the recording duration, recording format, sampling rate, number of recording channels, and bit rate.

Tips

  1. The recommended sampling rates are 8000, 16000, and 44100. For recording in the AAC format, please refer to the mapping between sampling rates and bit rates described in the table above.
  2. If the recording duration is too short, the recording may fail.