audio.js 3.6 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
let audioCtxFc = require("audioCtxFactory");
let playStatus = require("playStatus");

if (wx.setInnerAudioOption) {
wx.setInnerAudioOption({
obeyMuteSwitch: false,
autoplay: true
})
}
Component({
properties: {
msg: {
type: Object,
val: {}
},
},
data: {
playStatus: playStatus,
curStatus: playStatus.STOP,
time: "0'",
opcity: 1,
__comps__: {
audioCtx: null,
}
},
methods: {
audioPlay(){
wx.inter && clearInterval(wx.inter)
let audioCtx = this.data.__comps__.audioCtx;
var curl = ''
wx.downloadFile({
url: this.data.msg.msg.url,
header: {
"X-Requested-With": "XMLHttpRequest",
Accept: "audio/mp3",
Authorization: "Bearer " + this.data.msg.msg.token
},
success(res){
curl = res.tempFilePath;
console.log('音频本地',audioCtx)
//renderableMsg.msg.url = res.tempFilePath;
audioCtx.src = curl;
audioCtx.play();
},
fail(e){
console.log("downloadFile failed", e);
wx.showToast({
title: "下载失败,请检查网络状态",
duration: 1000
});
}
});
},

audioPause(auCtx){
//let audioCtx = this.data.__comps__.audioCtx;
let audioCtx = this.data.__comps__.audioCtx = audioCtxFc.getCtx(this.data.msg.mid) || auCtx
audioCtx&&audioCtx.pause();
},

addEvent(){
let audioCtx = this.data.__comps__.audioCtx;
audioCtx.onPlay(this.onPlaying);
audioCtx.onPause(this.onPause);
audioCtx.onWaiting(this.onPause);
audioCtx.onStop(this.onDone);
audioCtx.onEnded(this.onDone);
audioCtx.onError(this.onDone);
audioCtx.onWaiting(this.onWait)

//audioCtx.onTimeUpdate(this.onTimeUpdate);
},

delEvent(){
let audioCtx = this.data.__comps__.audioCtx;
audioCtx.offPlay(this.onPlaying);
audioCtx.offPause(this.onPause);
audioCtx.offWaiting(this.onPause);
audioCtx.offStop(this.onDone);
audioCtx.offEnded(this.onDone);
audioCtx.offError(this.onDone);
audioCtx.offWaiting(this.onWait)
// 多次播放会丢失这个回调,所以不用卸载
// audioCtx.offTimeUpdate(this.onTimeUpdate);
},
},

// lifetimes
created(){},
attached(){
console.log(this.properties.msg.msg)
this.setData({
time: this.properties.msg.msg.length + "''",
style: this.properties.msg.style
})
},
moved(){},
detached(){
let audioCtx = this.data.__comps__.audioCtx = audioCtxFc.getCtx(this.data.msg.mid);
this.audioPause(audioCtx);
this.delEvent();
//audioCtx.destroy();
},
ready(){
let self = this
let curl = ''
let audioCtx = this.data.__comps__.audioCtx = audioCtxFc.getCtx(this.data.msg.mid);
audioCtx.autoplay = false;
audioCtx.loop = false;
//
this.onPlaying = () => {
//console.log("onPlaying", JSON.stringify(this.data));
this.setData({
curStatus: playStatus.PLAYING,
});
wx.inter && clearInterval(wx.inter)
wx.inter = setInterval(() => {
let opcity = this.data.opcity;
this.setData({
opcity: opcity == 1 ? 0.4 : 1
})
}, 500)
};
this.onPause = () => {
// console.log("onPause", JSON.stringify(this.data));
// 第二次播放会立即抛出一个异常的 onPause
if(parseInt(this.data.time, 10) < 1){
return;
}
this.setData({
curStatus: playStatus.PAUSE,
opcity: 1
//time: "0'",
});
};
this.onDone = () => {
// console.log("onDone", JSON.stringify(this.data));
this.setData({
curStatus: playStatus.STOP,
opcity: 1
//time: "0'",
});
clearInterval(wx.inter)
};
// 多次播放会丢失这个回调
this.onTimeUpdate = () => {
this.setData({
time: (audioCtx.currentTime >> 0) + "'"
});
};
this.onWait = () => {

}
this.addEvent();
},
});