Make a Custom Music Player in JavaScript


In this course, I will show you how to Make a Custom Music Player with a neumorphic design in HTML, CSS, and JavaScript.




Image Assets






HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Music Player</title>
    <link rel="stylesheet" href="main.css">

</head>
<body>
    <div class="container">
        <div class="wrapper">
            <h1>Play your favorite music</h1>
            <img src="https://i.pinimg.com/originals/b4/75/00/b4750046d94fed05d00dd849aa5f0ab7.jpg">
            <input id="range" class="level" type="range" value="0" min="0">
            <div class="buttons">
                <button id="pre"><img src="rewind.png"></button>
                <button id="play"><img id="play_img" src="play.png"></button>
                <button id="next"><img src="fast-forward.png"></button>
            </div>
            
        </div>
       
    </div>
    <script src="main.js"></script>
</body>
</html>

CSS

@import url('https://fonts.googleapis.com/css2?family=Nunito&family=Nunito+Sans&display=swap');
*{
    margin0;
    padding0;
    outline0;
}
body{
    background-color#E1E6EC;
}
.container{
    positionabsolute;
    left50%;
    top50%;
    transformtranslate(-50%,-50%);
    padding6px;
    width300px;
    border-radius8px;
    background-color#E1E6EC;
    box-shadow5px 5px 11px #A2B1C6 , -5px -5px 11px  #fff  ;
    text-aligncenter;
    font-family'Nunito'sans-serif;
    
}
.wrapper{
    positionrelative;
}
.container img{
    width80%;
   
}
.container h1{
    font-size1.5rem;
    margin12px 0 12px 0;
}
.buttons{
    displayinline-block;
    width100%;
    padding0;
    margin0;
    text-aligncenter;
}
.buttons button{
    positionrelative;
    width50px;
    height50px;
    padding16px;
    border-radius90px;
    background-color#E1E6EC;
    text-alignleft;
    margin10px 9px;
    bordernone;
    box-shadow2px 2px 5px #A2B1C6 , -2px -2px 11px  #fff  ;
    /*background:linear-gradient(-45deg,#a2b1c6,#fff);*/
}
.buttons button:active{
    background#E1E6EC;
    box-shadowinset 2px 2px 5px #A2B1C6 , -2px -2px 11px  #fff  ;
}
.buttons button img{
    positionabsolute;
    left50%;
    top50%;
    transformtranslate(-50%,-50%);
    width42%;
    height42%;
}

.level{
    width80%;
    -webkit-appearancenone;
    appearancenone;
    background#E1E6EC;
    box-shadow2px 2px 5px #A2B1C6 , -2px -2px 11px  #fff  ;
    border-radius50px;
    margin12px 0;
}
.level::-webkit-slider-thumb{
    -webkit-appearancenone;
    background-color#A2B1C6;
    box-shadow2px 2px 5px #A2B1C6 , -2px -2px 5px  #fff  ;
    width15px/* Set a specific slider handle width */
    height15px/* Slider handle height */
    border-radius90px;
}
.level::-moz-range-thumb{
    width50px;
    height50px;
}

JavaScript

music_name = "music.mp3"
let play_btn = document.querySelector("#play");
let prev_btn = document.querySelector("#pre");
let next_btn = document.querySelector("#next");
let range = document.querySelector("#range");
let play_img = document.querySelector("#play_img")
let total_time = 0;
let currentTime = 0;
let isPlaying = false;
let song = new Audio();
window.onload = playSong;

function playSong(){
    song.src = music_name;
    console.log(song)
    
    
    play_btn.addEventListener('click',function(){
        if(!isPlaying){
            song.play();
            isPlaying = true;
            total_time = song.duration;
            range.max = total_time;
            play_img.src = "pause.png";
        }else{
            song.pause();
            isPlaying = false;
            play_img.src = "play.png";
        }
       song.addEventListener('ended',function(){
            song.currentTime = 0
            song.pause();
            isPlaying = false;
            range.value = 0;
            play_img.src = "play.png";
        })
        song.addEventListener('timeupdate',function(){
            range.value = song.currentTime;
        })
        range.addEventListener('change',function(){
            song.currentTime = range.value;
        })
       
    })
}
Make a Custom Music Player in JavaScript Make a Custom Music Player in JavaScript Reviewed by Medics on April 09, 2020 Rating: 5

5 comments:

  1. kindly add playlist with controls.

    ReplyDelete
  2. Three are usually cheap Ralph Lauren available for sale each and every time you wish to buy. custom lapel pins

    ReplyDelete
  3. A great website with interesting and unique material what else would you need. 相親

    ReplyDelete

-->
Powered by Blogger.