Files
2021-04-08 11:46:50 +08:00

64 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Component({
options: {
addGlobalClass: true,
},
properties: {
src: {
type: String,
value: ""
},
duration: {
type: Number,
value: 0
}
},
data: {
width: "",
play: false,
finalDuration: "",
audioContext: null
},
methods: {
playAudio() {
// 播放时才创建audioContext,播放完毕销毁
var self = this;
this.setData({
audioContext: wx.createInnerAudioContext()
});
this.data.audioContext.src = this.data.src;
this.switchAudioState();
setTimeout(() => {
self.switchAudioState();
self.data.audioContext.destroy();
}, self.data.finalDuration*1000);
this.data.audioContext.play();
this.data.audioContext.onPlay(()=>{
console.log("正在播放......");
});
this.data.audioContext.onError((res) => {
console.log("audio error:",res)
});
},
switchAudioState(){
this.setData({
play: !this.data.play
});
},
},
attached: function() {
// 在组件实例进入页面节点树时执行
this.setData({
width: Math.ceil(this.data.duration)*7+80,
finalDuration: Math.ceil(this.data.duration),
});
},
detached: function() {
// 在组件实例被从页面节点树移除时执行
// 语音还在播放时退出该界面时audioContext还没有被销毁因此调用该方法清空audioContext
if(this.data.audioContext != null){
this.data.audioContext.destroy();
}
},
})