禁用播放/暂停按钮,直到音频连接

时间:2019-01-08 04:33:51

标签: javascript button input form-submit

在空文本输入上单击“设置”不应将svg更改为暂停

如何设置它,以便如果输入区域中没有 .src流,则单击它不会将播放svg更改为暂停。

要进行更改,必须在其中有 .src流

代码: https://jsfiddle.net/vhgL96se/23/

这是音频流:http://hi5.1980s.fm/;

如果没有 src流,则不能显示暂停按钮

什么是能够做到这一点的好方法?

我想禁用播放/暂停按钮,直到与音频建立连接。

enter image description here

(function iife() {
    "use strict";
    const player = document.getElementById("player");
    const button = document.getElementById("button");
    const value = document.getElementById("input");
    const sent = document.getElementById("sent");
    const input = document.getElementById("clear");

    function playPauseIcon(play) {
        if (play) {
            button.classList.add("is-playing");
        } else {
            button.classList.remove("is-playing");
        }
    }
    button.addEventListener("click", function () {
        if (player.paused) {
            player.play();
            playPauseIcon(true);
        } else {
            player.pause();
            playPauseIcon(false);
        }
    });
    button.addEventListener("mousedown", function (evt) {
        if (evt.detail > 1) {
            evt.preventDefault();
        }
    }, false);
    sent.addEventListener("click", function () {
        player.src = value.value;
        player.volume = 1.0;
        playPauseIcon(true);     
    });
    input.addEventListener("click", function () {
        value.value = "";
          button.classList.remove("is-playing");
          player.pause();
    }, false);
}());

1 个答案:

答案 0 :(得分:0)

您可以采用一个变量并将其用于音频的onloadstart事件

let canPlay = false;
player.onloadstart  = function() {
    canPlay = true;
  };

并像这样检查它

 button.addEventListener("click", function() {
    if(!canPlay){return};
    if (player.paused) {
      player.play();
      playPauseIcon(true);
    } else {
      player.pause();
      playPauseIcon(false);
    }
  });

将其更改为透明

input.addEventListener("click", function() {
    value.value = "";
    button.classList.remove("is-playing");
    player.pause();
    canPlay = false;
  }, false);

Demo