Loop Audio Specified Amount of Times Html5 or Javascript - javascript

I need to look this typing sound over and over again until my typing animation is done. I know I can use an audio editor to extend the sound but I just want to know if there is any way to do this with just html5 and javascript. I need to loop it a specific number of times then stop it.
Here is my code:
<audio autoplay>
<source src="sfx_typing.mp3" type="audio/mpeg">
<embed height="50" width="100" src="sfx_typing.mp3">
</audio>
Thanks in advance for any help.
This also should happen on page load with no delay and no plugins should be used in the audio playing.

You can play() audio again in ended event handler.

Related

Audio element won't play audio

I am building a website and there are some podcasts on it, which when user clicks play need to play. I've got some audio elements and most of them work but one in particular doesn't, even in JSFiddle.
I've been thinking about and googling stuff but I can't figure it out. When JavaScript executes the .play() command it just returns a promise that never resolves.
Here is the html:
<audio class="pid-player" href="https://www.amplifiedpodcast.com/wp-content/uploads/2019/10/01-Amplified3-Podcast_-Sean-Stein-Sm.mp3.mp3"></audio>
Here is the JavaScript:
console.log(document.querySelector('.pid-player').play());
When I open the audio href in a new tab it plays!
This is happening in Chrome and Firefox.
Any ideas appreciated!
"href" should be src like this
<audio class="pid-player" src="https://www.amplifiedpodcast.com/wp-content/uploads/2019/10/01-Amplified3-Podcast_-Sean-Stein-Sm.mp3.mp3" controls></audio>
You need to use source tag like this:
document.querySelector('.pid-player').play()
<audio class="pid-player">
<source src="https://www.amplifiedpodcast.com/wp-content/uploads/2019/10/01-Amplified3-Podcast_-Sean-Stein-Sm.mp3.mp3" type="audio/mp3">
</audio>

audio html5 with jquery to play next track

I have html5 like this
<audio controls class="audioPlayer" >
<source class="iSong" src="1.mp3" type="audio/mpeg">
<source class="iSong" src="2.mp3" type="audio/mpeg">
<source class="iSong" src="3.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
I want add jquery for button nextSong because my song always play only last one.
Adding multiple sources to an audio element does not create a playlist, it is to support different audio formats and your browser will simply play the first one it can, which is why you say only the last one is playing.
To have multiple songs you choose between you will have to write some javascript. Here is some information on audio in javascript.
It sounds like you will have to create a separate button for playing the next song, add a click event that changes the source of the audio element and plays it.

How to play mp3 on link click

There is a simple link
01. The Name of Track
How to play the mp3 file, when user clicks on link? Please help me to find some simple and effective solution.
Thank you.
Thank you for help.
I choosed this solution http://www.schillmania.com/projects/soundmanager2/demo/play-mp3-links/ as the most appropriate in my case.
Use HTML5 <audio>
<audio controls id="linkAudio">
<source src="demo.ogg" type="audio/ogg">
<source src="demo.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<script>
document.getElementById("link_id").addEventListener("click", function () {
document.getElementById("linkAudio").play();
});
</script>
Note: As audio is an HTML5 tag, it won't support old browsers, so be
sure before you use it..
Or take a look at this article
Many browsers today (like Chrome and Firefox) will not play MP3 or MP4 files, due to IP restrictions. You can either transcode your files to compatible alternatives, like Ogg, or you'll have to rely on plugins to get universal browser support. One very good option is Soundmanager, which I'm using in a project where transcoding is not an option. It uses HTML 5 playback when it can, but falls back to an invisible Flash movie if the file type is not supported by the chosen browser. It's extremely flexible, but it has a bit of a learning curve. The demos are fantastic though, and provide several types of players that you can probably just drop in to whatever your interface is.
You have several options, you could use the html5 audio tag, which would create a small player that would allow you to play the audio:
<audio controls width="100" height="100">
<source src="some.mp3" type="audio/mp3">
<!-- Fallback for older browsers -->
Your browser doesn't support html5 audio
</audio>
You could also use the embed html tag:
<embed src="some.mp3" autostart="false" id="somemp3" enablejavascript="yes">
Then run the following javascript upon clicking the link:
var snd = document.getElementById(somemp3);
snd.play();
Or you could use the embed method for browsers that don't support html5 audio, by putting the above code within the audio tags, and it will show up only if the browser doesn't recognize the audio tag
You could also use a flash audio player.
Play Audio
<audio>
<source src="demo.ogg" type="audio/ogg">
<source src="demo.mp3" type="audio/mpeg">
Your browser doesn't support the audio element.
</audio>
Using jQuery:
$("a").click(function() {
$("audio").play();
});

Embedding audio in IE(7+) with javascript capability to mute and unmute

I've been looking around for a method to embed audio onto a website, have a capability to loop and autoplay and also be able to mute and unmute the audio with javascript. I know that this is possible and very easy in html5, but I've heard that IE doesn't support html5 yet (or the audio tags perhaps).
I also need my embedded audio to work as far back as IE7. So i think that using the tags will work for all other browsers but IE, while I was hoping something like could work for IE; unfortunately, it doesn't support calls from javascript to mute and unmute - this is because I don't want any controls from the audio player to be visible; simply a custom sound button that the user can click to mute and unmute the audio. Any ideas? Seems that something like this is the most simple thing, but the hardest thing to code :/
Consider using a player that uses HTML 5 by default, but can fall back on Flash if it's not supported.
JPlayer can do that, and has a mute function.
HTML5 is your best bet.
<audio>
<source src="horse.ogg" type="audio/ogg" />
<source src="horse.mp3" type="audio/mp3" />
<!--
You can put a flash player here in case the users browser doesn't support HTML5 or any of the audio formats you have added.
-->
</audio>
the attributes you might want to specify in the audio tag are controls='controls' to enable playback options like play pause and volume controls including mute; loop='loop' to enable auto-loop; autoplay='autoplay' obviously to autoplay; and preload='auto' to load the audio on page load (you can also specify "metadata" or "none")
your player would probably look like:
<audio preload='auto' autoplay='autoplay' controls='controls' loop='loop'>
<source src="yoursound.ogg" type="audio/ogg" />
<source src="yoursound.mp3" type="audio/mp3" />
<!--
Flash player here to fall back on if the users browser doesn't support HTML5
-->
</audio>

Muting a HTML5 audio element using a custom button

What's the best way to mute a HTML5 audio element? I don't want the browser's custom controls, so I've been looking at the javascript instead. Unfortunately it just doesn't seem to work for me :( I'm undoubtedly doing something very wrong.
<audio id="background_audio" autoplay="autoplay">
<source src="static/audio/clip.ogg" type="audio/ogg" />
<source src="static/audio/clip.mp3" type="audio/mpeg" />
</audio>
mute sound
Still getting my head around this. Thanks!
This should work:
document.getElementById('background_audio').muted = true;
Demo: http://jsfiddle.net/mattball/sVwXH/ (no jQuery)

Categories

Resources