1

I'm trying to play audio when the div .deley change to display: block, but something does not work. The online audio clips are correct, it's not the problem. My problem is in my script, but I can not find it. Maybe .pause is not a function?

What could be wrong?

function see() {
  var x = document.getElementById("deley");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
};

// === Scrip for audio play ====

$(document).ready(function(){
    $(".deley").hide();
    // if sound is currently playing, stop it and reset
    if(!$("#notify").paused) {
        $("#notify").pause();
        $("#notify").currentTime = 0;
    }
    setTimeout(function () {
        $(".deley").show();
        $("#notify").play();            
    }, 0);
});
#cont {
  width:200px;
  margin:0 auto
}

.deley {
  display:none;
  width:96px;
  height:40px;
  background:red;
  margin:20px 0;
  text-align:center;
  line-height:40px;
  font-family:Arial, sans-serif;
  color:#fff
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div id="cont">

<div class="deley" id="deley">Deley</div>

<audio id="notify">
  <source src="https://notificationsounds.com/soundfiles/8b16ebc056e613024c057be590b542eb/file-sounds-1113-unconvinced.ogg" type="audio/ogg">
  <source src="https://notificationsounds.com/soundfiles/8b16ebc056e613024c057be590b542eb/file-sounds-1113-unconvinced.mp3" type="audio/mpeg">
</audio>

<button onclick="see()">Open / Close</button>
  
</cont>
Dale K
  • 25,246
  • 15
  • 42
  • 71
Pablo_Web
  • 423
  • 2
  • 14

4 Answers4

3

jQuery Objects and JavaScript Objects are different. They do not recognize one another which is why the following statements do not work:

if (!$(".notify").paused) {
  $(".notify").pause();
  $(".notify").currentTime = 0;
...
  $(".notify").play();

The MediaElement API is Plain JavaScript only. Any methods/properties/events from said API is not recognized by jQuery -- Plain JavaScript Objects are needed to use methods .pause() and .play(), and properties .paused and .currentTime.

Solutions

  1. Reference a Plain JavaScript Object

    • document.querySelector(selector)
  2. Dereference a jQuery Object

    • $(selector)[0]
      OR
    • $(selector).get(0)

There are a couple of other important things to remember about jQuery:

  1. Never use onevent attributes: <buttononclick='func();'</button>

  2. Assign .class attributes to elements avoid #id attributes.

The versatility jQuery affords to us makes such antiquated practices unnecessary and wasteful. Also, the function see() is no longer needed -- functionality of see() is now integrated into function audioSwitch().

$('button').on('click', audioSwitch);

function audioSwitch(e) {
  if (!$(".notify")[0].paused || $('.delay').is(':visible')) {
    $(".notify")[0].pause();
    $(".notify")[0].currentTime = 0;
    $(".delay").hide();
  } else {
    setTimeout(function() {
      $(".notify")[0].play();
      $(".delay").show();
    }, 750);
  }
}
.cont {
  width: 200px;
  margin: 0 auto
}

.delay {
  display: none;
  width: 96px;
  height: 40px;
  line-height: 40px;
  background: red;
  color: #fff;
  margin: 20px 0;
  text-align: center;
  font-family: Arial, sans-serif;
}
<section class="cont">

  <figure class="delay">
    <figcaption>Delay</figcaption>
  </figure>

  <audio class="notify" src="https://notificationsounds.com/soundfiles/8b16ebc056e613024c057be590b542eb/file-sounds-1113-unconvinced.mp3">
</audio>

  <button>Open / Close</button>

</section>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
zer00ne
  • 41,936
  • 6
  • 41
  • 68
2

Here is a working example.

function see() {
  var x = document.getElementById("deley");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
};

// === Scrip for audio play ====

$(document).ready(function(){
    $(".deley").hide();
    // if sound is currently playing, stop it and reset
    if(!$("#notify")[0].paused) {
        $("#notify")[0].pause();
        $("#notify")[0].currentTime = 0;
    }
    setTimeout(function () {
        $(".deley").show();
        $("#notify")[0].play();            
    }, 0);
});
#cont {
  width:200px;
  margin:0 auto
}

.deley {
  display:none;
  width:96px;
  height:40px;
  background:red;
  margin:20px 0;
  text-align:center;
  line-height:40px;
  font-family:Arial, sans-serif;
  color:#fff
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div id="cont">

<div class="deley" id="deley">Deley</div>

<audio id="notify">
  <source src="https://notificationsounds.com/soundfiles/8b16ebc056e613024c057be590b542eb/file-sounds-1113-unconvinced.ogg" type="audio/ogg">
  <source src="https://notificationsounds.com/soundfiles/8b16ebc056e613024c057be590b542eb/file-sounds-1113-unconvinced.mp3" type="audio/mpeg">
</audio>

<button onclick="see()">Open / Close</button>
  
</cont>
Todd Chaffee
  • 6,754
  • 32
  • 41
2

Jq object dose not have play, pause events. you will have to change it to a simple javascript to access those events. emphasized text Have a look below and i also simplified the function fot you

function see() {
   $(".deley").toggle("fast", function(){
     var player= $("#notify")[0];
    // is the player hidden then pause and reset
    if($(this).is("hidden")) {
        player.pause();
        player.currentTime = 0;
    }else
      player.play();  
    });
};
#cont {
  width:200px;
  margin:0 auto
}

.deley {
  display:none;
  width:96px;
  height:40px;
  background:red;
  margin:20px 0;
  text-align:center;
  line-height:40px;
  font-family:Arial, sans-serif;
  color:#fff
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div id="cont">

<div class="deley" id="deley">Deley</div>

<audio id="notify">
  <source src="https://notificationsounds.com/soundfiles/8b16ebc056e613024c057be590b542eb/file-sounds-1113-unconvinced.ogg" type="audio/ogg">
  <source src="https://notificationsounds.com/soundfiles/8b16ebc056e613024c057be590b542eb/file-sounds-1113-unconvinced.mp3" type="audio/mpeg">
</audio>

<button onclick="see()">Open / Close</button>
  
</cont>
Alen.Toma
  • 4,684
  • 2
  • 14
  • 31
1
$(document).ready(function(){
    $(".deley").hide();
    // if sound is currently playing, stop it and reset
    if(!$("#notify")[0].paused) {
        $("#notify")[0].pause();
        $("#notify")[0].currentTime = 0;
    }
    setTimeout(function () {
        $(".deley").show();
        $("#notify")[0].play();            
    }, 0);
});

https://stackoverflow.com/a/16093819/11343720


Or

 document.getElementById('notify').play();
Wang Liang
  • 4,244
  • 6
  • 22
  • 45