欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > 微信小程序--录制音频,播放音频

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:740

高端HTML5响应式企业通用网

2020-05-06   浏览:521

html5响应式外贸网站英文版

2020-05-08   浏览:510

HTML5自适应律师工作室类网

2020-04-04   浏览:504

HTML5影视传媒文化公司类网

2020-05-12   浏览:501

微信小程序--录制音频,播放音频

发布时间:2020-10-30  

1.在pages创建一个main文件夹 
2.在main文件夹下创建一个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 是网络请求,可能会在 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() {

// 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问

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.新建一个main.wxml文件  添加代码如下

 

<view>

<button bindtap='getlocat'>测试</button>

<button bindtap='strop'>测sss试</button>

<button bindtap="play" >播放录音</button>

本文标签

: