This commit is contained in:
2021-04-07 15:18:17 +08:00
parent dfdf73fdfc
commit ab7a7cf62f
88 changed files with 2974 additions and 4 deletions

View File

@@ -0,0 +1,63 @@
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();
}
},
})