HTML5 Audio stop function
Instead of stop()
you could try with:
sound.pause();
sound.currentTime = 0;
This should have the desired effect.
How to stop audio played by audio tag of HTML5
Try this:
try
{
if(MyAudio != undefined)
{
MyAudio.pause();
}
MyAudio = new Audio("filename");
MyAudio.play();
}
catch(v)
{
//alert(v.message);
}
Javascript to stop playing sound when another starts
This is not very difficult to do if you use HTML5 which introduced the HTMLAudioElement.
Here is the minimal code for what you are trying to do:
// Let's create a soundboard module ("sb")var sb = { song: null, init: function () { sb.song = new Audio(); sb.listeners(); }, listeners: function () { $("button").click(sb.play); }, play: function (e) { sb.song.src = e.target.value; sb.song.play(); }};
$(document).ready(sb.init);
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Audio</title></head><body> <button value="https://www.gnu.org/music/FreeSWSong.ogg">Song #1</button> <button value="https://www.gnu.org/music/free-software-song-herzog.ogg">Song #2</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script></body></html>
HTML5 - how to stop audio when another audio tag starts playing?
Ok, here is how I solved it:
The addEventListener
grabs the audio beginning to play and the for
loop pauses all other audio tags.
document.addEventListener('play', function(e) {
var audios = document.getElementsByTagName('audio');
for (var i = 0, len = audios.length; i < len; i++) {
if (audios[i] != e.target) {
audios[i].pause();
}
}
}, true);
How to pause or stop an HTML5 Audio element from component.ts
Call .pause()
on the audio element.
stopAudio() {
this.audio.pause();
}
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
HTML5 Audio Element is Restarting instead of Resuming using play(), pause()
The problem is that you override audio src when you pause and play again.
Try to check if src exists and it is the same was playing before paused it.
if(audio_pbar.src !== datasrc) audio_pbar.src=datasrc;
Sound Play / Stop / Pause
If you change that:
soundPlayer = new Audio(soundName).play();
To that:
soundPlayer = new Audio(soundName);
soundPlayer.play();
Your pause will be working. The problem is that you assigned "play" function to soundPlayer. SoundPlayer isnt an Audio object now.
Instead of stop() use:
soundPlayer.pause();
soundPlayer.currentTime = 0;
It works the same I guess.
Related Topics
Is It Wrong to Change a Block Element to Inline With CSS If It Contains Another Block Element
How to Vertically Center a Container in Bootstrap
Margin-Top Push Outer Div Down
Responsively Change Div Size Keeping Aspect Ratio
How to Add a New Line in Textarea Element
Can an Option in a Select Tag Carry Multiple Values
How to Avoid Joining All Text from Nodes When Scraping
Why Can't I Do ≪Img Src="C:/Localfile.Jpg"≫
How to Add a Browser Tab Icon (Favicon) For a Website
Equal Height Children of Flex Items
Should I Use ≪I≫ Tag For Icons Instead of ≪Span≫
An Invalid Form Control With Name='' Is Not Focusable
How to Use the "Required" Attribute With a "Radio" Input Field
Base64 Encoded Images in Email Signatures