小程序采集錄音并上傳到后臺(tái)
本文實(shí)例為大家分享了小程序錄音上傳的具體代碼,供大家參考,具體內(nèi)容如下
demo.wxml
<view> <button bindtap='startRecordMp3' type='primary'>錄音開(kāi)始(mp3)</button> </view> <view> <button bindtap='stopRecord' type='primary'>錄音結(jié)束</button> </view> <view> <button bindtap='playRecord' type='primary'>播放錄音</button> </view> <view> <button bindtap='sendRecord' type='primary'>播放錄音</button> </view>
demo.wxss
view{ padding: 15px; }
demo.js
// pages/newMusic/index.js const recorderManager = wx.getRecorderManager(); Page({ data: { }, /** * 提示 */ tip: function (msg) { wx.showModal({ title: '提示', content: msg, showCancel: false }) } /** * 錄制mp3音頻 */ , startRecordMp3: function () { recorderManager.start({ format: 'mp3' }); } /** * 停止錄音 */ , stopRecord: function () { recorderManager.stop() } /** * 播放錄音 */ , playRecord: function () { var that = this; var src = this.data.src; if (src == '') { this.tip("請(qǐng)先錄音!") return; } this.innerAudioContext.src = this.data.src; this.innerAudioContext.play() }, onLoad: function (options) { var that = this; recorderManager.onError(function () { that.tip("錄音失敗!") }); recorderManager.onStop(function (res) { that.setData({ src: res.tempFilePath }) console.log(res.tempFilePath) that.tip("錄音完成!") }); this.innerAudioContext = wx.createInnerAudioContext(); this.innerAudioContext.onError((res) => { that.tip("播放錄音失?。?) }) } })
java后臺(tái)接收
package com.azor.controller; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import com.azor.utils.HttpAPIService; import ch.qos.logback.classic.Logger; import net.sf.json.JSONObject; @RestController @RequestMapping("/base_voice") public class BaseController { private static final Logger logger = (Logger) LoggerFactory.getLogger(BaseController.class); private static String lineSeparator = System.getProperty("line.separator"); @Autowired protected Environment env; @Autowired protected HttpAPIService httpAPIService; /** 上傳文件保存路徑 */ private final String FILE_SAVE_PATH = "D:/photo/jac_hr_miniprogram_file/"; /** 主業(yè)務(wù)數(shù)據(jù)Map */ protected Map<String, Object> dataMap = new HashMap<>(); /** HTTP POST 請(qǐng)求Map */ protected Map<String, Object> postMap = new HashMap<>(); @RequestMapping("/file_upload") public void saveFile(HttpServletRequest request, String url) throws Exception { logger.info("文件上傳開(kāi)始" + lineSeparator); // 1.獲取從前臺(tái)傳過(guò)來(lái)得圖片 MultipartHttpServletRequest req = (MultipartHttpServletRequest) request; MultipartFile multipartFile = req.getFile("file"); // 2.獲得文件擴(kuò)展名 String extOfFile = getExtOfFile(multipartFile); // 3.保存到本地 BufferedOutputStream bos = null; String filename = null; try { File dir = new File(file_save_path); if (!dir.exists()) {// 判斷文件目錄是否存在 dir.mkdirs(); } filename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + (int) (Math.random() * 1000) + "." + extOfFile; bos = new BufferedOutputStream(new FileOutputStream(file_save_path + filename)); bos.write(multipartFile.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public String getExtOfFile(MultipartFile multipartFile) { // 獲取文件的 名稱.擴(kuò)展名 String oldName = multipartFile.getOriginalFilename(); String extensionName = ""; // 獲取原來(lái)的擴(kuò)展名 if ((oldName != null) && (oldName.length() > 0)) { int dot = oldName.lastIndexOf('.'); if ((dot > -1) && (dot < (oldName.length() - 1))) { extensionName = oldName.substring(dot+1); } } return extensionName; } }
效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 小程序?qū)崿F(xiàn)錄音功能
- 微信小程序錄音實(shí)現(xiàn)功能并上傳(使用node解析接收)
- 微信小程序?qū)崿F(xiàn)錄音功能
- 小程序?qū)崿F(xiàn)按下錄音松開(kāi)識(shí)別語(yǔ)音
- 小程序?qū)崿F(xiàn)錄音上傳功能
- 微信小程序?qū)崿F(xiàn)錄音時(shí)的麥克風(fēng)動(dòng)畫(huà)效果實(shí)例
- 微信小程序錄音與播放錄音功能
- 微信小程序開(kāi)發(fā)之錄音機(jī) 音頻播放 動(dòng)畫(huà)實(shí)例 (真機(jī)可用)
- 微信小程序-圖片、錄音、音頻播放、音樂(lè)播放、視頻、文件代碼實(shí)例
- 小程序錄音功能實(shí)現(xiàn)
相關(guān)文章
JavaScript讓網(wǎng)頁(yè)出現(xiàn)漸隱漸顯背景顏色的方法
這篇文章主要介紹了JavaScript讓網(wǎng)頁(yè)出現(xiàn)漸隱漸顯背景顏色的方法,涉及javascript操作樣式的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04詳解如何編寫(xiě)一個(gè)Typescript的類型聲明文件
我們知道TypeScript根據(jù)類型聲明進(jìn)行類型檢查,但有些情況可能沒(méi)有類型聲明,這個(gè)時(shí)候就需要我們自己寫(xiě)一個(gè),下面小編就來(lái)和大家聊聊如果寫(xiě)一個(gè)Typescript的類型聲明文件呢2023-06-06javascript 原型模式實(shí)現(xiàn)OOP的再研究
目前網(wǎng)絡(luò)上有關(guān)javascript實(shí)現(xiàn)OOP模式的方案基本上都是prototype模式,一般性的示例代碼如下2009-04-04javascript通過(guò)navigator.userAgent識(shí)別各種瀏覽器
識(shí)別各種瀏覽器的實(shí)現(xiàn)原理是根據(jù)navigator.userAgent返回值識(shí)別,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-10-10深入聊聊Array的sort方法的使用技巧.詳細(xì)點(diǎn)評(píng)protype.js中的sortBy方法
深入聊聊Array的sort方法的使用技巧.詳細(xì)點(diǎn)評(píng)protype.js中的sortBy方法...2007-04-04基于JavaScript短信驗(yàn)證碼如何實(shí)現(xiàn)
我們?cè)谑褂靡苿?dòng)、電信等運(yùn)營(yíng)商網(wǎng)上營(yíng)業(yè)廳的時(shí)候,為確保業(yè)務(wù)的完整和正確性,經(jīng)常會(huì)需要用到短信的驗(yàn)證碼。最近因?yàn)槟呈I(yè)務(wù)需要,也做了個(gè)類似的功能2016-01-01最全的JavaScript開(kāi)發(fā)工具列表 總有一款適合你
最全的JavaScript開(kāi)發(fā)工具列表分享給你,總有一款適合你!2017-06-06?JavaScript?數(shù)據(jù)結(jié)構(gòu)之散列表的創(chuàng)建(2)
這篇文章主要介紹了?JavaScript?數(shù)據(jù)結(jié)構(gòu)之散列表的創(chuàng)建,主要看如何處理散列值沖突的問(wèn)題,并實(shí)現(xiàn)更完美的散列表。下文詳細(xì)介紹需要的小伙伴可以參考一下2022-04-04