我已经在javascript / jquery中创建了一个音频播放器。台式机版本运行良好,但是在手机上,我无法使音量进度条可用。
首先,我在播放/暂停按钮上遇到了同样的问题,但是现在已经解决了。
我发现,offsetX似乎不适用于移动设备。
我已经尝试过pageX和clientX,但是没有解决。
我绝对是使用javascript的新手,所以希望有人能提供帮助。
function updateVolumeProgressBar(audio) {
let volume = audio.volume * 100;
$(".volumeBar .progress").css("width", volume + "%");
}
this.audio.addEventListener("volumechange", function() {
updateVolumeProgressBar(this);
});
$(".volumeBar .progressBar").on("mousedown touchstart", function() {
mouseDown = true;
console.log("test4");
});
$(".volumeBar .progressBar").on("mousemove", function(e) {
if(mouseDown === true) {
let percentage = e.offsetX / $(this).width();
console.log(e.offsetX);
if(percentage >= 0 && percentage <= 1) {
audioElement.audio.volume = percentage;
}
}
});
$(".volumeBar .progressBar").on("mouseup", function(e) {
let percentage = e.offsetX / $(this).width();
if(percentage >= 0 && percentage <= 1) {
audioElement.audio.volume = percentage;
}
});
$(".volumeBar .progressBar").on("touchend", function(e) {
var orig = e.originalEvent;
let percentage = (orig.offsetX) / $(this).width();
console.log(orig.offsetX);
if(percentage >= 0 && percentage <= 1) {
audioElement.audio.volume = percentage;
}
});
$(document).mouseup(function() {
mouseDown = false;
});
$(document).on("touchend", function() {
mouseDown = false;
});
更新:我的Java语言已更改
function initProgressBar(audio) {
var length = audio.duration;
var current_time = audio.currentTime;
// calculate total length of value
var totalLength = calculateRemainingTime();
jQuery(".progressTime.remaining").text(totalLength);
// calculate current value time
var currentTime = calculateCurrentValue(current_time);
jQuery(".progressTime.current").text(currentTime);
var progressbar = document.getElementById('timeSeek');
progressbar.value = (audio.currentTime / audio.duration);
$(progressbar).on("vclick", seek);
function seek(evt) {
var seekto = audio.duration * (progressbar.value / 100);
var percent = evt.offsetX / this.offsetWidth;
audio.currentTime = percent * audio.duration;
progressbar.value = percent / 100;
}
}
function Audio() {
this.currentlyPlaying;
this.audio = document.createElement('audio');
this.audio.addEventListener("ended", function() {
nextSong();
});
this.audio.addEventListener("timeupdate", function(){
if(this.duration) {
initProgressBar(this);
}
});
this.audio.addEventListener("volumechange", function() {
updateVolumeProgressBar(this);
});
this.setTrack = function(track) {
this.currentlyPlaying = track;
this.audio.src = track.path;
};
this.play = function() {
this.audio.play();
};
this.pause = function() {
this.audio.pause();
};
}
HTML部分
<div class="playbackBar">
<span class="progressTime current">0:00</span>
<div class="progressBar">
<div class="progressBarBg">
<progress id="timeSeek" value="0" max="1"></progress>
</div>
</div>
<span class="progressTime remaining">0.00</span>
</div>