Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play/Pause Button HTML5 Audio

I'm trying to get HTML5 Audio to play/pause in one button. How would I possibly go around doing this? So the play button switches to the pause icon which is font awesome 'fa fa-pause' The code is here:

<audio id="myTune">
<source src="http://96.47.236.72:8364/;">
</audio>
<div class="btn-group btn-group-xs">
<a href="javascript:void(0)" class="btn btn-default" data-toggle="tooltip" title="Preview"     onclick="document.getElementById('myTune').play()"><i class="fa fa-play"></i></a>

Thank you!

like image 392
screamingchimps Avatar asked Jun 30 '14 20:06

screamingchimps


People also ask

How do you make a play pause button for audio in HTML?

The HTML DOM Audio pause() Method is used to pause the currently playing audio. To use the audio pause() method, one must use the controls property to display the audio controls such as play, pause, volume, etc, attached on the audio.

How do I pause audio in HTML?

The pause() method halts (pauses) the currently playing audio. Tip: This method is often used together with the play() method. Tip: Use the controls property to display audio controls (like play, pause, seeking, volume, etc, attached on the audio).

How do I create a pause button in HTML?

HTML Audio/Video DOM pause() MethodThe pause() method halts (pauses) the currently playing audio or video. Tip: Use the play() method to start playing the current audio/video.

What does code control do audio?

HTML Audio - How It WorksThe controls attribute adds audio controls, like play, pause, and volume. The <source> element allows you to specify alternative audio files which the browser may choose from.


2 Answers

You can put an id to the <i> tag and assign the fa fa-pause class when change of state:

<a href="javascript:void(0)" class="btn btn-default" data-toggle="tooltip" title="Preview" onclick="aud_play_pause()"><i id="stateicon" class="fa fa-play"></i></a>

<script>
  function aud_play_pause() {
    var myAudio = document.getElementById("myTune");
    if (myAudio.paused) {
      $('#stateicon').removeClass('fa fa-play');
      $('#stateicon').addClass('fa fa-pause');
      myAudio.play();
    } else {
      $('#stateicon').removeClass('fa fa-pause');
      $('#stateicon').addClass('fa fa-play');
      myAudio.pause();
   }
 }

Hope this helps

like image 186
martinezjc Avatar answered Sep 21 '22 17:09

martinezjc


Give this a whirl:

function aud_play_pause() {
  var myAudio = document.getElementById("myTune");
  if (myAudio.paused) {
    myAudio.play();
  } else {
    myAudio.pause();
  }
}
<audio id="myTune" src="http://www.rachelgallen.com/HappyBirthday.mp3"></audio>


<button type="button"  onclick="aud_play_pause()">Play/Pause</button>
like image 27
Rachel Gallen Avatar answered Sep 20 '22 17:09

Rachel Gallen