Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does WeChat Mini Programs save the download file name automatically?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容介绍了"微信小程序怎么实现自动保存下载文件名"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

问题的提出

小程序使用wx.playVoice播放音频时,需要从网上下载播放的对象文件,但是每次播放都要下载的话,就太傻了。好在小程序提供了保存文件的功能。

思路

准备一个url到缓存文件的映射,当小程序成功的下载播放铃声以后,自动保存下载的文件名。下次播放同一个文件时确认是否存在已经下载的文件,如果有则直接播放已经下载的文件。由于某些不知道的原因,我们假设已经下载的文件也可能播放失败,对策是重新下载该文件。

实现

下面的代码在app.js中,是这个处理的主线。

//播放铃声文件,优先使用缓存文件,根据需要下载。

playRingtone: function(index) {

var that = this

var url = that.getRingtoneUrl(index)

var savedFile = that.globalData.urlMap[url]

if (savedFile != undefined){

//已经存在缓存文件,直接播放缓存文件

that.addLog('播放缓存铃声')

wx.playVoice({

filePath: savedFile,

fail:function(){

//播放缓存文件失败,清除缓存文件信息

that.globalData.urlMap[url] = undefined;

wx.setStorageSync('urlMap', that.globalData.urlMap);

//下载并播放缓存文件

that.downloadAndPlayRingtone(url)

},

})

}else{

//没有缓存文件,下载并播放

that.downloadAndPlayRingtone(url)

}

},

程序中用到了downloadAndPlayRingtone函数,其代码如下。

//下载,保存,播放铃声文件。

downloadAndPlayRingtone:function(url){

var that = this

that.downloadFile({

url: url,

success: function (savedFilePath) {

//that.addLog('saveFileSuccess')

//下载成功,播放文件

that.addLog('播放下载铃声')

wx.playVoice({

filePath: savedFilePath,

})

//更新缓存文件信息。

that.globalData.urlMap[url] = savedFilePath;

wx.setStorageSync('urlMap', that.globalData.urlMap);

}

})

},

下载铃声和播放铃声是应用领域的功能,为了促进代码重用,我们又抽出一个共同函数downloadFile。

提炼共通功能的关键并不在于它会被多少次使用,而在于它可以成为一个共通的功能。

//下载并保存文件

downloadFile: function(parameter){

var that = this

wx.downloadFile({

url: parameter.url,

success: function (res) {

//保存临时文件,以供将来使用

wx.saveFile({

tempFilePath: res.tempFilePath,

success: function (save_res) {

parameter.success(save_res.savedFilePath)

}

})

},

})

},

执行结果

可以看到,第一次是播放下载铃声,以后都是播放缓存铃声。log的出处可从代码中找到。

The content of "Weixin Mini Programs (Mini) How to Automatically Save Download File Names" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report