在Javascript中暂停音频

时间:2017-10-24 16:12:09

标签: javascript audio

我正在尝试使用触发不同音频剪辑的按钮制作数字MPC。

我点击了一次按钮即可运行并播放音频,但是,我正在尝试再次点击时暂停音频。

我已将所有代码输入到此帖子中,任何人都想运行它,但我的问题是在带有“mousedown”功能的Javascript文件中。

如果有人能提供见解,我会很感激。

@import 'https://fonts.googleapis.com/css?family=Oswald';

html, body {
  background: #000000;
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  font-family: 'Oswald', sans-serif;
}

h1 {
  color: #fff;
  font-size: 5vw;
  letter-spacing: 6px;
}

.pad {
  width: 500px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.box {
  width: 100px;
  height: 100px;
  margin: 10px 0;
  box-shadow: 0 8px 6px -6px black;
  background-color: #444;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  color: rgba(255, 255, 255, 0.4);
  user-select: none;
  
  &:hover {
    background-color: lighten(#444, 10%);
    cursor: pointer;
  }
  
  &:active {
    background-color: darken(#444, 10%);
    transform: scale(1.1);
    transition: all .2s;
  }
}

.active {
  background-color: darken(#444, 10%);
  transform: scale(1.1);
  transition: all .2s;
}

.pad-1{
  background-image: url(../images/shrek.jpg);
  background-size:cover;
  background-repeat: no-repeat;
  background-position: center;
  border: 2px solid #ffffff;

}
<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Viral Remix</title>
  
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
      <link rel="stylesheet" href="css/styles.css">

</head>

<body>
  <h1>VIRAL REMIXER</h1>

<div class="pad">
  <div class="box pad-1" data-code="82">R</div>

</div>




  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>

    <script  src="js/scripts.js"></script>

</body>
</html>
&lt;b&gt;

1 个答案:

答案 0 :(得分:0)

我只想使用布尔标志来表示播放状态。

var playing = false;

$('.pad-1').mousedown(function() {
    if(!playing) {
        play();
    } else {
        pause();
    }
});

function play() {
   padOne.load();
   padOne.play();
   playing = true;
}

function pause() {
   padOne.pause();
   playing = false;
}