Plain HTML5 audio player : duration and start - javascript

It's very simple to design an audio player with HTML5 :
<audio controls>
<source src="http://picosong.com/cdn/27590d7ce366627ec0d20d06ec2bbe1c.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
I would like to set a starting point (ex: 1'26") and a duration (ex: 10 seconds), i.e. the player shouldn't display the whole audio file, but only these 10 seconds.
How to do set start point and duration with HTML5's <audio> with plain HTML5 or HTML5+JS? (i.e. no big third party JS library)

You can set the playback range in audio files url.
Like this #t=[starttime][,endtime]
mdn article
Example:
<audio controls>
<source src="http://picosong.com/cdn/27590d7ce366627ec0d20d06ec2bbe1c.mp3#t=120,130" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

Related

How to make MP3 file play in the background of HTML/CSS/JS page?

Would like to have an MP3 track play in the background immediately after a HTML/CSS/JavaScript page is loaded.
So far, only able to have a player with controls at the bottom of the page:
Bottom of webpage
using the following code:
<audio id="bg_music" class="audio" controls loop>
<source src="audio/silent-night.mp3"
type="audio/mp3">
Your browser does not support the audio element.
</audio>
Would like to find a way to have the MP3 file play automatically after the browser (Chrome) loads the webpage.
Thanks,
Benjamin
You need to add an attributed autoplay, and it will be done. You may additionaly provide value autoplay=true or autoplay=false, but still if you don't give it, it will automatically consider it to be true if you just add the attribute.
<audio id="bg_music" class="audio" autoplay controls loop>
<source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mp3" />
Your browser does not support the audio element.
</audio>

Problem to play audio instantly for large size audio file

I am trying to add audio in my code
<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
and above code is working fine.. but i am facing problem if audio is quite big. i have to wait for couples of second to listen the audio,
is there is any alternative for the same so we can play audio instantly without any wait?
I found one solution , see if it works :
You can use the following property of audio element to solve your problem!!
Preload hinting:
You can optionally use the preload attribute to give the browser a hint as to whether, and how, it should preload the audio file when the page loads. Preloading the audio file means it can play instantly when the user hits the “play” button, which is a nicer experience for the user.
<audio preload="auto">
<source src="WhiteChristmas.mp3">
<source src="WhiteChristmas.ogg">
</audio>
Refer this site:
https://www.elated.com/html5-audio/
you could try to check the spelling...i to had this problem but when i checked and corrected the spelling of the file,it loaded instantly.The code is:
<audio controls
Your browser does not support the audio element.

How to pause audio tag when screen off or start second one

I'm trying to pause audio when mobile screen off or run a second audio tag.
this is sample example of my html code
<audio controls>
<source src="audio1.mp3">
Does not support the audio element.
</audio>
<audio controls>
<source src="audio2.mp3">
Does not support the audio element.
</audio>
Unfortunately I don't know how can I make EventListener to check the above cases.

play mp3 in tag audio by javascript

i've the following html:
<audio id="carteSoudCtrl">
<source src="http://soundjax.com/reddo/17350%5EClick02.mp3" type="audio/mpeg">
<source src="http://soundjax.com/reddo/90754%5EWINNER.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
with this javascript:
$('#carteSoudCtrl')[1].play();
why is not working on chrome? i'm missing something? $('#carteSoudCtrl')[0].play(); is working.
thank you.
https://jsfiddle.net/o8mjvty5/
See the updated demo here Link
The HTML <audio> element is used to embed sound content in documents.
It may contain one or more audio sources, represented using the src attribute or the element; the browser will choose the most suitable one.
So all different files inside an <audio> tag must corresponding to same file in different format and browser will play the format which is most suitable to play.
If you need to play the second file we need to create second tag and then insert the <source src="http://soundjax.com/reddo/90754%5EWINNER.mp3" type="audio/mpeg"> tag inside the second audio tag.
<audio id="carteSoudCtrl">
<source src="http://soundjax.com/reddo/17350%5EClick02.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<audio id="carteSoudCtrl1">
<source src="http://soundjax.com/reddo/90754%5EWINNER.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
Jquery file :
//if you want to play first file
$('#carteSoudCtrl')[0].play();
//if you want to play second file
$('#carteSoudCtrl1')[0].play();

How to pause/mute S3 video which is embedded in an iframe?

We are hosting a careersites for customers, one of our customers provided the data which contains video from S3 which is embedded in an iframe.
Problem is the video is getting played automatically when page is loaded. Customer is asking to pause or mute the video on page load.
Is there any way we can pause the video?
Unless there is a special reason you need to use an iframe, the usual way to handle video now is with the HTML5 video tag.
This has an attribute 'autoplay' which if present will start the video automatically, and if absent will not start the video:
http://www.w3schools.com/tags/att_video_autoplay.asp
The following will autoplay (if the devices supports this - some mobile device do not to save on data charges):
<video controls autoplay>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
and this will not:
<video controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>

Categories