微信小程序录音与播放录音和上传功能

论坛 期权论坛 脚本     
匿名技术用户   2020-12-29 14:54   713   0

//index.js From:www.uzhanbao.com

//获取应用实例

const app = getApp()

const recorderManager = wx.getRecorderManager()

Page({

data: {

record_tmpfile: '', // 最近一次录音的临时文件路径;

record_ms: '', // 录音的毫秒数

uploaded_url: '', // 已上传文件的路径;

userInfo: {}

},

//事件处理函数

bindViewTap: function() {

wx.navigateTo({

url: '../logs/logs'

})

},

onLoad: function () {

},

// 录音功能:

handleRecordStart: function (e) {

const that = this;

const timeStart = Date.now();

this.recorderManager = wx.getRecorderManager();

this.recorderManager.onError(function (res1) {

// 录音失败的回调处理

console.log(res1);

if (res1.errMsg.indexOf('Not Found') >= 0) {

wx.showModal({

title: '无法录音',

content: '您已经拒绝访问麦克风,无法使用录音功能,如需使用,请删除此小程序,并重新搜索打开',

showCancel: false,

});

} else {

wx.showModal({

title: '有错误',

content: res1.errMsg,

showCancel: false,

});

}

});

this.recorderManager.onStop(function (res) {

// 停止录音之后,把录取到的音频放在res.tempFilePath

that.setData({

src: res.tempFilePath,

record_tmpfile: res.tempFilePath,

record_ms: Date.now() - timeStart,

})

console.log(res.tempFilePath)

});

this.recorderManager.start({

format: 'mp3' // 如果录制acc类型音频则改成aac

});

setTimeout(function () {

//结束录音

//wx.stopRecord()

this.recorderManager.stop()

}, 60000);

},

// 停止录音:

handleRecordEnd: function (e) {

//wx.stopRecord()

this.recorderManager.stop()

},

// 播放录音:

handlePlayVoice: function () {

console.log('start play voice');

this.innerAudioContext = wx.createInnerAudioContext();

this.innerAudioContext.onError((res) => {

// 播放音频失败的回调

})

this.innerAudioContext.src = this.data.src; // 这里可以是录音的临时路径

this.innerAudioContext.play()

//wx.playVoice({

//filePath: this.data.record_tmpfile,

//})

},

// 上传录音

handleUploadVoice: function () {

const { record_tmpfile, record_ms } = this.data;

if (record_ms=="")

{

wx.showToast({

title: '录音不能为空',

icon: 'success',

duration: 2000

})

}

else

{

wx.showLoading({ title: '上传中' });

const that = this;

wx.uploadFile({

url: 'https://www.uzhanbao.com/UploadVoice.ashx',

filePath: record_tmpfile,

name: 'file',

formData: {

audio_ms: record_ms,

},

success: function (res) {

wx.hideLoading();

var data = res.data

console.log('upload_res: ', res);

const data2 = JSON.parse(res.data);

console.log('upload res data2: ', data2);

//do something

let toastTitle = '上传成功';

if (data2.states == "0") {

toastTitle = data2.message;

} else {

that.setData({ uploaded_url: data2.FileName });

const innerAudioContext = wx.createInnerAudioContext()

innerAudioContext.autoplay = true

innerAudioContext.src = data2.url

innerAudioContext.onPlay(() => {

console.log('开始播放')

})

innerAudioContext.onError((res) => {

console.log(res.errMsg)

console.log(res.errCode)

})

}

setTimeout(function () {

wx.showToast({ title: toastTitle });

}, 500);

},

fail: function (res) {

wx.hideLoading();

}

})

}

},

getUserInfo: function(e) {

console.log(e)

app.globalData.userInfo = e.detail.userInfo

this.setData({

userInfo: e.detail.userInfo,

hasUserInfo: true

})

}

})

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP