使用H5audio特性实现简单的音乐播放器

本文详细介绍了如何利用HTML5的audio元素构建一个基本的音乐播放器。内容包括audio标签的属性设置、事件监听、控制播放暂停、音量调节等功能的实现,帮助开发者掌握H5audio在网页音乐播放中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!Doctype html>
<html>
    <head>
        <title>成兮音乐浏览器</title>
        <meta http-equiv="Keywords" content="music" />
        <meta http-equiv="author" content="chengxi" />
        <style>
            #mysele{
                width: 150px ;
            }
        </style>
        <script>
            //定义属于window的全局变量
            var musics = [
                "D://music/告白气球.mp3" ,
                "D://music/轩辕剑.mp3" ,
                "D://music/守婚如玉.mp3" ,
            ] ;
            //记录当前播放的音乐所处的musics索引
            var index = 0 ;
            //记录选择的播放方式,默认为sequence顺序播放
            var playtype = "sequence" ;
            window.onload = function() {
                var typesele = document.getElementById("mysele") ;
                //select下拉列表的值改变时触发该事件
                typesele.onchange = function() {
                    window.playtype = typesele.value ;
                } ;
                var player = document.getElementById("easyaudio") ;
                player.src = musics[index] ;
                //当前音乐播放完成后触发该事件
                player.onended = function() {
                    //随机播放
                    if(playtype == "random") {
                        //生成一个随机数
                        index = Math.floor(Math.random() * musics.length) ;
                    }
                    //顺序播放
                    else if(playtype == "sequence") {
                        index = index++ % musics.length ;
                    }
                    //单曲循环则最终index不变
                    player.src = musics[index] ;
                    //改变完成后自动播放
                    player.play() ;
                } ;
            } ;
    </head>
    <body>
        <select id="mysele">
            <option value="sequence" selected>顺序播放</option>
            <option value="random">随机播放</option>
            <option value="loop">单曲循环</option>
        </select>
        <audio id="easyaudio" controls>
            您的浏览器不支持audio元素
        </audio>
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值