Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Random Music Player</h1>
<p>Hit play and sit back. No skipping!!!</p>
<i>Feel free to use this in your project! No attributions required :)</i>
<br><br><br>

<div class="audio-player">
    <div class="audio-player-canvas">
        <canvas id="audio-player-canvas" width="100" height="100">
            Your browser does not support the canvas element
        </canvas>
    </div>
    <div class="audio-player-controls">
        <button class="play-button"></button>
        <div class="audio-player-info">
            <div class="audio-player-title">No song playing...</div>
            <input class="audio-player-seek" type="range" value="0" id="audio-player-seek" disabled>
        </div>
    </div>
    <audio style="display: none;" id="audio-player-tag">
        <source id="audio-src">
    </audio>
</div>
              
            
!

CSS

              
                body {
    margin: 5%; 
    background: black;
    color: white;
    font-family: monospace;
}
/* ^ unimportant ^ */

.play-button {
    border: 0;
    background: transparent;
    box-sizing: border-box;
    width: 0;
    height: 40px;

    border-color: transparent transparent transparent #fff;
    cursor: pointer;

    border-style: solid;
    border-width: 20px 0 20px 35px;
    
    display: inline-block;
}

.audio-player {
    background: #131215;
    border: #d1a0f2 10px solid;
    display: inline-block;
    height: 100px;
}

.audio-player-canvas {
    display: inline-block;
}

.audio-player-controls {
    display: inline-block;
    vertical-align: top;
}

.audio-player-controls {
    background: #131215;
    margin: 28px;
    height: 50px;
}

.audio-player-controls .play-button.paused {
    padding-bottom: 24px;
}

.audio-player-info {
    display: inline-block;
    vertical-align: top;
    font-family: monospace;
    padding-left: 10px;
    color: white;
    width: 225px;
}

.audio-player-title {
    margin-bottom: 5px;
    white-space: pre;
    overflow: hidden;
}

.audio-player-seek {
    display: inline;
}

.play-button.paused {
    border-style: double;
    border-width: 0px 0 0px 35px;
}

.play-button:hover {
    border-color: transparent transparent transparent #ddd;
}

#audio-player-canvas {
    background: #090909;
}

input[type="range"] {
    width: 225px;
}
              
            
!

JS

              
                let songs = [
    {
        "title": "Verse/Chorus/Verse",
        "src": "https://gateway.pinata.cloud/ipfs/QmNNACM4KtsFzrMYB5gkf1p39Dr2xcZN7naZE1SXRNPaZc",
        "artist": "Planning for Burial"
    },
    {
        "title": "Deep, Deep",
        "src": "https://gateway.pinata.cloud/ipfs/QmQ3XiiEPyqSTNqjMM1G7t8qfXyUjZxbAFiQpQzqH11bqM",
        "artist": "Have a Nice Life"
    },
    {
        "title": "Sept",
        "src": "https://gateway.pinata.cloud/ipfs/QmZZUyshP1BrRHKHhWRbcNuhTYHRmDd9grVZkgqFvMBdwt",
        "artist": "Sweet Trip"
    },
    {
        "title": "Humming Quietly",
        "src": "https://gateway.pinata.cloud/ipfs/QmTNZFnj97Ke3UJQPVg6jobkAfUAw12BSjK6YDfwx7Uy7v",
        "artist": "Planning for Burial"
    },
    {
        "title": "Chocolate Matter",
        "src": "https://gateway.pinata.cloud/ipfs/QmXXsnAuQYsGh6tXdPZW5ZcUMuUrKG3MLCfosTdv7dn99L",
        "artist": "Sweet Trip"
    },
    {
        "title": "Charade (Fern Prairie)",
        "src": "https://gateway.pinata.cloud/ipfs/Qmf99K3KVDDuAAKuMcRwU9ww5LrjZWGemBZDTJVfpF9Y4G",
        "artist": "Candy Claws"
    },
    {
        "title": "Spectral Bride",
        "src": "https://gateway.pinata.cloud/ipfs/QmdnaKvAkZEi8qP9tDjQUC7NDi2KDiKKWKwPuAs5ydfjsv",
        "artist": "Giles Corey"
    },
    {
        "title": "The Apartment Song",
        "src": "https://gateway.pinata.cloud/ipfs/QmWpKmiJAatgT5pUGZCPJM9Lr2vdvgaQ6iq85FdvRGi7iA",
        "artist": "Do Make Say Think"
    },
    {
        "title": "Pangaea Girls (Magic Feeling)",
        "src": "https://gateway.pinata.cloud/ipfs/QmQqX6p6pZhs1wLR9CojaNuikk1GseGxT2gWCCc6SqQxyg",
        "artist": "Candy Claws"
    },
    {
        "title": "Vine Boom Sound Effect",
        "src": "https://gateway.pinata.cloud/ipfs/QmcYhi4Yh3DNxsGuoxkdzjAH3Z9JcLsZrNCxTMMe6D8QUd",
        "artist": "Trolled"
    }
];

