Muting a HTML5 audio element using a custom button - javascript

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)

Related

How i can play audio in html?

<audio controls="controls" preload="auto">
<source src="src/audio/blog_audio/music_file.ogg" type="audio/ogg" />
Your browser does not support the audio element.
<source src="src/audio/blog_audio/music_file.mp3" type="audio/mp3" />
Your browser does not support the audio element.
<source src="src/audio/blog_audio/music_file.aac" type="audio/aac" />
Your browser does not support the audio element.
</audio>
I searched a lot here for a solution to the problem of playing the audio in the html5 file not working, and I did not find a solution to my problem, so I want to repeat the question in another form: the audio file works on the device or local host, but after uploading to the server it does not work.
I think the code is written correctly and I don't know how to solve it
I modified the mp3 audio file type to type="audio/mpeg"
This is the console error
https://imgur.com/a/GUS48j2
Please help to solve the problem
Thank you all
This is not from your code. The problem appear because you don't set the correct permissions for the file, this is why you get a 403 error and you can't play audio files on your page.
(I know my answer is a little bit blur, hope someone else could help you better ...)

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>

Won't stop download music on localhost on the embed element of audio

I almost made it so I can have background music but when I go to my localhost it just downloads the music instead of playing it but in the full path one it plays it in the background so how can I prevent this from happening so it won't download in localhost?
Here is the code for it and the file name is actually "Sound.mp3" as well.
<embed src="Sound.mp3" autostart="true" loop="true"width="2" height="0" controls>
Any ideas of what do add to make it just play and not download?
I am actually working on a page that looks pretty cool and wanted to add background music cause it is actually an "under construction" page and wanted to make it look cooler with background music.
The prefered way of adding audio in Html5 is to use the audio element.You can achieve them by using the following codes,
This code will play the Sound.mp3 file with the controllers displayed for the user. So the users can control the background music. With the autoplay attribute, the music will start automatically.
<audio controls="controls" autoplay="autoplay"><source src="Sound.mp3" type="audio/mpeg" /></audio>
If you don't wanna show the controls, you can simply remove the controls attribute. As follows,
<audio autoplay="autoplay"><source src="Sound.mp3" type="audio/mpeg" /></audio>
But the song will autoplay because the autoplay attribute is added.
Unfortunately, not all browsers support mp3, so it's a good idea to include multiple versions of the file in different formats. As follows,
<audio autoplay="autoplay">
<source src="Sound.mp3" type="audio/mpeg" />
<source src="Sound.wav" type="audio/wav" />
<source src="Sound.ogg" type="audio/ogg" />
<p>This content is displayed by browsers that don't recognize the audio element.</p>
</audio>
Hope it helped. Good luck with your project. :)
More Attributes
loop - Automatically loop (keep playing over and over).
Use Tag to attach the attach the audio file to the html file.
If your audio file is Sound.mp3 then include it in <audio> tag.
<audio controls>
<source src="Sound.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
or use the below code
<audio src="Sound.mp3"> </audio>

Easiest way to include audio into website [duplicate]

This question already has answers here:
How to play audio?
(22 answers)
Closed 6 years ago.
Im looking to include some audio into a webpage which will be automatically played on command when some action is done. What is the easiest way to do this without having any physical play/pause buttons on the screen. If so, it it possible to do this just through javascript?
(I'm a beginner so please explain a little more in detail if you can!)
Thanks in Advance!
You can look into the HTML 5 audio tag
Omit the controls attribute to remove the physical play/pause. And add autoplay to make the sound play once it's loaded
Just add an audio tag in your html.. and link to where your audio file is:
<!DOCTYPE html>
<html>
<body>
<audio style="display:none" autoplay="autoplay" >
<source src="horse.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
</body>
</html>
use either mp3 or ogg files.
http://www.w3schools.com/html/html5_audio.asp
use html5 audio tag, with autoplay attribute, like this:
https://jsfiddle.net/grassog/odL69p59/
Use audio tag
<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Play audio
</audio>

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>

Categories

Resources