-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
240 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,151 @@ | ||
var progress = document.querySelector(".progress"); | ||
var progressBar = document.querySelector(".progress-bar"); | ||
var spanDrag = document.querySelector("span"); | ||
var spanDrag = document.querySelector("#rewind"); | ||
var progressWidth = progress.clientWidth; | ||
var positionSpanDragDown = 0; | ||
var positionSpanDragUp = 0; | ||
var positionLastSpanDrag = 0; | ||
var offsetX = 0; | ||
// var clientXDrag = 0; | ||
console.log(progressWidth); | ||
var rateAudio = 0; | ||
var flagCheckTimeUpdate = true; | ||
function handlerProgressDown(e) { | ||
console.log("down"); | ||
if (flagCheckTimeUpdate) { | ||
positionSpanDragUp = (audio.currentTime / audio.duration) * progressWidth; | ||
flagCheckTimeUpdate = false; | ||
} | ||
offsetX = e.offsetX; | ||
positionLastSpanDrag = offsetX; | ||
positionSpanDragUp = offsetX; | ||
positionSpanDragDown = e.clientX; | ||
console.log(positionSpanDragDown); | ||
var rate = (offsetX / progressWidth) * 100; | ||
rateAudio = rate; | ||
audio.currentTime = (rateAudio / 100) * audio.duration; | ||
progressBar.style.width = `${rate}%`; | ||
|
||
document.addEventListener("mousemove", handlerDrag); | ||
} | ||
|
||
function handlerDrag(e) { | ||
console.log("move"); | ||
var clientXDrag = e.clientX; | ||
console.log(clientXDrag); | ||
positionLastSpanDrag = | ||
clientXDrag - positionSpanDragDown + positionSpanDragUp; | ||
// console.log(positionLastSpanDrag); | ||
var rate = (positionLastSpanDrag / progressWidth) * 100; | ||
// console.log(rate); | ||
if (rate < 0) rate = 0; | ||
if (rate > 100) rate = 100; | ||
if (rate <= 0) { | ||
rate = 0; | ||
} | ||
if (rate >= 100) { | ||
rate = 100; | ||
} | ||
rateAudio = rate; | ||
console.log(`rate: ${rate}`); | ||
currentTime.innerText = `${getTime((rateAudio / 100) * audio.duration)}`; | ||
progressBar.style.width = `${rate}%`; | ||
} | ||
function handlerMouseUp() { | ||
console.log("up"); | ||
if (positionLastSpanDrag < 0) positionLastSpanDrag = 0; | ||
if (positionLastSpanDrag > progressWidth) { | ||
positionLastSpanDrag = progressWidth; | ||
} | ||
if (rateAudio >= 100) { | ||
rateAudio = 100; | ||
audio.currentTime = audio.duration; | ||
positionLastSpanDrag = progressWidth; | ||
audio.pause(); | ||
} | ||
if (flagCheckTimeUpdate === false) { | ||
flagCheckTimeUpdate = true; | ||
audio.currentTime = (rateAudio / 100) * audio.duration; | ||
} | ||
positionSpanDragUp = positionLastSpanDrag; | ||
// console.log(`positionSpanDragUp mouseup: ${positionSpanDragUp}`); | ||
document.removeEventListener("mousemove", handlerDrag); | ||
} | ||
|
||
function handlerProgressMove(e) { | ||
// console.log(e.target.className); | ||
if ( | ||
e.target.className === "progress" || | ||
e.target.className === "progress-bar" | ||
) { | ||
var timeView = (e.offsetX / progressWidth) * audio.duration; | ||
// console.log(getTime(timeView)); | ||
timerView.classList.add("action-timer-view"); | ||
timerView.innerText = getTime(timeView); | ||
timerView.style.left = `${e.offsetX}px`; | ||
} | ||
} | ||
|
||
progress.addEventListener("mousedown", handlerProgressDown); | ||
progress.addEventListener("mouseover", function () { | ||
progress.addEventListener("mousemove", handlerProgressMove); | ||
}); | ||
progress.addEventListener("mouseout", function () { | ||
timerView.classList.remove("action-timer-view"); | ||
progress.removeEventListener("mousemove", handlerProgressMove); | ||
}); | ||
|
||
// spanDrag.addEventListener("mousemove", function (e) { | ||
// e.stopPropagation(); | ||
// }); | ||
|
||
spanDrag.addEventListener("mousedown", function (e) { | ||
e.stopPropagation(); | ||
document.addEventListener("mousemove", handlerDrag); | ||
if (flagCheckTimeUpdate) { | ||
positionSpanDragUp = (audio.currentTime / audio.duration) * progressWidth; | ||
flagCheckTimeUpdate = false; | ||
} | ||
positionSpanDragDown = e.clientX; | ||
document.addEventListener("mousemove", handlerDrag); | ||
}); | ||
document.addEventListener("mouseup", handlerMouseUp); | ||
// Edit Audio | ||
var playerAudio = document.querySelector(".player-audio i"); | ||
var audio = document.querySelector("audio"); | ||
var currentTime = progress.previousElementSibling; | ||
var totalTime = progress.nextElementSibling; | ||
var timerView = progressBar.previousElementSibling; | ||
// console.log(timerView); | ||
function getTime(time) { | ||
var mins = Math.floor(time / 60); | ||
var secs = Math.floor(time - mins * 60); | ||
return `${mins < 10 ? `0${mins}` : mins}:${secs < 10 ? `0${secs}` : secs}`; | ||
} | ||
// 278.282449 | ||
window.addEventListener("load", function () { | ||
totalTime.innerText = getTime(audio.duration); | ||
|
||
currentTime.innerText = getTime(audio.currentTime); | ||
// event ended hết nhạc | ||
audio.addEventListener("ended", function () { | ||
playerAudio.classList.replace("fa-pause", "fa-play"); | ||
}); | ||
// event play | ||
audio.addEventListener("play", function () { | ||
playerAudio.classList.replace("fa-play", "fa-pause"); | ||
}); | ||
// event pause | ||
audio.addEventListener("pause", function () { | ||
playerAudio.classList.replace("fa-pause", "fa-play"); | ||
}); | ||
// event timeupdate | ||
audio.addEventListener("timeupdate", function (e) { | ||
if (flagCheckTimeUpdate) { | ||
currentTime.innerText = getTime(audio.currentTime); | ||
// console.log(); | ||
console.log(audio.currentTime, audio.duration); | ||
// console.log(getTime(audio.currentTime)); | ||
rateAudio = (audio.currentTime / audio.duration) * 100; | ||
progressBar.style.width = `${rateAudio}%`; | ||
} | ||
}); | ||
// event playerAudio click | ||
playerAudio.addEventListener("click", handlerPlayerClick); | ||
}); | ||
|
||
function handlerPlayerClick(e) { | ||
e.stopPropagation(); | ||
if (audio.paused) { | ||
audio.play(); | ||
} else { | ||
audio.pause(); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters