本文實(shí)例為大家分享了微信小程序錄音功能的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)是專業(yè)的武安網(wǎng)站建設(shè)公司,武安接單;提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行武安網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
release.wxml
<!--pages/index/release/release.wxml--> <scroll-view> <view wx:if="{{voices}}" class="common-list" > <block wx:for="{{voices}}"> <view class="board"> <view class="cell"> <view class="cell-bd" data-key="{{item.filePath}}" bindtap="gotoPlay"> <view class="date">存儲(chǔ)路徑:{{item.filePath}}</view> <view class="date">存儲(chǔ)時(shí)間:{{item.createTime}}</view> <view class="date">音頻大小:{{item.size}}KB</view> </view> </view> </view> </block> </view> </scroll-view> <view wx:if="{{isSpeaking}}" class="microphone"> <image class="sound-style" src="/images/speak.png"></image> <image wx:if="{{j==2}}" class="sound-style" src="/images/speak.png"></image> <image wx:if="{{j==3}}" class="sound-style" src="/images/speak.png"></image> <image wx:if="{{j==4}}" class="sound-style" src="/images/speak.png"></image> <image wx:if="{{j==5}}" class="sound-style" src="/images/speak_end.png"></image> </view> <view class="record-style"> <button class="btn-style" bindtouchstart="touchdown" bindtouchend="touchup">按住 錄音</button> </view>
release.wxss
/* pages/index/release/release.wxss */ .microphone{ position:fixed; left: 250rpx; bottom: 0; height: 240rpx; width: 240rpx; border-radius: 20rpx; margin: 50% auto; background: #26A5FF; } .item-style{ margin-top: 30rpx; margin-bottom: 30rpx; } .text-style{ text-align: center; } .record-style{ position: fixed; bottom: 0; left: 0; height: 120rpx; width: 100%; } .btn-style{ margin-left: 30rpx; margin-right: 30rpx; } .sound-style{ position: absolute; width: 74rpx; height:150rpx; margin-top: 45rpx; margin-left: 83rpx; } .board { overflow: hidden; border-bottom: 2rpx solid #26A5FF; } /*列布局*/ .cell{ display: flex; margin: 20rpx; } .cell-hd{ margin-left: 10rpx; color: #885A38; } .cell .cell-bd{ flex:1; position: relative; } /**只顯示一行*/ .date{ font-size: 30rpx; text-overflow: ellipsis; white-space:nowrap; overflow:hidden; }
release.js
// pages/index/release/release.js var app = getApp() Page({ data: { j: 1,//幀動(dòng)畫(huà)初始圖片 isSpeaking: false,//是否正在說(shuō)話 voices: [],//音頻數(shù)組 }, onLoad: function () { }, //手指按下 touchdown: function () { console.log("手指按下了...") console.log("new date : " + new Date) var _this = this; speaking.call(this); this.setData({ isSpeaking: true }) //開(kāi)始錄音 wx.startRecord({ success: function (res) { //臨時(shí)路徑,下次進(jìn)入小程序時(shí)無(wú)法正常使用 var tempFilePath = res.tempFilePath console.log("tempFilePath: " + tempFilePath) //持久保存 wx.saveFile({ tempFilePath: tempFilePath, success: function (res) { //持久路徑 //本地文件存儲(chǔ)的大小限制為 100M var savedFilePath = res.savedFilePath console.log("savedFilePath: " + savedFilePath) } }) wx.showToast({ title: '恭喜!錄音成功', icon: 'success', duration: 1000 }) //獲取錄音音頻列表 wx.getSavedFileList({ success: function (res) { var voices = []; for (var i = 0; i < res.fileList.length; i++) { //格式化時(shí)間 var createTime = new Date(res.fileList[i].createTime) //將音頻大小B轉(zhuǎn)為KB var size = (res.fileList[i].size / 1024).toFixed(2); var voice = { filePath: res.fileList[i].filePath, createTime: createTime, size: size }; console.log("文件路徑: " + res.fileList[i].filePath) console.log("文件時(shí)間: " + createTime) console.log("文件大小: " + size) voices = voices.concat(voice); } _this.setData({ voices: voices }) } }) }, fail: function (res) { //錄音失敗 wx.showModal({ title: '提示', content: '錄音的姿勢(shì)不對(duì)!', showCancel: false, success: function (res) { if (res.confirm) { console.log('用戶點(diǎn)擊確定') return } } }) } }) }, //手指抬起 touchup: function () { console.log("手指抬起了...") this.setData({ isSpeaking: false, }) clearInterval(this.timer) wx.stopRecord() }, //點(diǎn)擊播放錄音 gotoPlay: function (e) { var filePath = e.currentTarget.dataset.key; //點(diǎn)擊開(kāi)始播放 wx.showToast({ title: '開(kāi)始播放', icon: 'success', duration: 1000 }) wx.playVoice({ filePath: filePath, success: function () { wx.showToast({ title: '播放結(jié)束', icon: 'success', duration: 1000 }) } }) } }) //麥克風(fēng)幀動(dòng)畫(huà) function speaking() { var _this = this; //話筒幀動(dòng)畫(huà) var i = 1; this.timer = setInterval(function () { i++; i = i % 5; _this.setData({ j: i }) }, 200); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前標(biāo)題:微信小程序?qū)崿F(xiàn)錄音功能
本文鏈接:http://chinadenli.net/article44/joeghe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、云服務(wù)器、建站公司、外貿(mào)建站、面包屑導(dǎo)航、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)