-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioPlay.html
332 lines (291 loc) · 10.3 KB
/
audioPlay.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="css/common.css"/>
<link rel="stylesheet" href="css/audioPlay.css"/>
<title>happy English</title>
<style>
body{
background: #f68a5c;
}
</style>
</head>
<body>
<div>
<div class="top">
<p class="top_title">Old McDonald Had A Farm</p>
<p class="top_author">by The Kiboomers</p>
</div>
</div>
<div class="vedioShow">
<img src="img/audioPlay/vedio.png" class="vedioImg"/>
</div>
<div class="audio-wrapper">
<audio id="audio">
<source src="http://sc1.111ttt.com/2017/1/11/11/304112002347.mp3" type="audio/mp3">
</audio>
<div class="audio-right">
<div class="progress-bar-bg" id="progressBarBg"><span id="progressDot" style="left:0px"></span>
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="audio-time"><span class="audio-length-current" id="audioCurTime">00:00</span><span class="audio-length-total" id="audio-length-total"></span></div>
</div>
<div class="audio-left"><img id="audioPlayer" src="img/audioPlay/vPlay.png" ></div>
</div>
</body>
<script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script>
<script>
var audio = $('audio')[0];
$(document).ready(function() {
// 控制音频文件名显示宽度
var maxW = $('.audio-right').width();
$('.audio-right p').css({
"max-width": maxW
});
initAudioEvent();
getTime();
dragMove();//并且滚动条开始滑动
});
//计算总时间
function getTime() {
setTimeout(function () {
var duration = $("#audio")[0].duration;
if(isNaN(duration)){
getTime();
}
else{
var allTime=transTime($("#audio")[0].duration);
document.getElementById('audio-length-total').innerHTML=allTime;
}
}, 10);
}
/**
* 初始化音频控制事件
*/
function initAudioEvent() {
// 点击播放/暂停图片时,控制音乐的播放与暂停
$('#audioPlayer').click(function() {
// 监听音频播放时间并更新进度条
audio.addEventListener('timeupdate', function() {
updateProgress(audio);
}, false);
// 监听播放完成事件
audio.addEventListener('ended', function() {
audioEnded();
}, false);
// 改变播放/暂停图片
if(audio.paused) {
// 开始播放当前点击的音频
audio.play();
$('#audioPlayer').attr('src', 'img/audioPlay/vStop.png');
$('.vedioImg').addClass('rotateImg');
} else {
audio.pause();
$('#audioPlayer').attr('src', 'img/audioPlay/vPlay.png');
$('.vedioImg').removeClass('rotateImg');
}
addListenTouch();
});
// 点击进度条跳到指定点播放
$('#progressBarBg').click(function(e) {
// 只有音乐开始播放后才可以调节,已经播放过但暂停了的也可以
if(!audio.paused || audio.currentTime != 0) {
var pgsWidth = $('.progress-bar-bg').width();
var rate = e.offsetX / pgsWidth;
audio.currentTime = audio.duration * rate;
updateProgress(audio);
}
});
/*var dot = document.getElementById('progressDot');
// 鼠标拖动进度点时可以调节进度
// 只有音乐开始播放后才可以调节,已经播放过但暂停了的也可以
// 鼠标按下时
var moveing = false;
function addListenTouch() {
dot.addEventListener("touchstart", touchStart, false);
dot.addEventListener("touchmove", touchMove, false);
dot.addEventListener("touchend", touchEnd, false);
}
function touchStart(e) {
e.preventDefault();
var touch = e.touches[0];
startX = touch.clientX;
}
function touchMove(e) { //滑动
var touch = e.touches[0];
var pgsWidth = $('.progress-bar-bg').width();
var oriLeft = dot.offsetLeft;
var mouseX = touch.clientX;
var maxLeft = oriLeft; // 向左最大可拖动距离
var maxRight = document.getElementById('progressBarBg').offsetWidth - oriLeft; // 向右最大可拖动距离
// 禁止默认的选中事 件(避免鼠标拖拽进度点的时候选中文字)
if(e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
// 禁止事件冒泡
if(e && e.stopPropagation) {
e.stopPropagation();
} else {
window.event.cancelBubble = true;
}
var length = touch.clientX - mouseX;
if(length > maxRight) {
length = maxRight;
} else if(length < -maxLeft) {
length = -maxLeft;
}
var pgsWidth = $('.progress-bar-bg').width();
var rate = (oriLeft + length) / pgsWidth;
var nowtime = audio.duration * rate;
audio.currentTime = nowtime;
updateProgress(audio);
}
function touchEnd(e) { //手指离开屏幕
e.preventDefault();
document.onmousemove = null;
document.onmouseup = null;
}
}*/
//进度条
var startX, x, aboveX = 0; //触摸时的坐标 //滑动的距离 //设一个全局变量记录上一次内部块滑动的位置
//1拖动监听touch事件
var dot = document.getElementById('progressDot');
function addListenTouch() {
dot.addEventListener("touchstart", touchStart, false);
dot.addEventListener("touchmove", touchMove, false);
dot.addEventListener("touchend", touchEnd, false);
//var speed = document.getElementById("speed");
}
//touchstart,touchmove,touchend事件函数
function touchStart(e) {
e.preventDefault();
var touch = e.touches[0];
startX = touch.pageX;
}
function touchMove(e) { //滑动
e.preventDefault();
var touch = e.touches[0];
x = touch.pageX - startX; //滑动的距离
//drag.style.webkitTransform = 'translate(' + 0+ 'px, ' + y + 'px)';
dot.style.left = aboveX + x + "px"; //
//speed.style.left = -((window.innerWidth) - (aboveX + x)) + "px";
}
function touchEnd(e) { //手指离开屏幕
e.preventDefault();
aboveX = parseInt(dot.style.left);
var pgsWidth = $('.progress-bar-bg').width();
var touch = e.touches[0];
var dragPaddingLeft = dot.style.left;
var change = dragPaddingLeft.replace("px", "");
numDragpaddingLeft = parseInt(change);
var currentTime = (numDragpaddingLeft / (pgsWidth)) * audio.duration;
audio.currentTime = currentTime;
/*var pgsWidth = $('.progress-bar-bg').width();
var rate = (oriLeft + length) / pgsWidth;
var nowtime = audio.duration * rate;
audio.currentTime = nowtime;*/
/*var pgsWidth = $('.progress-bar-bg').width();
var oriLeft = dot.offsetLeft;//距离左边
var mouseX = touch.clientX;
var maxLeft = oriLeft; // 向左最大可拖动距离
var maxRight = document.getElementById('progressBarBg').offsetWidth - oriLeft; // 向右最大可拖动距离
// 禁止默认的选中事 件(避免鼠标拖拽进度点的时候选中文字)
if(e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
// 禁止事件冒泡
if(e && e.stopPropagation) {
e.stopPropagation();
} else {
window.event.cancelBubble = true;
}
var length = touch.clientX - mouseX;
if(length > maxRight) {
length = maxRight;
} else if(length < -maxLeft) {
length = -maxLeft;
}
var pgsWidth = $('.progress-bar-bg').width();
var rate = (oriLeft + length) / pgsWidth;
var nowtime = audio.duration * rate;
audio.currentTime = nowtime;
updateProgress(audio);*/
}
}
//3拖动的滑动条前进
function dragMove() {
setInterval(function() {
var dot = document.getElementById('progressDot');
var value =audio.currentTime / audio.duration;
var value1 =(audio.currentTime / audio.duration)
$('#progressBar').css('width', value * 100 + '%');
dot.style.left=(value1 * 100 + '%');
//dot.style.left = (audio.currentTime / audio.duration) * (window.innerWidth - 30) + "px";
//speed.style.left = -((window.innerWidth) - (audio.currentTime / audio.duration) * (window.innerWidth - 30)) + "px";
}, 500);
}
/**
* 更新进度条与当前播放时间
* @param {object} audio - audio对象
*/
function updateProgress(audio) {
var dot = document.getElementById('progressDot');
var value =audio.currentTime / audio.duration;
$('#progressBar').css('width', value * 100 + '%');
dot.style.left=(value * 100 + '%');
//dot.css('left', value * 100 + '%');
console.log(dot.style.left)
$('#audioCurTime').html(transTime(audio.currentTime));
}
/**
* 播放完成时把进度调回开始的位置
*/
function audioEnded() {
$('#progressBar').css('width', 0);
$('#progressDot').css('left', 0);
$('#audioPlayer').attr('src', 'img/audioPlay/vPlay.png');
$('.vedioImg').removeClass('rotateImg');
$('#audioCurTime').html('00:00');
}
/**
* 音频播放时间换算
* @param {number} value - 音频当前播放时间,单位秒
*/
function transTime(value) {
var time = "";
var h = parseInt(value / 3600);
value %= 3600;
var m = parseInt(value / 60);
var s = parseInt(value % 60);
if(h > 0) {
time = formatTime(h + ":" + m + ":" + s);
} else {
time = formatTime(m + ":" + s);
}
return time;
}
/**
* 格式化时间显示,补零对齐
* eg:2:4 --> 02:04
* @param {string} value - 形如 h:m:s 的字符串
*/
function formatTime(value) {
var time = "";
var s = value.split(':');
var i = 0;
for(; i < s.length - 1; i++) {
time += s[i].length == 1 ? ("0" + s[i]) : s[i];
time += ":";
}
time += s[i].length == 1 ? ("0" + s[i]) : s[i];
return time;
}
</script>
</script>
</html>