H5自动加载音乐,点击暂停、播放

html代码

    <div id="btn">
        <audio id="audio-player" ref="audio" preload="preload" autoplay="autoplay" loop="loop">
            <source src="../../mp3/kwgr11.mp3" type="audio/mpeg">
            Your browser does not support the audio tag.
        </audio>
    </div>

CSS样式

#btn{
    width: 0.9866666666666667rem;
    height: 0.9866666666666667rem;
    background: url("../../images/act/juxiantang/icon/mp3s.png") no-repeat;
    background-size:100% 100%;
    -webkit-background-size:100% 100%;
    -moz-background-size:100% 100%;
    -ms-background-size:100% 100%;
    -o-background-size:100% 100%;
    position: fixed;
    top: 2.1866666666666665rem;
    right: 0.22rem;
    z-index: 6666;
}

JS代码操控

音乐
method 判断是否为微信浏览器
let isWeixin = () => {
    let ua = window.navigator.userAgent.toLowerCase()
    return;
    /micromessenger/
        .test(ua)
}
// method 自动播放
let audioPlayer = (id) => {
    let audio = document.getElementById(id)
    if (isWeixin()) {
        document.addEventListener(
            'WeixinJSBridgeReady'
            , () => {
                audio.play()
            }, false);

// 添加 getNetworkType 的判断原因, 请看问题分析2
        if (typeof window.
                WeixinJSBridge
            ==
            "object"
            && typeof window.
                WeixinJSBridge
                .invoke ==
            "function"
        ) {
            window.
            WeixinJSBridge
                .invoke(
                    'getNetworkType'
                    , {}, () => {
                        audio.play()
                    })
        }
    } else {
        audio.play();
        let touchPlay = () => {
            audio.play();
            document.removeEventListener(
                'touchstart'
                , touchPlay, false)
        };
        if (audio.paused) {
            document.addEventListener(
                'touchstart'
                , touchPlay, false)
        }
    }
}
// 使用
audioPlayer(
    'audio-player'
);

var audio = document.getElementById("audio-player");
var btn = document.getElementById("btn");
var rotateVal = 0;

var InterVal;     // 定时器
// 设置定时器
function rotate () {
    InterVal = setInterval(function () {
        rotateVal += 4;
        // 设置旋转属性(顺时针)
        btn.style.transform = 'rotate(' + rotateVal + 'deg)';
        // 设置旋转属性(逆时针)
        //img.style.transform = 'rotate(-' + rotateVal + 'deg)'
        // 设置旋转时的动画  匀速0.1s
        btn.style.transition = '0.1s linear'
    }, 100)
}
rotate ();
btn.onclick = function () {         //点击按钮播放停止
    if (audio.paused) { //判断当前的状态是否为暂停,若是则点击播放,否则暂停
        audio.play();
        rotate ()
    }else{
        audio.pause();
        clearInterval(InterVal)
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值