微信小步伐常用API练习 - 灌音机小步伐开辟笔记
目录
灌音机
预备工作
创建项目
配置导航栏
加载图片资源
创建模块脚本
灌音机页面布局
灌音机页面样式
灌音机脚本控制
功能截图
总结
灌音机
灌音机是生活中的常用工具,可以记录声音和播放声音。它可以在开会的时候记录说话人的声音,也可以在生活中留下一段优美歌声。本案例将对“灌音机”微信小步伐的开辟进行详细解说。
灌音机”微信小步伐页面分为顶部区域和按钮控制区域。其中,顶部区域展示灌音时长,按钮控制区域中从左到右的3个按钮分别是“播放灌音”按钮、“开始/暂停灌音”按钮和“停止灌音”按钮,这3个按钮分别实现了播放灌音、开始或暂停灌音、停止灌音的功能。
预备工作
在开辟本案例前,需要先完成一些预备工作,主要包罗创建项目、配置导航栏和复制素材,
详细步骤如下。
创建项目
在微信开辟者工具中创建一个新的项目,项目名称为“灌音机”,模板选择“不使用模板”。
配置导航栏
在pages/index/index.json文件中配置页面导航栏,详细代码如下:
- {
- "usingComponents": {
- "navigation-bar": "/components/navigation-bar/navigation-bar"
- },
- "navigationBarTitleText": "录音机"
- }
复制代码
加载图片资源
创建images文件夹,放入三个图片,分别为:播放、开始录制、竣事的功能按钮显示icon。
如下图:
创建模块脚本
创建utils文件夹,并创建timer.js封装一些对灌音Api操作方法,用以在灌音机中调用:
包含开始、竣事灌音,更新灌音的时间等。
项目结构如下:
详细代码如下:
- module.exports = {
- message: 'timer',
- recorderManager:wx.getRecorderManager(),// 获取录音管理器对象
- options: {
- duration: 60000, // 最长录音时间为60秒
- sampleRate: 44100, // 采样率
- numberOfChannels: 1, // 单声道
- encodeBitRate: 192000, // 编码码率
- format: 'mp3' // 录音文件格式
- },
- // 开始录音
- start() {
- this.recorderManager.start(this.options)
- },
- // 暂停录音
- pause() {
- this.recorderManager.pause(this.options)
- },
- // 停止录音
- stop(){
- this.recorderManager.stop(this.options)
- },
- // 时间格式转化
- getTime(seconds) {
- var hours = Math.floor(seconds / 3600);
- var minutes = Math.floor((seconds % 3600) / 60);
- var secods = seconds % 60;
- secods = secods >= 10 ? secods : "0" + secods
- if (minutes > 0) {
- minutes = minutes >= 10 ? minutes : "0" + minutes;
- } else {
- minutes = "00";
- }
- if (hours > 0) {
- hours = hours >= 10 ? hours : '0' + hours;
- } else {
- hours = "00";
- }
- return hours + ":" + minutes + ":" + secods;
- }
- }
复制代码
灌音机页面布局
灌音机的页面布局在pages/index/index.wxml中编写。主要分为两个部分:上面显示标题和灌音时间,下方显示三个功能按钮,从左到右分别为:播放、开始录制、竣事录制。
详细代码如下:
- <!--index.wxml-->
- <navigation-bar title="录音机" back="{{false}}" color="black" background="#FFF"></navigation-bar>
- <view class="container">
- <view class="top">
- <view class="top-title">录音机</view>
- <view class="top-time">{{ time }}</view>
- </view>
- <view class="control">
- <view class="btn btn-play" bindtap="play" hover-class="btn-hover" hover-stay-time="50">
- <image src="/images/play.png" mode=""/>
- </view>
- <view class="btn btn-rec {{ state === 1 ? 'btn-rec-pause' : 'btn-rec-normal' }}" bindtap="rec" hover-class="btn-hover" hover-stay-time="50">
- <image src="/images/center.png" mode=""/>
- </view>
- <view class="btn btn-stop" bindtap="stop" hover-class="btn-hover" hover-stay-time="50">
- <image src="/images/stop.png" mode=""/>
- </view>
- </view>
- </view>
复制代码
灌音机页面样式
对灌音机页面样式的设置在pages/index/index.wxss中编写,详细代码如下:
- /**index.wxss**/
- .top {
- margin: 0 auto;
- text-align: center;
- }
- .top .top-title {
- font-size: 30px;
- }
- .top .top-time {
- margin-top:5vh;
- font-size: 60px;
- }
- .control {
- margin-top:10vh;
- display: flex;
- flex-direction: row;
- }
- .btn {
- margin: 0 10vw;
- }
- .btn-play image{
- width:50px;
- height:50px;
- border-radius: 20px;
- margin-top:30px;
- }
- .btn-rec image{
- width:100px;
- height:100px;
- border-radius: 30px;
- }
- .btn-stop image{
- width:50px;
- height:50px;
- border-radius: 20px;
- margin-top:30px;
- }
复制代码
灌音机脚本控制
对灌音机功能操作的脚本控制在pages/index/index.js中编写。主要内容为:设置初始变量,开始录制、暂停、停止及灌音文件播放脚本。
详细代码如下:
- // index.js
- const timer = require('../../utils/timer.js')
- // 音频对象
- var audioCtx = wx.createInnerAudioContext()
- // 初始化录音功能
- var rec = wx.getRecorderManager()
- var tempFilePath = null
- rec.onStop(res => {
- tempFilePath = res.tempFilePath
- console.log('录音成功:' + tempFilePath)
- })
- Page({
- data: {
- time: '00:00:00', // 录音时长
- state: 0, // 录音状态,0表示停止,1表示开始,2表示暂停
- seconds: 0, // 秒数
- },
- // 实现开始或暂停录音
- rec: function () {
- var that = this;
- switch (this.data.state) {
- case 0:
- rec.start()
- // 开始计时
- this.timer = setInterval(function () {
- that.setData({
- seconds: that.data.seconds + 1,
- time: timer.getTime(that.data.seconds)
- })
- }, 1000);
- timer.start()
- this.setData({
- time: '00:00:00',
- state: 1
- })
- break
- case 1:
- rec.pause()
- timer.pause()
- this.setData({
- state: 2
- })
- break
- case 2:
- rec.resume()
- timer.start()
- this.setData({
- state: 1
- })
- break
- }
- },
- // 停止录音
- stop: function () {
- console.log("停止")
- var onStopCallBack = null
- rec.onStop(res => {
- tempFilePath = res.tempFilePath
- console.log('录音成功:' + tempFilePath)
- onStopCallBack && onStopCallBack(tempFilePath)
- })
- this.setData({
- time: '00:00:00',
- state: 0,
- seconds:0
- });
- timer.stop()
- // 清除定时器
- clearInterval(this.timer)
- },
- // 播放录音
- play: function () {
- console.log("播放录音")
- if (this.data.state > 0) {
- // 第1种情况,录音尚未完成
- onStopCallBack = tempFilePath => {
- onStopCallBack = null
- audioCtx.src = tempFilePath
- audioCtx.play()
- this.setData({
- time: '播放录音'
- })
- }
- this.stop()
- } else if (tempFilePath) {
- // 第2种情况,录音已完成
- audioCtx.src = tempFilePath
- audioCtx.play()
- this.setData({
- time: '播放录音'
- })
- } else {
- // 第3种情况,尚未录音
- this.setData({
- time: '暂无录音'
- })
- }
- }
- })
复制代码 功能截图
总结
微信小步伐常用API练习 - 灌音机小步伐开辟笔记
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |