微信小程序--录音
var app = getApp(),
$ = require("../../utils/util.js");
const recorderManager = wx.getRecorderManager();
var interval;
Page({
data: {
listen: false,
voicePath: "",
minute: 0,
second: 0,
timeText: "00:00"
},
onLoad: function(e) {},
//计时器
timer: function() {
let second = Number(this.data.second);
let minute = Number(this.data.minute);
let timeText = this.data.timeText;
second = second + 1;
if (second >= 60) {
second = 0;
minute = minute + 1;
}
second < 10 ? second = "0" + second : second;
minute < 10 ? minute = "0" + minute : minute;
timeText = minute + ":" + second;
this.setData({
second: second,
minute: minute,
timeText: timeText
})
},
//开始录音
startluyin: function() {
let that = this;
that.setData({
start: true,
});
interval = setInterval(function() {
that.timer();
}, 1000)
const options = {
duration: 600000, //指定录音的时长,单位 ms
sampleRate: 16000, //采样率
numberOfChannels: 1, //录音通道数
encodeBitRate: 96000, //编码码率
format: 'mp3', //音频格式,有效值 aac/mp3
frameSize: 50, //指定帧大小,单位 KB
}
//开始录音
recorderManager.start(options);
recorderManager.onStart(() => {
console.log('recorder start')
});
//错误回调
recorderManager.onError((res) => {
console.log(res);
})
},
//结束录音
endluyin: function() {
this.setData({
start: false
})
recorderManager.stop();
recorderManager.onStop((res) => {
this.tempFilePath = res.tempFilePath;
console.log('停止录音', res.tempFilePath)
const {
tempFilePath
} = res;
this.setData({
audioPath: res.tempFilePath
})
})
clearInterval(interval);
},
//删除录音
deleteluyin: function() {
let that = this;
wx.showModal({
title: '提示',
content: '您确定要删除此录音吗',
success: function(res) {
if (res.confirm) {
that.setData({
timeText: "00:00",
voicePath: "",
audioPath: "" })
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
},
//播放录音
playaudio: function() {
const innerAudioContext = wx.createInnerAudioContext();
this.setData({
listen: true
})
innerAudioContext.autoplay = true
innerAudioContext.src = this.tempFilePath
innerAudioContext.onPlay(() => {
console.log('开始播放')
})
//监听是否播放完毕
innerAudioContext.onEnded(() => {
console.log('播放结束')
this.setData({
listen: false
})
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
},
//上传录音
uploadluyin: function() {
let that = this;
$.AJAX(app.globalData.API_OSSUPLOAD + '?type=sp_twitter', "GET", {}, function(res) {
var oss = res.datas;
var audioPath = that.data.audioPath;
var voicePath = that.data.voicePath;
console.log(audioPath, "111")
if (audioPath) {
$.loading("上传中...", 10000)
oss.path = audioPath;
var imgtype = audioPath.substr(audioPath.length - 3);
var imgname = $.getrandom() + '/' + $.getrandom2() + '.' + imgtype;
oss.key = oss.key + imgname;
$.UPLOADOSS(oss.host, oss, function(res) {
voicePath = oss.host + "/" + oss.key;
that.setData({
voicePath: voicePath
})
$.hideloading();
wx.showToast({
title: '上传成功',
icon: 'success',
duration: 2000
})
wx.navigateBack({
delta: 1
})
let pages = getCurrentPages();
console.log(pages, "pages---")
let timeText = that.data.timeText;
pages[1].setData({
voice: voicePath,
audioflag: true,
timeText: timeText
})
})
} else {
wx.showModal({
title: '提示',
content: '请录音再上传',
showCancel: false,
confirmText: "知道了",
success: function(res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
})
}
})
}
})
<view class="container">
<view class='topbox'>
<view class="m-loading-box" bindtap='playaudio'>
<label class="{{listen?'u-loading':'u-noloading'}}"></label>
<image class="luyin1" src="https://cdn.quansuwangluo.com/shapan/luyin1.png"></image>
</view>
<view class='time'>{{timeText}}</view>
</view>
<view class='bottombox'>
<view class='opt opt_del' bindtap='deleteluyin'>
<view class='delwrap'>
<image src="https://cdn.quansuwangluo.com/shapan/del.png"></image>
</view>
<view>删除</view>
</view>
<view class='opt opt_luyin' bindtap='startluyin' wx:if="{{!start}}">
<view class='luyinwrap'>
<image src="https://cdn.quansuwangluo.com/shapan/pause.png"></image>
</view>
<view>点击录音</view>
</view>
<view class='opt opt_luyin' bindtap='endluyin' wx:else>
<view class='luyinwrap'>
<image src="https://cdn.quansuwangluo.com/shapan/nopause.png"></image>
</view>
<view>点击录音</view>
</view>
<view class='opt opt_listen' bindtap='uploadluyin'>
<view class='listenwrap'>
<image src="https://cdn.quansuwangluo.com/shapan/upload.png"></image>
</view>
<view>上传</view>
</view>
</view>
</view>
.topbox{
background-color: #FCF6ED;
font-size: 58rpx;
text-align: center;
position: relative;
padding: 225rpx 0;
}
.topbox .u-loading{
width: 240rpx;
height: 240rpx;
display: inline-block;
vertical-align: middle;
-webkit-animation: weuiLoading 1s steps(12, end) infinite;
animation: weuiLoading 1s steps(12, end) infinite;
background: transparent url('https://cdn.quansuwangluo.com/shapan/loading.png') no-repeat;
background-size: 100%;
}
.topbox .u-noloading{
width: 240rpx;
height: 240rpx;
display: inline-block;
vertical-align: middle;
background: transparent url('https://cdn.quansuwangluo.com/shapan/loading.png') no-repeat;
background-size: 100%;
}
.luyin1{
height: 60rpx;
width: 38rpx;
position: absolute;
left: 358rpx;
top:314rpx;
}
.topbox .time{
margin-top: 35rpx;
}
.bottombox{
background-color: #FEA22C;
display: flex;
align-items: center;
height: 400rpx;
color: #fff;
font-size: 34rpx;
padding: 0 90rpx;
text-align: center;
}
.opt_del,.opt_listen{
margin-top: 20rpx;
}
.opt_del .delwrap{
width: 120rpx;
height: 120rpx;
display: flex;
justify-content: center;
align-items: center;
border: 4rpx solid #fff;
border-radius: 50%;
margin-bottom: 20rpx;
}
.opt_del image{
width: 44rpx;
height: 58rpx;
}
.opt_luyin{
padding: 0 70rpx;
}
.opt_luyin .luyinwrap{
width: 180rpx;
height: 180rpx;
display: flex;
justify-content: center;
align-items: center;
background-color: #fff;
border-radius: 50%;
margin-bottom: 20rpx;
}
.opt_luyin image{
width: 60rpx;
height: 60rpx;
}
.opt_listen image{
width: 60rpx;
height: 54rpx;
}
.opt_listen .listenwrap{
width: 120rpx;
height: 120rpx;
display: flex;
justify-content: center;
align-items: center;
border: 4rpx solid #fff;
border-radius: 50%;
margin-bottom: 20rpx;
}
微信小程序--录音的更多相关文章
- 微信小程序录音实现
最近在写一个小程序的功能,需求是需要把用户的录音记录并上传到服务器,然后调用接口来实现播放功能. 翻阅不少资料都发现小程序对wx.startRecord()接口不在维护, 注意:1.6.0 版本开始, ...
- 微信小程序语音识别服务搭建全过程解析(https api开放,支持新接口mp3录音、老接口silk录音)
silk v3(或新录音接口mp3)录音转olami语音识别和语义处理的api服务(ubuntu16.04服务器上实现) 重要的写在前面 重要事项一: 所有相关更新,我优先更新到我个人博客中,其它地方 ...
- 微信小程序语音识别服务搭建全过程解析(项目开源在github)
silk v3录音转olami语音识别和语义处理的api服务(ubuntu16.04服务器上实现) ## 重要的写在前面 重要事项一: 目前本文中提到的API已支持微信小程序录音文件格式:silk v ...
- 微信小程序语音识别开发过程记录 微信小程序silk转mp3 silk转wav 以及ffmpeg使用
说说最近在开发微信小程序语音识别遇到的问题吧 最先使用微信小程序录音控件可以拿到silk格式,后来微信官方又支持mp3格式了 但是我们拿到这些格式以后,都还不能直接使用,做语音识别,因为目前百度的语音 ...
- 微信小程序silk格式转码成mp3格式
最近小程序项目需要录制语音并上传到服务器,经过查资料了解 目前微信小程序录音的文件后缀名是silk,因此需要转换. 经过查资料了解,参考一下的地址 https://github.com/kn007/s ...
- 语音识别功能_微信小程序代办清单任务
最近想给自己的代办清单任务微信小程序想加个语音识别识别功能,废话不多说,直接说重点,语音识别使用的是百度语音识别api,因为微信小程序的录音输入文件目前只能是mp3或aac 但是百度语音识别不支持这两 ...
- 微信小程序(有始有终,全部代码)开发--- 新增【录音】以及UI改进
开篇语 寒假发了一篇练手文章,不出意外地火了: <简年15: 微信小程序(有始有终,全部代码)开发---跑步App+音乐播放器 > 后来又发了BUG修复的版本,出乎意料的火了: 简年18: ...
- 微信小程序常见问题集合(长期更新)
最新更新: 新手跳坑系列:推荐阅读:<二十四>request:fail错误(含https解决方案)(真机预览问题 跳坑指南<七十>如何让微信小程序服务类目审核通过 跳坑六十九: ...
- 微信小程序简介
什么是微信小程序? 今年下半年的时候,微信推出了微信小程序,当然刚刚推出来的时候还是处于内测阶段,但是这并不影响这家伙的热度,也许这是一个新的时代的开启.但是什么是微信小程序呢?微信应用号是一个app ...
随机推荐
- CSS 不规则图形绘制
基础技能1 - 神奇的border 我们先来画一个长方形: .Rectangle { height: 100px; width: 200px; background: darkgray; border ...
- three.js的组合与合并,raycaster射线无法获取group
1.组合 创建一个组非常简单,在组中添加子元素的效果是,你可以对组进行移动.缩放和变形,而所有的子对象都会受到影响.使用组的时候,你依然可以引用.修改每一个单独的几何体.但是,使用raycaster射 ...
- 网页中HTML代码如何实现字体删除线效果
有的朋友在制作网站的时候,需要给字体制作删除线,例如:选择题,错误标识等!那么我们就需要用到了<s>这个标签写法如下 字体删除线: <s>这里是内容</s> 效果如 ...
- promise规范之部分总结
1. promise构造函数中的reject和resolve是微任务, 即先执行resolve后的代码,再执行之前通过then注册的代码 2. 对于状态已变更的promise来说,比如promiseA ...
- 前端反爬虫策略--font-face 猫眼数据爬取
1 .font-face定义了字符集,通过unicode去印射展示. 2 .font-face加载网络字体,我么可以自己创建一套字体,然后自定义一套字符映射关系表例如设置0xefab是映射字符1, ...
- Centos7之系统优化
优化条目: 修改ip地址.网关.主机名.DNS等 关闭selinux,清空iptables 添加普通用户并进行sudo授权管理 更新yum源及必要软件安装 定时自动更新服务器时间 精简开机自启动服务 ...
- java 多线程执行时长统计
ExecutorService——shutdown方法和awaitTermination方法 shutdown方法:平滑的关闭ExecutorService,当此方法被调用时,ExecutorServ ...
- 海伦去约会——kNN算法
下午于屋中闲居,于是翻开<机器学习实战>一书看了看“k-邻近算法”的内容,并学习了一位很厉害的博主Jack Cui的代码,自己照着码了一遍.在此感谢博主Jack Cui的知识分享. 一.k ...
- asp.net mvc5中使用Swagger 自动生成WebApi文档笔记
Swagger可以自动生成Api说明文档,并内置在线测试,通过NSwagStudio还可以自动生成Api客户端调用代码,以下为具体实现步骤 1.写一个简单的WebApi并加上注释 public cla ...
- include和require的区别
include与require除了在处理引入文件的方式不同外,最大的区别就是:include在引入不存文件时产生一个警告且脚本还会继续执行,require则会导致一个致命性错误且脚本停止执行. inc ...