-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscript.js
134 lines (115 loc) · 4.59 KB
/
script.js
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
// 原生HTML5音频播放器,微信文章页播放器样式
// Oaker (http://github.com/cyio)
// origin : http://www.alexkatz.me/html5-audio/building-a-custom-html5-audio-player-with-javascript/
window.onload = function () {
player();
function player() {
console.log('player init');
var audio = document.getElementsByTagName('audio')[0]; // id for audio element
var audioWrapper = document.getElementById('music'); // audio interface
var duration; // Duration of audio clip
var pButton = document.getElementById('pButton'); // play button
var playhead = document.getElementById('playhead');
var timeline = document.getElementById('timeline');
var playLength = document.getElementsByClassName('audio_length')[0];
// timeline width adjusted for playhead
var timelineWidth = timeline.offsetWidth - playhead.offsetWidth;
var playIcon = document.getElementsByClassName('icon_audio_default')[0];
var playingIcon = document.getElementsByClassName('icon_audio_playing')[0];
audioWrapper.addEventListener("click", function () {
play();
}, false);
// timeupdate event listener
audio.addEventListener("timeupdate", timeUpdate, false);
//Makes timeline clickable
timeline.addEventListener("click", function (event) {
// moveplayhead(event);
// audio.currentTime = duration * clickPercent(event);
}, false);
// returns click as decimal (.77) of the total timelineWidth
function clickPercent(e) {
return (e.pageX - timeline.offsetLeft) / timelineWidth;
}
// Makes playhead draggable
playhead.addEventListener('mousedown', mouseDown, false);
window.addEventListener('mouseup', mouseUp, false);
// Boolean value so that mouse is moved on mouseUp only when the playhead is released
var onplayhead = false;
// mouseDown EventListener
function mouseDown() {
onplayhead = true;
window.addEventListener('mousemove', moveplayhead, true);
audio.removeEventListener('timeupdate', timeUpdate, false);
}
// mouseUp EventListener
// getting input from all mouse clicks
function mouseUp(e) {
if (onplayhead === true) {
moveplayhead(e);
window.removeEventListener('mousemove', moveplayhead, true);
// change current time
audio.currentTime = duration * clickPercent(e);
audio.addEventListener('timeupdate', timeUpdate, false);
}
onplayhead = false;
}
// mousemove EventListener
// Moves playhead as user drags
function moveplayhead(e) {
var newMargLeft = e.pageX - timeline.offsetLeft;
if (newMargLeft >= 0 && newMargLeft <= timelineWidth) {
playhead.style.marginLeft = newMargLeft + "px";
}
if (newMargLeft < 0) {
playhead.style.marginLeft = "0px";
}
if (newMargLeft > timelineWidth) {
playhead.style.marginLeft = timelineWidth + "px";
}
}
// timeUpdate
// Synchronizes playhead position with current point in audio
function timeUpdate() {
var playPercent = timelineWidth * (audio.currentTime / duration);
playhead.style.width = playPercent + "px";
if (audio.currentTime == duration) {
playIcon.style.display = "inline-block";
playingIcon.style.display = "none";
}
}
//Play and Pause
function play() {
// start audio
if (audio.paused) {
audio.play();
// toggle icons display
playIcon.style.display = "none";
playingIcon.style.display = "inline-block";
} else { // pause audio
audio.pause();
playIcon.style.display = "inline-block";
playingIcon.style.display = "none";
}
}
// Gets audio file duration
audio.addEventListener("canplaythrough", function () {
duration = audio.duration;
playLength.textContent = timeFormat(Math.floor(duration));
}, false);
// when audio stop
audio.addEventListener("ended", function () {
playIcon.style.display = "inline-block";
playingIcon.style.display = "none";
}, false);
function timeFormat(timestamp) {
var date = new Date(parseInt(timestamp) * 1000);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minite = date.getMinutes();
var second = date.getSeconds();
return minite + ":" + second;
}
};
}