马黑黑 发表于 2022-10-31 08:12

音频播放器:小频谱+进度条(初稿)

<style>
#papa {
        margin: auto;
        width: 740px;
        height: 460px;
        background: #666;
        box-shadow: 3px 3px 20px #000;
        display: grid;
        place-items: center;
        position: relative;
}
#mplayer {
        position: absolute;
        display: grid;
        grid-template-areas:
                'cur btnplay dur'
                'prog prog prog';
        gap: 4px;
        place-items: end center;
        font-size: 14px;
        bottom: 20px;
}
#cur { grid-area: cur; color: snow; }
#dur { grid-area: dur; color: snow; }
#btnplay {
        grid-area: btnplay;
        display: grid;
        grid-auto-flow: column;
        place-items: end center;
        gap: 0 4px;
        height: 60px;
        cursor: pointer;
}
#btnplay > span {
        background: red;
        width: 2px;
        transition: .3s;
}
#prog {
        --ww: 0px;
        grid-area: prog;
        width: 300px;
        height: 12px;
        border-radius: 6px;
        background: snow;
        opacity: .65;
        cursor: pointer;
        position: relative;
}
#prog::before {
        position: absolute;
        content: '';
        width: var(--ww);
        height: 12px;
        border-radius: 6px;
        background: snow linear-gradient(90deg, green,red);
        opacity: .65;
}

</style>

<div id="papa">
        <div id="mplayer">
                <div id="cur">00:00</div>
                <div id="btnplay"></div>
                <div id="dur">00:00</div>
                <div id="prog"></div>
        </div>
        <audio id="aud" src="https://music.163.com/song/media/outer/url?id=25935621.mp3" loop autoplay></audio>
</div>

<script>
(function() {
        for(j=0; j<20; j++) {
                let pinpu = document.createElement('span');
                pinpu.className = 'pinpu';
                pinpu.style.cssText += `height: ${Math.floor(Math.random()*50) + 10}px; background: #${Math.random().toString(16).substr(-6)};`;
                btnplay.appendChild(pinpu);
        }

        btnplay.onclick = () => aud.paused ? aud.play() : aud.pause();
        prog.onclick = (e) => aud.currentTime = aud.duration * e.offsetX / prog.offsetWidth;
        aud.addEventListener('timeupdate', () => {
                let prg = aud.currentTime * prog.offsetWidth / aud.duration < 6 ? 6 : aud.currentTime * prog.offsetWidth / aud.duration;
                prog.style.setProperty('--ww', prg + 'px');
                cur.innerText = toMin(aud.currentTime);
                dur.innerText = toMin(aud.duration);
                setPinpu();
        });

        let toMin = (val) => {
                if (!val) return '00:00';
                val = Math.floor(val);
                let min = parseInt(val / 60),
                        sec = parseFloat(val % 60);
                if (min < 10) min = '0' + min;
                if (sec < 10) sec = '0' + sec;
                return min + ':' + sec;
        };

        let setPinpu = ()=> {
                let eles = document.querySelectorAll('.pinpu');
                eles.forEach((item) => item.style.height = `${Math.floor(Math.random()*50) + 10}px`);
        };
})();
</script>

马黑黑 发表于 2022-10-31 08:12

代码
<style>
#papa {
        margin: auto;
        width: 740px;
        height: 460px;
        background: #666;
        box-shadow: 3px 3px 20px #000;
        display: grid;
        place-items: center;
        position: relative;
}
#mplayer {
        position: absolute;
        display: grid;
        grid-template-areas:
                'cur btnplay dur'
                'prog prog prog';
        gap: 4px;
        place-items: end center;
        font-size: 14px;
        bottom: 20px;
}
#cur { grid-area: cur; color: snow; }
#dur { grid-area: dur; color: snow; }
#btnplay {
        grid-area: btnplay;
        display: grid;
        grid-auto-flow: column;
        place-items: end center;
        gap: 0 4px;
        height: 60px;
        cursor: pointer;
}
#btnplay > span {
        background: red;
        width: 2px;
        transition: .3s;
}
#prog {
        --ww: 0px;
        grid-area: prog;
        width: 300px;
        height: 12px;
        border-radius: 6px;
        background: snow;
        opacity: .65;
        cursor: pointer;
        position: relative;
}
#prog::before {
        position: absolute;
        content: '';
        width: var(--ww);
        height: 12px;
        border-radius: 6px;
        background: snow linear-gradient(90deg, green,red);
        opacity: .65;
}

</style>

<div id="papa">
        <div id="mplayer">
                <div id="cur">00:00</div>
                <div id="btnplay"></div>
                <div id="dur">00:00</div>
                <div id="prog"></div>
        </div>
        <audio id="aud" src="https://music.163.com/song/media/outer/url?id=25935621.mp3" loop autoplay></audio>
