-
Notifications
You must be signed in to change notification settings - Fork 0
/
myScript.js
130 lines (106 loc) · 3.61 KB
/
myScript.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
let player = new Audio()
let playing = false
let prevTrack;
let repeat = false
$('#repeat').on('click', toggleRepeat)
$('button').on('click', playClip)
$('#next').on('click', playNext)
$('#prev').on('click', playNext)
$(player).on('timeupdate', function () {
$('#seek-obj').attr("value", this.currentTime / this.duration,
secondsToMinutes(this.currentTime, "start-time"),
secondsToMinutes(this.duration, "end-time"));
})
$(player).on('ended', playNext)
function playClip() {
if ($(this).parent(".list-item").length > 0) {
play($(this))
let track = $(this).attr('track')
songInfo(track)
markSong(track)
}
// play pause
if ($("#play").is($(this))) {
if (playing) {
player.pause()
playing = false
$("#play").addClass("fa-play")
$("#play").removeClass("fa-pause")
} else {
player.play()
playing = true;
$("#play").addClass("fa-pause")
$("#play").removeClass("fa-play")
}
}
}
function playNext() {
let number;
if ($("#next").is($(this))) {
number = 1;
} else if ($("#prev").is($(this))){
//If the song have played less then 3 seconds play previous song else start from 0.
if (player.currentTime < 3) {
number = -1
} else {
number = 0
}
}else{ // if the songs end if it should repeat or go to next
if (repeat){
number = 0
}else {
number = 1
}
}
let nextTrack = parseInt(prevTrack) + number
if (0 > nextTrack) {
nextTrack = $("#playlist li").toArray().length
} else if ($("#playlist li").toArray().length < nextTrack) {
nextTrack = 0
}
play($("#item" + nextTrack + " >button"))
songInfo(nextTrack)
markSong(nextTrack)
}
function play(song) {
player.src = $(song).attr('data-sound')
player.play()
playing = true;
$("#play").addClass("fa-pause")
$("#play").removeClass("fa-play")
}
function songInfo(track) {
let bg = $("#item-pic" + track)
let artist = $("#artist" + track)
let song = $("#song" + track)
$("#song-image-container").css('background-image', bg.css('background-image'))
$("#info-item-artist").html($(artist).html())
$("#info-item-song").html($(song).html())
}
function markSong(track) {
if (prevTrack !== track) {
$("#item" + track).addClass("active")
if (prevTrack !== null && prevTrack !== undefined) {
$("#item" + prevTrack).removeClass("active")
}
prevTrack = track
}
}
function secondsToMinutes(time, holder) {
var min = ~~((time % 3600) / 60)
var sec = Math.floor(time % 60);
if(!isNaN(min) && !isNaN(sec)){
var secMin = min + ":" + (sec < 10 ? "0" : "") + sec
$("#" + holder).html(secMin)
}
}
function toggleRepeat(){
repeat = !repeat
if(repeat){
$(this).addClass("activeIcon")
console.log("repeat")
}else{
console.log("dont repeat")
$(this).removeClass("activeIcon")
}
}