全国服务热线:4008-888-888

技术知识

HTML5自定mp3播发器源代码

audio目标

src适配.ogg .wav .mp3

<audio controls src='data/imooc.wav'></audio>

width autoplay loop muted静音

<audio controls src='data/imooc.wav' autoplay loop width='500' height='500' muted></audio>

播发play()

 var myAudio = new Audio();
        myAudio.src = 'data/imooc.wav';
        myAudio.play();
        btn.onclick = function(){
            myAudio.play();
        };

中止pause()

pauseNode.onclick = function(){
                myAudio.pause();
            };

当今播发的時间currentTime

声频总时长duration

   //回到声频的总长度
            myAudio.addEventListener('canplay',function(){
                durationNode.innerHTML = myAudio.duration;
            });
            //升级当今播发的時间
            setInterval(function(){
                currentNode.innerHTML = myAudio.currentTime;
            },100);

声频源currentSrc

var myAudio = new Audio();
        myAudio.src = 'data/imooc.mp3';
        console.log(myAudio.currentSrc);

loop循环系统

myAudio.loop = true;

声频播发完毕ended

myAudio.addEventListener('ended',function(){
            console.log('声频播发完毕');
            console.log(myAudio.ended)
        });

再次载入

 

 loadBtn.onclick = function(){
            myAudio.load();
        };

自动跳转到新的播发部位seeked / seeking

 myAudio.addEventListener('seeked',function(){
            console.log('seeked');
        });
        myAudio.addEventListener('seeking',function(){
            console.log('seeking');
            sekingNum++;
            seekingNum.innerHTML = sekingNum;
        });

playbackRate设定当今播发速率

 

   myAudio.playbackRate = '15';
        console.log(myAudio.playbackRate)

全屏requestFullScreen

 btnScreen.onclick = function(){
            myAudio.webkitRequestFullScreen();
        }

loop 循环系统

 

 myAudio.loop = true;

volumechange声音更改

 

  myAudio.addEventListener('volumechange',function(){
            console.log('声频的响声更改了')
        });

timeupdate声频正在播发情况

 myAudio.addEventListener('timeupdate',function(){
            console.log('声频正在播发中...')
        })

自定mp3播发器

放图

<!doctype html> 
<html> 
<head> 
    <meta charset="utf⑻"> 
    <title></title> 
    <style type="text/css">