$(document).ready(function() {
    let btn = $(".play-button");
    btn.click(function() {
        btn.toggleClass("paused");
        buttonPressed();
        return false;
    });
});

let isPlaying = false;
let isPaused = false;
let isSpinning = false;
let art = null;
let rotation = 0;
let t = "";
let isShowingMarquee = false;
let mposition = 0;
let mdirection = true;

function buttonPressed(){
    isPlaying = !isPlaying;
    if (isPlaying) {
        if(isPaused){
            resume();
        }else{
            play();
        }
    } else {
        pause();
    }
}

function pause(){
    isSpinning = false;
    $("#audio-player-tag")[0].pause();
    isPlaying = false;
    isPaused = true;
}

function resume(){
    isSpinning = true;
    $("#audio-player-tag")[0].play();
    isPlaying = true;
    isPaused = false;
}

function play(songname){
    isPlaying = true;
    let c = $("#audio-player-canvas")[0];
    let ctx = c.getContext('2d');
    ctx.imageSmoothingEnabled = true;
    art = drawArt();
    window.requestAnimationFrame(function(){spin(ctx)});
    isSpinning = true;
    let song = {
        title: songname
    }
    while(song.title == songname){
        song = songs[Math.floor(Math.random()*songs.length)];
    }
    t = song.title + " - " + song.artist;
    $(".audio-player-title").text(t);
    if($(".audio-player-title").text().length > 24 && !isShowingMarquee){
        marquee();
        isShowingMarquee = true
    }
    let player = $("#audio-player-tag")[0];
    let seekbar = $("#audio-player-seek")[0];
    $("#audio-src").attr("src", song.src);
    seekbar.disabled = false;
    player.onloadedmetadata = () => seekbar.max = player.duration;
    seekbar.onchange = () => player.currentTime = seekbar.value;
    player.ontimeupdate = () => seekbar.value = player.currentTime;
    player.load();
    player.play();
    player.onended = function() {
        isPlaying = false;
        isPaused = false;
        isSpinning = false;
        art = null;
        rotation = 0;
        t = "Choosing new song...";
        mposition = 0;
        mdirection = true;
        setTimeout(function(){
            play(song.title);
        }, 2000);
    }
}

function spin(ctx) {
    ctx.save();
    ctx.beginPath();
    ctx.arc(50, 50, 40, 0, Math.PI * 2, true);
    ctx.closePath();
    ctx.clip();

    rotation += 5;
    ctx.translate(50, 50);
    ctx.rotate(rotation*Math.PI/180);
    ctx.translate(-50, -50);
    if(art)ctx.drawImage(art, 0, 0);
    if(rotation == 360){
        rotation = 0;
    }

    ctx.beginPath();
    ctx.arc(0, 0, 25, 0, Math.PI * 2, true);
    ctx.clip();
    ctx.closePath();
    ctx.restore();
    
    ctx.fillStyle = "#090909";
    ctx.arc(50, 50, 10, 0, Math.PI * 2, true);
    ctx.fill();
    
    ctx.beginPath();
    ctx.strokeStyle = "#fff";
    ctx.arc(50, 50, 40, 0, Math.PI * 2, true);
    ctx.stroke();
    if(isSpinning){
        window.requestAnimationFrame(function(){spin(ctx)});
    }else if(art){
        waitForResume(ctx);
    }
}

function waitForResume(ctx){
    if(isSpinning){
        window.requestAnimationFrame(function(){spin(ctx)});
    }else if(art){
        window.requestAnimationFrame(function(){waitForResume(ctx)});
    }
}

function drawArt(){
    let tempCanvas = document.createElement("canvas");
    let ctx = tempCanvas.getContext('2d');
    ctx.fillStyle = "white";
    ctx.fillRect(0,0,100,100);
    for(let i=0;i<30;i++){
        ctx.beginPath();
        ctx.fillStyle = makeColor();
        let x = Math.random()*100;
        let y = Math.random()*100;
        let x2 = Math.random()*50 *
            ((Math.floor(Math.random()*2) == 1) ? 1 : -1);
        let y2 = Math.random()*50 *
            ((Math.floor(Math.random()*2) == 1) ? 1 : -1);
        ctx.fillRect(x,y,x+x2,y+y2);
    }
    return tempCanvas;
}

function makeColor(){
    let color = "#";
    let hex = "1234567890abcdef";
    for(let i=0;i<6;i++){
        color += hex[Math.floor(Math.random()*16)];
    }
    return color;
}

function marquee(){
    let title = $(".audio-player-title");
    setTimeout(function(){
        title.text(t.substr(mposition, t.length));
        if(mdirection){mposition++;}else{mposition--;}
        if(t.length-mposition == 24 || mposition == 0){
            mdirection = !mdirection;
        }
        if(t.length > 24){
            marquee();
        }else{
            isShowingMarquee = false;
        }
    }, 
    ((mposition == t.length-25 && !mdirection) ||
         (mposition == 1 && mdirection))
         ? 2000 : 250);
}


              
            
!
999px

Console