Wednesday, January 29, 2014

Use Javascript To Experience An Mp3 File

Use a JavaScript function to let website visitors play an MP3.


A number of ways are possible to embed and play MP3 audio files on your Web pages. The deprecated "embed" tag in HTML, various Flash players, and Java-based players are all examples. The HTML5 standard, with its emphasis on scripting and its lack of reliance on third-party software, offers the most compatible way to embed and play MP3s on Web pages going forward. Embed the MP3 file with the HTML5 "audio" object, and then use a JavaScript function to start it playing


Instructions


1. Add the following code to the body of your HTML document:


This code embeds and loads the MP3 file, but does not play it. The value of the "src" tag contains the path to and filename of the MP3 file.


2. Add the following JavaScript code between the "head" tags of your HTML document:


function playMP3(){


document.getElementById("mp3").play();


}


This function, when called, accesses the "audio" element by its id ("mp3"), and uses the method play() to play it.


3. Add a button to the Web page to call the "playMP3" function when the user clicks it. Add the following code to the body of the HTML document:


Change the "value" attribute to give the button a different title.


4. Use the following function instead of "playMP3" to let the user pause the file after starting it:


function playpauseMP3(){


if(document.getElementById("mp3").paused){


document.getElementById("mp3").play();


}else{


document.getElementById("mp3").pause();


}


}


Change the "onClick" attribute of the "input" tag in Step 3 to match this function's name.









Related posts



    MPA files are highly compatible with both PCs and Macs.The MPA (MPEG-2) audio container type is one of the most commonly used sound files available, according to fileinfo.com. Based on the MPEG-1...
    If you run a podcast or create your own music, adding MP3s to your blog is a great way to share your work with your followers. How you post your audio depends on your blogging platform. WordPress....
    A file format tells your computer which program can play a file. Files can be given any extension, but a program will not play a file with an incorrect or unknown extension. An unknown extension i...
    Play a DVD or audio disc on your PC.By default, all DVDs and audio CDs inserted into your PC will play through the Windows Media Player program. Windows Media Player comes with all PCs running Win...
    Create an MP3 CD That Will Play in Any CD PlayerThe MP3 music file is a common format for music. MP3's can be downloaded directly onto MP3 players or burned onto a CD disc compatible with CD playe...