*{margin: 0;padding: 0;list-style: none;}        
.outerNode{width: 505px;height: 406px;position: absolute;left: 50%;top: 50%;margin: ⑵04px 0 0 ⑵53.5px;border: 1px solid #a6a18d;border-radius:8px;box-shadow: 0 0 16px #a6a18d; }
.innerNode{width: 503px;height: 405px;border-top:1px solid #e1d1b9;border-left:1px solid #ceccbf;border-radius: 8px;overflow: hidden;border-right:1px solid #ceccbf;   }
.topNode{width: 100%;height: 198px;border-bottom: 1px solid #787463;background: url(music/pic/fmt01.jpg) center center;background-size:cover; transition:.7s;position: relative;}
.lineNode{
    width: 100%;height: 46px;border-top: 1px solid #f9f7ee;border-bottom: 1px solid #a29d8a;background: url(musicimage/linebg.jpg) repeat-x; 
}
.progressNode{width: 440px;height: 18px;float: left;margin:13px 0 0 28px;background: url(musicimage/progressbg.jpg) repeat-x;position: relative; }
.progressNode .progressleft{
    width: 7px;height: 100%;position: absolute;left: 0;
    background: url(musicimage/leftNode.jpg);
}
.progressNode .progressright{
    width: 7px;height: 100%;position: absolute;right: 0;
    background: url(musicimage/rightNode.jpg);
}
.bottomNode{
    width: 100%;height: 157px;border-top: 1px solid #a29d8a;
    background: url(musicimage/bottombg.jpg) repeat-x;position: relative;
}
.lastNode{width: 75px;height: 74px;position: absolute;background: url(musicimage/lastBg.png) no-repeat;left: 118px;top: 39px;cursor: pointer;}
.playNode{width: 95px;height: 94px;position: absolute;background: url(musicimage/playNode.png) no-repeat;left: 202px;top: 29px;cursor: pointer;}
.nextNode{width: 75px;height: 74px;background: url(musicimage/rightbg.png) no-repeat;position: absolute;left: 306px;top: 39px;cursor: pointer;}
.volumeNode{width: 37px;height: 32px;background: url(musicimage/volume.png) no-repeat; position: absolute;right: 43px;top: 58px;cursor: pointer;}
.no_volumeNode{width: 37px;height: 32px;background: url(musicimage/no_volume.png) no-repeat; position: absolute;right: 43px;top: 58px;cursor: pointer;}
.trueLine{position: absolute;left: 3px;top: 2px;height: 12px;width: 0%;
    background: url(musicimage/green_bg.png) repeat-x;border-radius: 6px; 
    border-right: 1px solid #787463;
}
.musicName{color: white;position: absolute;bottom: 2px;left: 5px;}
    </style>
</head>
<body>
    <!-- outerNode 最外层的元素 -->
    <div class='outerNode'>
        <!-- innerNode 里层元素 -->
        <div class='innerNode'>
            <!-- topNode 封面图元素 -->
            <div class='topNode'>
                <!-- 歌曲名字 -->
                <div class='musicName'></div>
            </div>
            <!-- lineNode 进度条元素 -->
            <div class='lineNode'>
                <!-- 进度条-->
                <div class='progressNode'>
                    <div class='progressleft'></div>
                    <div class='progressright'></div>
                    <!-- 真实的进度条 -->
                    <div class='trueLine'></div>
                </div>
            </div>
            <!-- bottomNode 室内空间元素 -->
            <div class='bottomNode'>
                <!-- lastNode 上1曲的按钮-->
                <div class='lastNode'></div>
                <!-- playNode 播发中止的按钮 -->
                <div class='playNode'></div>
                <!-- nextNode 下1曲的按钮 -->
                <div class='nextNode'></div>
                <!-- volumeNode 静音或非静音的按钮-->
                <div class='volumeNode'></div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        //播发中止的按钮
        //playBln 操纵播发中止的布尔运算值
        var playBtn = document.querySelector('.playNode'),
        playBln = true,
        //操纵响声的按钮
        //volumeBln 操纵响声的布尔运算值
        volumeNode = document.querySelector('.volumeNode'),
        volumeBln = true,
        //进度条的挑选器
        trueLine = document.querySelector('.trueLine'),
        //进度条外层的元素
        progressNode = document.querySelector('.progressNode'),
        //最外层元素
        outerNode = document.querySelector('.outerNode'),
        //挑选1下封面情况
        topNode = document.querySelector('.topNode'),
        //下1首歌的按钮
        nextNode = document.querySelector('.nextNode'),
        //上1首歌的按钮
        lastNode = document.querySelector('.lastNode'),
        //歌曲名字
        musicName = document.querySelector('.musicName');
        //给播发器加上js
        //建立audio目标
        var myAudio = new Audio();
        //给audio目标1个 src
        //全部的数据信息存在数字能量数组里边
        let allMusic = [{
            'MusicSrc':'music/mus/AcousticGuitar1.mp3',
            'MusicPic':'music/pic/fmt01.jpg',
            'MusicName':'AcousticGuitar1'
        },{
            'MusicSrc':'music/mus/AmazingGrace.mp3',
            'MusicPic':'music/pic/fmt02.png',
            'MusicName':'AmazingGrace'
        },{
            'MusicSrc':'music/mus/FeelsGood2B.mp3',
            'MusicPic':'music/pic/fmt03.jpg',
            'MusicName':'FeelsGood2B'
        },{
            'MusicSrc':'music/mus/FunBusyIntro.mp3',
            'MusicPic':'music/pic/fmt04.jpg',
            'MusicName':'FunBusyIntro'
        },{
            'MusicSrc':'music/mus/GreenDaze.mp3',
            'MusicPic':'music/pic/fmt05.jpg',
            'MusicName':'GreenDaze'
        },{
            'MusicSrc':'music/mus/Limosine.mp3',
            'MusicPic':'music/pic/fmt06.jpg',
            'MusicName':'Limosine'
        }],Index = 0;
        myAudio.src = allMusic[Index].MusicSrc;
        //给封面取值
        topNode.style.backgroundImage = 'url('+allMusic[Index].MusicPic+')';
        //给歌曲名字
        musicName.innerHTML = allMusic[Index].MusicName;
        //谷歌访问器不容许立即play
        //myAudio.play();
        //播发中止的恶性事件
        playBtn.onclick = function(){
            //myAudio.play();
            playBln = !playBln;
            if(playBln == false){
                myAudio.play();
            }
            else{
                myAudio.pause();
            }
        };
        //响声的恶性事件
        volumeNode.onclick = function(){
            volumeBln = !volumeBln;
            if(volumeBln == false){
                myAudio.volume = 0;
                this.className = 'no_volumeNode';
            }
            else{
                myAudio.volume = 1;
                this.className = 'volumeNode';
            }
        };
        //播发时 进度条的长度操纵测算
        myAudio.addEventListener('timeupdate',function(){
            trueLine.style.width = myAudio.currentTime / myAudio.duration * 100 + '%';
        });
        //点一下progressNode元素 让进度条立即抵达这个部位
        progressNode.onclick = function(e){
            var ev = e || event;
            //优化算法 便是 算出 点一下的部位 在 外层进度条的 是多少像素
            //必须1个电脑鼠标座标点 减去 外层元素的 offsetLeft 和 最外层元素的offsetLeft 
            // 320秒 *  0.50 = 160秒
            myAudio.currentTime = myAudio.duration * ((ev.clientX - (this.offsetLeft + outerNode.offsetLeft))/this.offsetWidth);
            trueLine.style.width = ((ev.clientX - (this.offsetLeft + outerNode.offsetLeft))/this.offsetWidth) * 100 + '%';
        };
        //下1首歌的恶性事件
        nextNode.onclick = function(){
            Index ++;
            if(Index == allMusic.length){
                Index = 0;
            }
            MusicPlayFn();
        };
        //歌曲播发的涵数
        function MusicPlayFn(){
            myAudio.src = allMusic[Index].MusicSrc;
            myAudio.currentTime = 0;
            trueLine.style.width = '0%';
            if(playBln == false){
                myAudio.play();
            }
            else{
                myAudio.pause();
            }
            //给封面取值
            topNode.style.backgroundImage = 'url('+allMusic[Index].MusicPic+')';
            //给歌曲名字
            musicName.innerHTML = allMusic[Index].MusicName;
        }
        //上1首歌的点一下恶性事件
        lastNode.onclick = function(){
            Index --;
            if(Index == ⑴){
                Index = allMusic.length⑴;
            }
            MusicPlayFn();
        };
    </script>
</body>
</html>

总结

以上所述是网编给大伙儿详细介绍的HTML5自定mp3播发器源代码,期待对大伙儿有一定的协助,假如大伙儿有任何疑惑请给我留言,网编会立即回应大伙儿的。在此也十分谢谢大伙儿对脚本制作之家网站的适用!
假如你感觉本文对你有协助,欢迎转载,烦请注明出处,感谢!



在线客服

关闭

客户服务热线
4008-888-888


点击这里给我发消息 在线客服

点击这里给我发消息 在线客服