audioHelper.js
3.24 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
define(['mUtil', 'css!FSound_css'], function(mUtil){
var pauseAllAudios = function(){
$(".wev-audio audio").each(function(){
var $this = $(this);
if(!this.paused){
this.pause();
$this.siblings(".wev-audio-status").removeClass('playing').removeClass('error');
mUtil.getLabel(5328, "继续播放", function(msg){
$this.siblings(".wev-audio-tips").text(msg);
});
}
});
};
return {
playAudio: function(audioContainer){
if(!audioContainer.hasAttribute('data-audio')){
return;
}
var $container = $(audioContainer).children('.wev-audio-container'),
$status = $container.children('.wev-audio-status'),
$tips = $container.children('.wev-audio-tips'),
audio = $container.find('audio')[0];
$container.off('click.listenaudio');
$container.on('click.listenaudio', function(e){
if(!audio || !audio.hasAttribute('loaded')){
return;
}
if(audio.error != null){
$status.addClass('error');
mUtil.getLabel(5292, "语音加载出错", function (msg) {
$tips.text(msg);
});
return;
}
var totaltime = audio.duration;
if(totaltime && !isNaN(totaltime) && totaltime != Infinity) {
$container.siblings(".wev-audio-time").text(parseInt(totaltime) + '"');
}
if(audio.paused){
pauseAllAudios();
if(!audio.played.length){
audio.load();
}
audio.play();
$status.removeClass('error').addClass('playing');
$tips.text('');
} else {
audio.pause();
$status.removeClass('playing');
mUtil.getLabel(5328, "继续播放", function (msg) {
$tips.text(msg);
});
}
e.stopPropagation();
e.preventDefault();
});
audio.addEventListener('error', function(){
$status.addClass('error');
mUtil.getLabel(5292, "语音加载出错", function (msg) {
$tips.text(msg);
audio.removeAttribute('loaded');
})
});
audio.addEventListener('ended', function(){
$status.removeClass('playing');
mUtil.getLabel(5290, "重新播放", function (msg) {
$tips.text(msg);
});
audio.currentTime = 0;
audio.load();
}, false);
},
pauseAllAudios: pauseAllAudios,
reloadAudios: function($audios){
$audios.each(function(){
//reload播放中的语音,未播放或播放完的语音在click事件中load
if(this.played.length){
this.load();
}
});
}
};
});