在进度栏(搜索栏)末尾添加移动友好的“点抓”功能,以自定义htm5音频播放器

时间:2018-08-09 22:47:15

标签: javascript css html5 html5-audio

我找到了一个自定义的html5音频播放器,并成功地对其进行了重新设计,现在我想添加一个“抓取器”以能够在特定时间抓取和移动搜索栏进度线(适用于移动设备)。我希望iOS中的“点”之类的东西能够抓取(触摸)并将进度条(搜索条)移动到我想要的确切时间。

我是JS的新手,我不知道如何使用此源代码完成此任务

完整代码CodePen

如何做到?

function calculateTotalValue(length) {
  var minutes = Math.floor(length / 60),
    seconds_int = length - minutes * 60,
    seconds_str = seconds_int.toString(),
    seconds = seconds_str.substr(0, 2),
    time = minutes + ':' + seconds

  return time;
}

function calculateCurrentValue(currentTime) {
  var current_hour = parseInt(currentTime / 3600) % 24,
    current_minute = parseInt(currentTime / 60) % 60,
    current_seconds_long = currentTime % 60,
    current_seconds = current_seconds_long.toFixed(),
    current_time = (current_minute < 10 ? "0" + current_minute : current_minute) + ":" + (current_seconds < 10 ? "0" + current_seconds : current_seconds);

  return current_time;
}

function initProgressBar() {
  var player = document.getElementById('player');
  var length = player.duration
  var current_time = player.currentTime;

  // calculate total length of value
  var totalLength = calculateTotalValue(length)
  jQuery(".end-time").html(totalLength);

  // calculate current value time
  var currentTime = calculateCurrentValue(current_time);
  jQuery(".start-time").html(currentTime);

  var progressbar = document.getElementById('seekObj');
  progressbar.value = (player.currentTime / player.duration);
  progressbar.addEventListener("click", seek);

  if (player.currentTime == player.duration) {
    $('#play-btn').removeClass('pause');
  }

  function seek(evt) {
    var percent = evt.offsetX / this.offsetWidth;
    player.currentTime = percent * player.duration;
    progressbar.value = percent / 100;
  }
};

function initPlayers(num) {
  // pass num in if there are multiple audio players e.g 'player' + i

  for (var i = 0; i < num; i++) {
    (function() {

      // Variables
      // ----------------------------------------------------------
      // audio embed object
      var playerContainer = document.getElementById('player-container'),
        player = document.getElementById('player'),
        isPlaying = false,
        playBtn = document.getElementById('play-btn');

      // Controls Listeners
      // ----------------------------------------------------------
      if (playBtn != null) {
        playBtn.addEventListener('click', function() {
          togglePlay()
        });
      }

      // Controls & Sounds Methods
      // ----------------------------------------------------------
      function togglePlay() {
        if (player.paused === false) {
          player.pause();
          isPlaying = false;
          $('#play-btn').removeClass('pause');

        } else {
          player.play();
          $('#play-btn').addClass('pause');
          isPlaying = true;
        }
      }
    }());
  }
}

initPlayers(jQuery('#player-container').length);

1 个答案:

答案 0 :(得分:0)

不确定是否可以通过“ progress”元素来完成,但是可以通过“ range”元素来实现

首先,删除“进度”并添加

<input id="seekObj" type="range" name="rng" min="0" step="0.25" value="0" onchange="seekAudio()" oninput="seekAudio()" style="width: 99%">

在那之后,我们添加一些css使其看起来像是一个不错的搜索栏,在您的情况下,您正在使用“ SCSS”,因此这里是代码

$background_color_1: #df7164;

input[type=range] {
    -webkit-appearance: none;
    -moz-apperance: none;
    border-radius: 6px;
    height: 6px;
    background-image: -webkit-gradient(linear, left top, right top, color-stop(0%, #df7164), color-stop(0%, #F5D0CC));
    background-image: -moz-linear-gradient(left center, #DF7164 0%, #DF7164 0%, #F5D0CC 0%, #F5D0CC 100%);
    &:focus {
        outline: none;
        border: none;
    }
    &::-webkit-slider-thumb {
        -webkit-appearance: none !important;
        background-color: $background_color_1;
        height: 13px;
        width: 13px;
        border-radius: 50%;
    }
    &::-moz-range-thumb {
        -moz-appearance: none !important;
        background-color: $background_color_1;
        border: none;
        height: 13px;
        width: 13px;
        border-radius: 50%;
    }
}
input[type="range"] {
    &::-moz-range-track {
        border: none;
        background: none;
        outline: none;
    }
}

所有操作都已完成,因此我们必须对JS和HTML进行一些更改才能使其正常工作

在JS中添加这些功能

function setupSeek() {
  seek.max = player.duration;
}
function seekAudio() {
isSeeking=true;
player.currentTime = seek.value;
isSeeking=false;
}

将“玩家”和“寻找”元素检索移至顶部,并添加寻找状态持有者

var isSeeking=false;
var seek = document.getElementById("seekObj");
var player = document.getElementById("player");

现在在“ initProgressBar()”函数中添加一些代码,以使“ range”元素起作用

if(!isSeeking){
seek.value = player.currentTime;
}

到此为止,一切都已完成,我们只需要从HTML调用函数,就可以进行更改,从音频元素开始,我们需要调用“ setUpSeek”,因此只需将其添加到“ audio” < / p>

ondurationchange="setupSeek()"

现在,我们完成了,您可以检查CodePen以获得更好的理解

需要进行一些改进,以使滑块以更好的方式工作。

添加此Javascript代码,这将使搜索栏的外观和感觉像本机移动搜索栏

function SetSeekColor(){
  try{
   var val = ($("#seekObj").val() - $("#seekObj").attr('min')) / ($("#seekObj").attr('max') - $("#seekObj").attr('min'));
    var percent = val * 100;
  $("#seekObj").css('background-image',
        '-webkit-gradient(linear, left top, right top, ' +
        'color-stop(' + percent + '%, #df7164), ' +
        'color-stop(' + percent + '%, #F5D0CC)' +
        ')');

    $("#seekObj").css('background-image',
        '-moz-linear-gradient(left center, #DF7164 0%, #DF7164 ' + percent + '%, #F5D0CC ' + percent + '%, #F5D0CC 100%)');
  }
  catch(e){}
}

要完美拖动,您可能需要添加第三方插件/ js,您可以找到一个here