Introduction
Hello geeks. Today I will show you how to make your own personal music player but this time only using the functionalities of HTML and JavaScript.
Use the following procedure, here we go:
1. Open a text editor.
2. Enter this code and save the file with a name, like Music Player, having an extension of .htm or .html.
- <html>
- <head>
- <title>Music Player </title>
- </head>
- <body>
- <audio controls="”controls”">
- <source src=”music .mp3” type=”audio/mp3”>
- <source src=”music .ogg” type=”audio/ogg”>
- </audio>
- </body>
- </html>
3. Copy and save some music files to the same destination that you have already saved that code page.
4. Now in the
<audio> tag, make these modifications, since we will use JavaScript in the code. The modifications are:
delete controls=”controls”
Along with attribute and value.
5. So, due to these modifications the controls will not be visible, you can provide these controls to the user using the JavaScript functionality of
click events.
6. Add these
button codes between the tags:
- <button onclick=”playMusic()”>Play</button>
- <br/>
- <button onclick=”pauseMusic()”>Pause</button>
- <br/>
- <button onclick=”stopMusic()”>Stop</button>
- <br/>
This code will make the buttons appear on the screen. Now what we need to do next is to provide the functioning of the click event.
7. Now we need to create some JavaScript
functions. Follow these sub steps:
7.1. Create a script between the
<head> …. </head> tags as in the following:
- <head>
- <title>Music Player </title>
- <script>
- </script>
- </head>
7.2. Now will make a function between the <script> …. </script> tags as in the following:
- <head>
- <title>Music Player </title>
- <script>
- function playMusic() {
- }
- </script>
- </head>
7.3. Now modify the two other options as in the following:
- <head>
- <title>Music Player </title>
- <script>
- function playMusic() {
- }
- function pauseMusic() {
- }
- function stopMusic() {
- }
- </script>
- </head>
7.4. Now we will make a major task between these script tags, perform these tasks precisely, since JavaScript is case sensitive:
- <head>
- <title>Music Player </title>
- <script>
- Var player;
- window.onload =function()
- {
- player = document.getElementById(‘player’);
- }
- function playMusic()
- {
- player.play();
- }
- function pauseMusic()
- {
- Player.pause();
- }
- function stopMusic()
- {
- Player.stop();
- }
- </script>
- </head>
8. Now for the last modification, you need to do that in general HTML code as in the following:
put <audio id=”player”>