In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.