軟體創新實驗室:微信小程式開發——音訊錄製與播放
阿新 • • 發佈:2020-12-23
文章目錄
宣告
1)本次小專案程式碼來源於楊楊學姐實驗室授課內容。
2)博主是萌新上路,文中如有不當之處,請各位大佬指出,共同進步,謝謝。
主體
這個微信小程式較為簡單,適用於新手入門,話不多說,上程式碼,
// pages/index/index.js
var tempFilePath = "" //用於儲存臨時音訊檔案地址
Page({
/**
* 錄製音訊
*/
record:function(){
console.log("錄製音訊開始")
//錄製音訊
wx. startRecord({
success: (res) => {
tempFilePath = res.tempFilePath
},
})
//自動停止錄製,5000ms
// setTimeout(function(){
// console.log("停止錄製事件")
// wx.stopRecord({
// success: (res) => {},
// })
// },5000)
},
/**
* 結束錄製
*/
stop:function() {
console.log("結束錄製事件")
wx.stopRecord({
success: (res) => {},
})
},
/**
* 暫停錄製
*/
pauseVoice:function(){
console.log("錄製暫停")
wx.pauseVoice({
success: (res) => {},
})
},
/**
* 播放錄製好的音訊
*/
playRecord:function(){
wx. playVoice({
filePath: tempFilePath,
})
}
})
可以發現主要就是四個函式,
wx.startRecord
,
wx.stopRecord
,
wx.pauseVoice
,
wx.playVoice
,
通過這四個函式,實現了錄製,結束,暫停以及播放錄音的功能,
很簡單,很有趣的微信小程式!