Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does WeChat Mini Programs realize the independent module

Shulou Source: shulou.com Published: 2022-06-01 03:49:21 09月26日 Update

This article mainly introduces the relevant knowledge of "how to achieve an independent module by WeChat Mini Programs". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to achieve an independent module by WeChat Mini Programs" can help you solve the problem.

Voiceplay.js

/ / voice player

Function prepare () {

Var that = this

Var info_pair = wx.getStorageSync ('url2FileMap') | | []

This.urlMap = new Map ()

Info_pair.forEach (function (pair) {

That.urlMap.set (pair.url, pair.file)

});

}

Function saveMap () {

Var info_pair = []

This.urlMap.forEach (function (file, url, map) {

Info_pair.push ({url:url, file:file})

});

Wx.setStorageSync ('url2FileMap', info_pair)

}

/ / play ringtone files, give priority to using cache files, and download them as needed.

/ / var url = that.getRingtoneUrl (index)

Function play (url) {

Const app = getApp ()

Var that = this

Var savedFile = this.urlMap.get (url)

If (savedFile! = undefined) {

/ / the cache file already exists. Play the cache file directly.

App.addLog ('play cache ringtone')

Wx.playVoice ({

FilePath: savedFile

Fail: function () {

/ / failed to play the cache file. Clear the cache file information

That.urlMap.delete (url)

That.saveMap ()

/ / download and play the cache file

That.downloadAndPlay (url)

}

})

} else {

/ / No cache file, download and play

App.addLog ('that.downloadAndPlay')

That.downloadAndPlay (url)

}

}

/ / download, save, and play ringtone files.

Function downloadAndPlay (url) {

Const app = getApp ()

Var that = this

That.downloadFile ({

Url: url

Success: function (savedFilePath) {

App.addLog ('saveFileSuccess')

/ / download successfully and play the file

App.addLog ('play download ringtone')

Wx.playVoice ({

FilePath: savedFilePath

})

/ / update cache file information.

That.urlMap.set (url,savedFilePath)

That.saveMap ()

}

})

}

/ / download and save the file

Function downloadFile (parameter) {

Const app = getApp ()

Var that = this

Wx.downloadFile ({

Url: parameter.url

Success: function (res) {

/ / Save temporary files for future use

Wx.saveFile ({

TempFilePath: res.tempFilePath

Success: function (save_res) {

Parameter.success (save_res.savedFilePath)

}

})

}

})

}

Module.exports = {

Prepare: prepare

Play: play

DownloadAndPlay:downloadAndPlay

DownloadFile:downloadFile

SaveMap:saveMap

}

Usage

After the above processing, we have got a general audio playback module, which can be used directly in other Mini Program. There are three steps, one line for each step.

Import module

Const voiceplayer = require ('. / utils/voiceplayer.js')

The directory may need to be modified according to the directory structure.

Initialization

Voiceplayer.prepare ()

Call the prepare function to get the saved cache file information.

Play audio

Voiceplayer.play (media_file_url)

Call the play function to play the specified audio file. If there is no cache file, first download the file to play and save it for next use. Audio files need to be prepared separately.

This is the end of the introduction of "how WeChat Mini Programs realizes the independent module". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Files caches modules programs ringtones audio standalone information knowledge functions directories industries updates different practical successful one line download ringtones content practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux macOS Docker Shulou Tech Info