Automatically play music files on your website when a page loads
- Html Play Audio On Image Click
- Html Play Audio On Image Click Check
- Html Play Audio On Image Click Test
- Html Play Audio On Image Click Download
Definition and Usage. The play method starts playing the current audio. Tip: This method is often used together with the pause method. Tip: Use the controls property to display audio controls (like play, pause, seeking, volume, etc, attached on the audio). Audio controls autoplay Your browser does not support the audio element. So let’s do it with HTML5, which can play sound through its audio element (Firefox 3.5+, Chrome 3+, Opera 10.5+, Safari 4+, IE 9+). To get as much browser support as we can, we’ll do it like this with both an MP3 source (WebKit and IE) and an OGG source (Firefox and Opera). Learn how to quickly insert an audio file into your website using just 3 lines of HTML5 code.Code this live in your browser - no software needed - https://bi.
by Christopher Heng, thesitewizard.com
If you code your web pages using a text editor (as opposed to using a visual web editor likeDreamweaver),you may be wondering how you might go about adding background music to your web pages. While this is not a questionI encounter often at thesitewizard.com, I do get the occasional music aficionado sending me this query, hence this article.
Issues Surrounding Background Music
Before you start, you should be aware thatbackground music that automaticallystarts playing when a web page is loaded may not be appreciated by a large number of your visitors. Some of them, when greetedwith the sudden blaring of music from their speakers, may immediately hit the BACK button of their browsers. This may occur evenif you're playing a piece of music that you think is well loved by everyone: remember, there are people who surf the Internet inpublic libraries, at work, or in the dead of the night when others are asleep. Others may already have their favourite('favorite' in US English)piece of music playing on their computer speakers, and your auto-playing music file will only cause them to be annoyed.
Even if you are satisfied that your website has the type of target audience that will enjoyyour background music, there are alternatives to automatically playing music that you mightwant to consider. For example, giving visitors a link which they can click to play musicwould allow you to showcase your music while remaining sensitive to your visitors' preferences.Instructions on how to accomplish this, as well as how to implement autoplaying music, are given below.
Playing Music Only When a Link is Clicked
If you have a music file (such as an MP3 file) that you want played when a visitorclicks a link, put HTML code like the following on your page:
Replace the 'yourmusicfile.mp3' with the appropriate filename. Note that the above HTML codeworks for '.wav' and '.mid' files as well (or anything else for that matter).
How to Embed Background Music on Your Web Site
Update: For a modern treatment on how to include music on your website, please readHow to Play Musicor Audio on a Website with HTML (HTML5) instead.
This rest of this article (below) is obsolete. It was written at a time when Netscape 4 and Internet Explorer 4were the browsers used by the majority of people. ('What is Netscape?' you say. My point precisely.)It even talks about a type of music file (midi) that you rarely see on the Internet any more. It isleft here mainly for historical interest. You can safely skip directly to theupdatedversion here.
The problem with embedding background music is that the different browsers out there havetheir own methods of implementing embedded music files.
For all versions of Netscape, as well as Internet Explorer ('IE') 3.0 and above, you can use the following code:
width='2' height='0'>
</embed>
The width and height attribute given above causes the player to be invisible. If you do notwant it to be invisible, you can specify your own dimensions to suit your site decor.
For Opera and all IE versions, the following code works:
As you probably have noticed, IE 3.0 and above support both methods, so you cannot simply putboth those tags into your web document in the hope of supporting all browsers. It willwork on Netscape and early versions of IE, but the newer versions of IE will recognizeboth tags, leading to problems when IE tries to load the music file twice.
The workaround that I've seen on some sites, that seems to work for me, is to enclose theBGSOUND tag inside NOEMBED tags, thus preventing IE from interpreting the second tag.
width='2' height='0'>
</embed>
<noembed>
<bgsound src='yourmusicfile.mid' loop='infinite'>
</noembed>
This code appears to be compatible with all versions of IE, Netscape and Opera.
Fixing Your Web Server's MIME Types
If, after putting the above code on your site and uploading your music file, your music doesnot automatically play in IE, Netscape or Opera, it is possible that your web server is notsending the browser the correct MIME type. In order for the browser to know how to handlethe file, your web server needs to be configured to send the correct information about the file.For example, for MIDI files, the web server should send the browser a 'Content-Type' header of'audio/midi'.
You can find out if your server has been correctly configured by starting up Netscape,invoking the 'View' menu and selecting the 'Page Info' item on that menu. (I'm referring toversion 4.7x of Netscape - the item may be labelled differently on other versionsof Netscape.) The MIME type of your music file will be displayed (among other things).
If the MIME type is incorrect, contact your web host to have them fix it. If your website runson an Apache web server, and your host has configured it to allow you to override its settingsusing a .htaccess file, you can set up the MIME type yourself. For example, if you haveMIDI files with a file extension of '.midi', you might add the following line to your.htaccess file:
If you don't have any existing .htaccess file, you can simply create one using anASCII text editor and upload it to the top directory of your website. Remember,Microsoft Word and Wordpad are *not* ASCII text editors. Also note that this method onlyworks for Apache servers configured to allow .htaccess overrides. If this is not the casefor you (for example, if your site runs on a Windows machine running IIS), you will probablyhave to get your web host to fix the problem.
Browser Plug-in Problems
Sometimes you may encounter a situation where the embedded code works for you but not forothers using the same version of the same browser.
This situation may occur when the person has installed some other software that has replacedthe default browser plug-in to handle the music file type. For example, various multimediaplayers (like QuickTime) replace the browser's default plugins when they are installed.The replacement plugins may not function in quite the same way as the default plugin for thebrowser, leading to your code not working as expected.
Html Play Audio On Image Click
There's probably little you can do to prevent this from happening, short of posting noticeseverywhere on your site telling people not to install such-and-such a software, whichis of course ludicrous. If you insist on putting auto-playing sound files on your web page,my recommendation is that you ensure that your page makes sense whether or not those soundsare played on your visitor's machine. That is, don't auto-play a sound file that providesvital information that the visitor needs to understand your web page - unless of course you alsoprovide a clickable link on the page that allows him to hear the information should his browsernot autoplay the sound file.
Like many other aspects of coding for the web, putting music on the web requires acertain amount of defensive coding. Even then, there will probably be a percentage of visitorswho will not be able to view/hear your site the way you intended.
Copyright 2002-2018 by Christopher Heng. All rights reserved.
Get more free tips and articles like this,on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/.
Do you find this article useful? You can learn of new articles and scripts that are published onthesitewizard.comby subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds athttps://www.thesitewizard.com/thesitewizard.xml.You can read more about how to subscribe toRSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.
It will appear on your page as:
thesitewizard™, thefreecountry™ and HowToHaven™ are trademarks of Christopher Heng.
This page was last updated on 20 April 2018.
Introduced with HTML5, the <audio>
tag defines a sound and is used to embed audio content such as music or other streams in HTML documents.
As of 2020, <audio>
is widely supported among modern browsers. Internet Explorer 8 doesn’t support it, as well as earlier versions.
<audio>
can be used to play sound files in the following formats:
.mp3
: Supported by all modern browsers..wav
: Not supported by Internet Explorer..ogg
: Not supported by Internet Explorer and Safari.
Using <audio> to Insert an Audio Element on Your Website
Here is the most basic use of the HTML <audio>
tag: On this example it loads a .mp3
file from your webserver and plays it.
Notice the autoplay
attribute which is used to play audio files automatically. That being said, playing sounds automatically on a web page should be avoided as this is extremely annoying for your visitors, who have no way of stoping the sound playback.
Loop an Audio File
If you want an audio file to play over and over again, you can add the loop
attribute to your audio element.
Display Browser Controls
Instead of playing sounds automatically, it is possible to let the browser display controls such as volume, play/pause, etc to the user.
This can be done easily, simply by adding the controls
attribute to the tag.
Multiple File Formats
<audio>
lets you specify different formats of a same audio file. This is especially useful if you wish to play a file in .ogg
format, which is not supported by both Internet Explorer and Safari.
Specify MIME Types
Html Play Audio On Image Click Check
When using different file formats, it is a good practice to specify the MIME type of each file in order to help the browser localize the file it supports.
It can be done easily, using the type
attribute on the source
element.
Fallback For Old Browsers
All modern browsers support <audio>
. It is however possible to notify people who are using outdated browsers that the <audio>
tag isn’t supported.
As shown below, you can simply embed any message of your choice within the <audio>
tag. If the visitor’s browser doesn’t support HTML audio, a message will be displayed instead of the audio player.
Buffer Files
When playing large files, it is a good idea to buffer those files in order to provide visitors a smooth listening experience.
To do so, you can use the preload
attribute. It accept 3 values: none
(If you don’t want the file to be buffered), auto
(If you want the browser to buffer the file, and metadata
(To buffer only metadata when page loads).
Control HTML5 <audio> with JavaScript
Controling a HTML audio player with JavaScript is pretty easy. The following example shows how you can build a rudimentary audio player with basic controls (Play, Pause, Volume Up, Volume Down) using HTML and JavaScript.
Styling the HTML Audio Tag Using CSS
As of 2019, CSS styling options for the HTML audio
tag are quite limited. The CSS properties that can be used are: width
, box-shadow
, border-radius
and transform
.
You can experiment with the small demo that I’ve created on CodePen, or just have an overview of the possibilities by having a look at the code below.
Html Play Audio On Image Click Test
If you are looking to create your own HTML audio player, the best option would be to leave off the control
attribute and implement your own controls using JavaScript.
Advanced Audio Player Using MediaElement.js
As shown above, CSS styling options for <audio>
are very limited. Fortunately, an amazing open-source media framework named MediaElement.js allows you to have much more control on your audio elements.
The above example showcases how MediaElement.js allows developers to easily give a custom look and feel to their HTML audio players. You can view this example live here.
If you are looking to build custom audio (or video) players, MediaElement.js is definitely a tool to consider.
Html Play Audio On Image Click Download
Frequently Asked Questions
What Is Audio Tag In HTML?
The HTML audio tag (<audio>
) has been introduced in HTML5, and represents an audio element within a HTML page. It is used to embed sound files into a web page.
Which HTML Tags Can be Used Within <audio>?
No other HTML tags than source
, used to specify the path of an audio file, can be used within <audio>
and </audio>
. Any text within the tag will be displayed if the visitor’s browser does not support HTML audio.