Reputation: 109
I want to stop audio playing, but not only pause it, but I also want if I reopen it, it starts at the first, not the middle that I stopped it.
Upvotes: 0
Views: 235
Reputation: 1901
What did you already try ? This should do what you're looking for.
function stop(el){
el.pause(); // Stop playing
el.currentTime = 0; // Reset time
}
Here is a working example with an id
specified instead of an element
function stop(elementId) {
var el = document.getElementById(elementId);
el.pause(); // Stop playing
el.currentTime = 0; // Reset time
}
<audio id="myAudio" controls>
<source src="http://ccrma.stanford.edu/~jos/mp3/viola.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<br/><br/>
<button onclick="stop('myAudio')">Stop playing</button>
Upvotes: 2
Reputation: 337560
You cannot access media controls through a jQuery object. As such you need to call the audio related methods on the Element object itself.
The use of play()
and pause()
is obvious, but to set the time index back to the start of the file you will need to use currentTime
:
var audio = $('audio')[0]; // alternative: $('audio').get(0);
audio.pause();
audio.currentTime = 0;
audio.play();
Upvotes: 1