</div>

<script>
(function() {
        for(j=0; j<20; j++) {
                let pinpu = document.createElement('span');
                pinpu.className = 'pinpu';
                pinpu.style.cssText += `height: ${Math.floor(Math.random()*50) + 10}px; background: #${Math.random().toString(16).substr(-6)};`;
                btnplay.appendChild(pinpu);
        }

        btnplay.onclick = () => aud.paused ? aud.play() : aud.pause();
        prog.onclick = (e) => aud.currentTime = aud.duration * e.offsetX / prog.offsetWidth;
        aud.addEventListener('timeupdate', () => {
                let prg = aud.currentTime * prog.offsetWidth / aud.duration < 6 ? 6 : aud.currentTime * prog.offsetWidth / aud.duration;
                prog.style.setProperty('--ww', prg + 'px');
                cur.innerText = toMin(aud.currentTime);
                dur.innerText = toMin(aud.duration);
                setPinpu();
        });

        let toMin = (val) => {
                if (!val) return '00:00';
                val = Math.floor(val);
                let min = parseInt(val / 60),
                        sec = parseFloat(val % 60);
                if (min < 10) min = '0' + min;
                if (sec < 10) sec = '0' + sec;
                return min + ':' + sec;
        };

        let setPinpu = ()=> {
                let eles = document.querySelectorAll('.pinpu');
                eles.forEach((item) => item.style.height = `${Math.floor(Math.random()*50) + 10}px`);
        };
})();
</script>

马黑黑 发表于 2022-10-31 08:13

为能让友友们自由修改,CSS、HTML、JS都做了格式化,所以代码行数显得多了一些

马黑黑 发表于 2022-10-31 08:28

本帖最后由 马黑黑 于 2022-10-31 08:29 编辑

频谱设定


一、 频谱父元素 #btnplay(CSS代码)

#btnplay {
      grid-area: btnplay;
      display: grid;
      grid-auto-flow: column;
      place-items: end center;
      gap: 0 4px; /* 间距,修改第二个数值 */
      height: 60px; /* 频谱最大高度 */
      cursor: pointer;
}


二、频谱个体样式 #btnplay > span(CSS代码)

#btnplay > span {
      background: red; /* 初始颜色 */
      width: 2px;/* 频谱个体宽度 */
      transition: .3s; /* 变化总时长 */
}


三、生成频谱(JS代码)
      //修改红色数据
      for(j=0; j<20; j++) {
                // 具体代码略
      }



上海朝阳 发表于 2022-10-31 08:46

代码看不懂,这首日文歌听了,适合海岸边游览的车载音乐

红影 发表于 2022-10-31 09:58

虽然暂时看不到效果,但感觉这个带频谱的播放器很漂亮{:4_187:}

醉美水芙蓉 发表于 2022-10-31 11:45

醉美水芙蓉 发表于 2022-10-31 11:47

马黑黑 发表于 2022-10-31 12:13

醉美水芙蓉 发表于 2022-10-31 11:47
设计精美,带屏谱更加漂亮!

还行

马黑黑 发表于 2022-10-31 12:13

红影 发表于 2022-10-31 09:58
虽然暂时看不到效果,但感觉这个带频谱的播放器很漂亮

不差

相约爱晚亭 发表于 2022-10-31 13:34

频谱播放器好看,喜欢!

小辣椒 发表于 2022-10-31 13:58

点击频谱音乐出来了{:4_199:}

小辣椒 发表于 2022-10-31 13:59

马黑黑 发表于 2022-10-31 08:13
为能让友友们自由修改,CSS、HTML、JS都做了格式化,所以代码行数显得多了一些

黑黑想的很周到

红影 发表于 2022-10-31 16:30

马黑黑 发表于 2022-10-31 12:13
不差

肯定的。黑黑辛苦{:4_204:}

马黑黑 发表于 2022-10-31 18:13

红影 发表于 2022-10-31 16:30
肯定的。黑黑辛苦

举手之劳

马黑黑 发表于 2022-10-31 18:13

小辣椒 发表于 2022-10-31 13:59
黑黑想的很周到

还不差

马黑黑 发表于 2022-10-31 18:13

小辣椒 发表于 2022-10-31 13:58
点击频谱音乐出来了

自动播放的吧。

马黑黑 发表于 2022-10-31 18:14

相约爱晚亭 发表于 2022-10-31 13:34
频谱播放器好看,喜欢!

还可以吧

红影 发表于 2022-10-31 19:46

马黑黑 发表于 2022-10-31 18:13
举手之劳

现在看到效果了,漂亮{:4_187:}

马黑黑 发表于 2022-10-31 20:23

红影 发表于 2022-10-31 19:46
现在看到效果了,漂亮

还行的吧
页: [1] 2
查看完整版本: 音频播放器:小频谱+进度条(初稿)