Hello coders, hope you all are doing good. I’m back with the interesting and very useful topic. Today You’ll learn how to make custom music player in javascript with cool design of radio. Earlier, you’ve learned to create a responsive educational website using HTML, CSS & Javascript.

For creating custom music player the main thing you need to know is javascript. All the functionality can be created only with the help of javascript. However, the designing part is also important but there is main role of javascript in music player. Without doing further late let’s move towards the overview of music player we are going to make today.

As you have already seen in the above image, that’s exactly we are going to make today. In this music player I have tried something different for its design. I have tried to give the looks of old vintage radio. At the top, there is a antenna and after that the main body of radio start. In the main body there are three different div; one outer layer, one div for title and featured image and one div for all the control buttons and progress bar. That’s all in the designing part. Let me remind you that this one is completely responsive that can be run on all sizes of screen smoothly and perfectly.

If you are feeling difficult to understand what exactly I’m saying, you can watch video below.

You Might Also Like

Video of  custom radio music player in javascript

As you can see in the above video, all the designs are completely responsive created with pure css. You have also seen the javascript code which plays the vital role for creating this music player. If you are beginner and feeling difficult to understand javascript code then, don’t worry I have provided well commented source code for free. Believe me, it’s really easy. You have to just go through the code carefully. To get the source code of this custom radio music player, you just need to scroll down.

Before source code, let me explain the key points of javascript used in this music player. For showing progress bar I have divided the total duration of music by current playing time and convert it into percentage. After that I have submitted the calculated percentage to the width of progress bar. When the playing time of music is change the width of progress bar also change. The function is as follows:-

function progressbar(){

    var progressed = music.currentTime*100/music.duration;

    progress.style.width = progressed + "%";

}

For creating play and pause features I’ve created two function for play and pause separately that will check whether the music is playing or not as well the image is rotating or not. If music is at pause position then you have to click on play button. When you click play button music will start playing, image will start rotating and pause button appear. The same process is done in reverse way for pause function. The functions are as follows:-

Play function

function playmusic(){

    if(playing==false && imagerotation==false){

        music.play();

        pause.style.display = "grid";

        image.style.animation = "imagerotate 4s linear infinite";

        playing = true;

        imagerotation = true;

    }

}

Pause function

function pausemusic(){

    if(playing==true && imagerotation==true){

        music.pause();

        pause.style.display = "none";

        image.style.animation = "none";

        playing = false;

        imagerotation = false;

    }

}

For forward function I’ve added five seconds in the current playing time and for backward I’ve subtracted five seconds in the current playing time. The codes are as follows:-

Forward function

function forwardMusic(){

    var time = music.currentTime;

    music.currentTime = time + 5;

}

Backward function

backward.addEventListener('click', backwardMusic);

function backwardMusic(){

    var time = music.currentTime;

    music.currentTime = time - 5;

}


Custom Radio Music Player in Javascript [Source Codes]

To create this radio music player you have to create three files i.e. a HTML file, a CSS file and a Javascript file. After creating these files just paste the below code in your files. Folder structure is given below.

At first, create one HTML file with the name of index.html and paste the below html codes in your file. Remember, you have create a file with .html extension.

index.html

Second, create a file with the name of style.css and paste the below codes in your css file. Remember you have to create file with .css extension.

style.css

At last, create a file with the name of script.js and paste the below codes in you javascript file. Remember, you have to create a file with .js extension.

script.js

That’s all you have to do. Congratulation, you have successfully created your own custom radio music player in javascript. If you have faced any problems while creating this music player, drop your problems and queries below in the comment. I will help you to solve your problem. That’s all for today I’ll be back with next stunning blog later.

#keeplearning #keepcoding

Thank You :)

Post a Comment

أحدث أقدم