<view class='voiceTap' wx:if="{{status == 0}}">點擊錄音</view>
<view class='voiceTap' wx:if="{{status != 0}}">
<image src='/images/6006.png'></image>
<view class='voicelong'>{{voicetime}}</view>
</view>
<view class='voiceNavi' bindtap='workaudio'>
<view class='inner1'>
<view class='inner2' wx:if="{{status == 0}}"></view>
<view class='cube' wx:if="{{status == 1 || status == 3}}"></view>
<view class='triangle' wx:if="{{status == 2}}"></view>
</view>
</view>
page({
data:{
audio: 1,
status: 0,
voicetime: '0:00',
src: '',
},
onLoad: function (options){
var that = this;
this.recorderManager = wx.getRecorderManager();
this.recorderManager.onError(function () {
// 錄音失敗的回調處理
console.log(111)
});
this.recorderManager.onStop(function (res){
that.setData({
src: res.tempFilePath
})
// console.log(that.data.src)
// 該處存儲的“src”就是語音文件 ;
}
},
// 點擊開始/結束錄音
workaudio: function (event) {
var that = this;
var status = that.data.status;
var sta, voicetime, interval, listen;
if (status == 0) {//剛點擊進入
sta = 1;
//開始錄音
this.recorderManager.start({
duration: 60000,
format: 'mp3' // 如果錄制acc類型音頻則改成aac
});
var runtime = 0;
interval = setInterval(function () {
runtime++;
if (runtime < 10) {
voicetime = '0:0' + runtime;
} else {
voicetime = '0:' + runtime;
}
if (runtime == 60) {
clearInterval(interval);
this.recorderManager.stop();
that.setData({ status: 2 })
}
that.setData({ voicetime, interval, runtime })
}, 1000)
} else if (status == 1) {//錄音結束
sta = 2;
this.recorderManager.stop();
clearInterval(that.data.interval);
} else if (status == 2) {//播放
sta = 3;
var long = (that.data.runtime) - 1
listen = setInterval(function () {
long--;
console.log(long)
if (long <= 0) {
that.setData({ status: 2 });
clearInterval(that.data.listen);
}
that.setData({ listen });
}, 1000)
that.playVoice();
console.log(sta);
} else if (status == 3) {
sta = 2;
that.innerAudioContext.pause()
}
that.setData({ status: sta })
},
// 播放錄音
playVoice: function (e) {
this.innerAudioContext = wx.createInnerAudioContext();
this.innerAudioContext.onError((res) => {
// 播放音頻失敗的回調
})
this.innerAudioContext.src = this.data.src; // 這里可以是錄音的臨時路徑
this.innerAudioContext.play()
},
})