1.在pages創(chuàng)建一個(gè)main文件夾
2.在main文件夾下創(chuàng)建一個(gè)miain.js文件。
添加代碼:
-
const constant = require('../../utils/constant.js')
-
const app = getApp()
-
const recorderManager = wx.getRecorderManager()
-
const innerAudioContext = wx.createInnerAudioContext()
-
-
Page({
-
data:{
-
money:"0.00",
-
userInfo: {},
-
hasUserInfo: false,
-
canIUse: wx.canIUse('button.open-type.getUserInfo'),
-
},
-
onLoad: function () {
-
if (app.globalData.userInfo) {
-
this.setData({
-
userInfo: app.globalData.userInfo,
-
hasUserInfo: true
-
})
-
} else if (this.data.canIUse){
-
// 由于 getUserInfo 是網(wǎng)絡(luò)請(qǐng)求,可能會(huì)在 Page.onLoad 之后才返回
-
// 所以此處加入 callback 以防止這種情況
-
app.userInfoReadyCallback = res => {
-
-
this.setData({
-
userInfo: res.userInfo,
-
hasUserInfo: true
-
})
-
}
-
} else {
-
// 在沒有 open-type=getUserInfo 版本的兼容處理
-
wx.getUserInfo({
-
success: res => {
-
app.globalData.userInfo = res.userInfo
-
this.setData({
-
userInfo: res.userInfo,
-
hasUserInfo: true
-
})
-
}
-
})
-
}
-
},
-
getlocat:function(){
-
wx.authorize({
-
scope: 'scope.record',
-
success() {
-
// 用戶已經(jīng)同意小程序使用錄音功能,后續(xù)調(diào)用 wx.startRecord 接口不會(huì)彈窗詢問
-
const options = {
-
duration: 600000,
-
sampleRate: 44100,
-
numberOfChannels: 1,
-
encodeBitRate: 192000,
-
format: 'mp3',
-
frameSize: 50
-
}
-
-
-
recorderManager.start(options)
-
-
recorderManager.onStart(() => {
-
console.log('recorder start')
-
})
-
recorderManager.onError((res) => {
-
console.log(res);
-
})
-
}
-
})
-
// console.log(app)
-
},
-
-
strop:function(){
-
recorderManager.stop()
-
recorderManager.onStop((res) => {
-
console.log('recorder stop', res)
-
const { tempFilePath } = res
-
console.log(tempFilePath)
-
this.tempFilePath = tempFilePath
-
})
-
},
-
//播放聲音
-
play: function () {
-
innerAudioContext.autoplay = true
-
innerAudioContext.src = this.tempFilePath;
-
innerAudioContext.onPlay(() => {
-
console.log('開始播放')
-
})
-
innerAudioContext.onError((res) => {
-
console.log(res.errMsg)
-
console.log(res.errCode)
-
})
-
-
},
-
})
3.新建一個(gè)main.wxml文件 添加代碼如下
-
<view>
-
<button bindtap='getlocat'>測(cè)試</button>
-
<button bindtap='strop'>測(cè)sss試</button>
-
<button bindtap="play" >播放錄音</button>
-
</view>
|