/**
* @file test for getRecorderManager api
* @author haoyang
*/
/* globals Page, swan */
/* eslint-disable new-cap */
Page({
data: {
title: 'start/stopRecord、play/stopVoice',
isend: false, // 是否停止录制
isrecord: false, // 是否正在录制
isplay: false, // 是否正在播放
startTime: '00:00:00',
endTime: '',
num: 0
},
onLoad() {
let self = this;
self.options = {
duration: 60000,
sampleRate: 44100,
numberOfChannels: 1,
encodeBitRate: 96000,
format: 'aac'
};
self.recorderManager = swan.getRecorderManager(); // 创建录音实例
self.syncTimer;
},
getRecord() {
// 开始/结束录音
let self = this;
let isrecord = self.getData('isrecord');
if (!isrecord) {
self.setData('isrecord', true);
self.recorderManager.start(self.options);
self.recorderManager.onStart(function () {
self.timeRecoder(0);
});
} else {
let endTime = self.getData('startTime');
self.setData({
'isrecord': false,
'isend': true,
'endTime': endTime,
'startTime': '00:00:00'
});
self.recorderManager.stop();
self.syncTimer && clearInterval(self.syncTimer);
self.recorderManager.onStop(function (res) {
const {tempFilePath} = res;
self.tempFilePath = tempFilePath;
});
}
},
playRecord() {
// 创建音频实例,播放/暂停音频
let self = this;
let isplay = !self.getData('isplay');
let num = self.getData('num');
self.setData('isplay', isplay);
if (self.innerAudioContext) {
self.innerAudioContext.destroy();
}
self.innerAudioContext = swan.createInnerAudioContext();
self.innerAudioContext.src = self.tempFilePath;
if (isplay) {
self.innerAudioContext.play();
self.timeRecoder(0);
self.innerAudioContext.onEnded(function () {
self.setData({
'isplay': false,
'startTime': '00:00:00'
});
swan.showToast({
'title': '录音自然结束'
});
self.syncTimer && clearInterval(self.syncTimer);
});
} else {
self.innerAudioContext.pause();
this.syncTimer && clearInterval(this.syncTimer);
}
},
deleteRecord() {
// 停止播放音频,摧毁语音实例
this.setData({
'isend': false,
'isplay': false,
'isrecord': false,
'startTime': '00:00:00'
});
this.isFirstPlay = true;
this.syncTimer && clearInterval(this.syncTimer);
if (this.innerAudioContext) {
this.innerAudioContext.stop();
this.innerAudioContext.destroy();
}
},
onHide() {
// 监听页面隐藏时重置计时器及摧毁语音实例
let endStatus = this.getData('isend');
if (!endStatus) {
// 未结束录音前隐藏小程序,回退到录音初始状态
this.setData({
'isrecord': false,
'startTime': '00:00:00'
});
this.syncTimer && clearInterval(this.syncTimer);
this.recorderManager.stop();
} else {
// 录音结束隐藏小程序,回退到录音播放状态
this.setData({
'isplay': false,
'startTime': '00:00:00'
});
this.syncTimer && clearInterval(this.syncTimer);
if (this.innerAudioContext) {
this.innerAudioContext.pause();
}
}
},
onUnload() {
// 监听页面卸载时摧毁语音实例
if (this.innerAudioContext) {
this.innerAudioContext.stop();
this.innerAudioContext.destroy();
}
},
formatTime(name) {
name = name.toString();
return name.length !== 1 ? name : '0' + name;
},
timeRecoder(init) {
// 录音计时器
let self = this;
let hour;
let minute;
let second;
clearInterval(self.syncTimer);
self.syncTimer = setInterval(function () {
init++;
if (init < 3600) {
minute = self.formatTime(parseInt(init / 60, 10));
second = self.formatTime(parseInt(init % 60, 10));
hour = '00';
}
else {
hour = self.formatTime(parseInt(init / 3600, 10));
minute = self.formatTime(parseInt((init - 3600) / 60, 10));
second = self.formatTime(parseInt((init - 3600) % 60, 10));
}
self.setData({
'startTime': hour + ':' + minute + ':' + second,
'num': init
});
},
1000);
}
});
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
百度智能小程序官方Demo (262个子文件)
app.css 4KB
getCompass.css 3KB
getRecorderManager.css 1KB
component.css 1KB
api.css 1KB
getGravity.css 1KB
textarea.css 1KB
chooseVideo.css 1KB
swiper.css 1KB
text.css 1KB
label.css 931B
button.css 905B
view.css 710B
getUserInfo.css 693B
icon.css 655B
scroll-view.css 608B
checkbox.css 563B
getSystemInfo.css 540B
live-player.css 483B
switch.css 475B
image.css 411B
makePhoneCall.css 409B
picker.css 388B
radio.css 380B
clipboardData.css 313B
input.css 309B
detail.css 300B
pullDownRefresh.css 288B
getNetworkType.css 286B
getLocation.css 283B
onNetworkStatusChange.css 247B
canvas.css 237B
showActionSheet.css 201B
modal.css 150B
authorize.css 149B
video.css 143B
login.css 132B
progress.css 125B
api-image.css 109B
toast.css 109B
navigationBarLoading.css 109B
navigateTo.css 109B
file.css 109B
storage.css 106B
setNavigationBarTitle.css 103B
navigator.css 100B
slider.css 89B
uploadFile.css 65B
downloadFile.css 65B
request.css 65B
openSetting.css 65B
openShare.css 65B
map.css 58B
audio.css 37B
WebSocket.css 35B
payment.css 1B
getRecorderManager.js 5KB
api.js 4KB
component.js 3KB
live-player.js 3KB
getGravity.js 2KB
storage.js 2KB
file.js 2KB
video.js 2KB
swiper.js 1KB
getSystemInfo.js 1KB
clipboardData.js 1KB
getLocation.js 1KB
downloadFile.js 1KB
getCompass.js 1KB
payment.js 1KB
chooseVideo.js 1KB
canvas.js 1KB
picker.js 1KB
audio.js 1KB
getUserInfo.js 1004B
uploadFile.js 974B
button.js 946B
request.js 918B
scroll-view.js 818B
makePhoneCall.js 815B
login.js 814B
api-image.js 808B
authorize.js 798B
toast.js 748B
WebSocket.js 704B
radio.js 650B
checkbox.js 643B
camera.js 621B
modal.js 618B
openShare.js 603B
getNetworkType.js 589B
setNavigationBarTitle.js 578B
text.js 550B
icon.js 527B
onNetworkStatusChange.js 508B
navigateTo.js 464B
showActionSheet.js 459B
textarea.js 447B
label.js 374B
共 262 条
- 1
- 2
- 3
资源评论
- 6大少2019-09-27百度太垃圾了把 官网上不放DEMO 百度搜小程序搜到一堆 什么微。。。。 服劲
- qq_355653422018-11-29不错啊,试试看看啊
- qq_218905832019-03-26不错啊,试试看看啊
- qq_157810372019-03-07不错啊,试试看看啊
- Anlige2019-05-20不错啊,试试看看啊
一源-Spring
- 粉丝: 3
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功