Just after this iteration, I did an H5 project about singing activities. It was so much trouble that I had to work overtime for half a month.

This article is mainly about wechat JS-SDK to achieve mobile phone recording, in fact, as long as the secure domain name, to obtain basic information is more than half of the success, but there is a hole is that the recording file in wechat server can only be retained for 3 days.

Wechat open documents

The early stage of the work

1. Bind the domain name

It must be a secure domain name for the record

2. Import the JS file

<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
Copy the code

3. Verify permissions

wx.config({
    appId: ' '.// The unique identifier of the public account
    timestamp: ' '.// Generate the timestamp of the signature
    nonceStr: ' '.// Generate a random string of signatures
    signature: ' './ / signature
    debug: true.// If debug mode is enabled, the return value of all API calls will be displayed in the client alert
    jsApiList: [ // The JS interface to use
      'startRecord'.'stopRecord'.'onVoiceRecordEnd'.'uploadVoice'.'downloadVoice'.'playVoice'.'pauseVoice'.'onVoicePlayEnd'.'checkJsApi'.'openLocation'.'getLocation'.'onMenuShareTimeline'.'onMenuShareAppMessage']});Copy the code

The official start of the

1. Start recording

wx.startRecord({
    success: function () {
        console.log('Start recording');
    },
    cancel: function () {
        alert('User refuses to authorize recording'); }})Copy the code

2. End the recording

wx.stopRecord({
    success(res) {
        console.log(res.localId);// Returns the local ID of the audio
    },
    fail(error){
        console.log(error)
    }
});
Copy the code

3. Upload the recording

Notice that this points to

// This upload is uploaded to the wechat server, valid for only 3 days. If it is used for a long time, it needs to be uploaded to the personal or company server
wx.uploadVoice({
    localId: this.localId, // The local ID of the audio to be uploaded, obtained by the stopRecord interface
    isShowProgressTips: 1.// The default value is 1, indicating progress
    success: function (res) {
        console.log(res)
    }
});
Copy the code

The end of the

Leave a message if you have any questions