使用audio5.js

来源:互联网 发布:卓克 卓老板 知乎 编辑:程序博客网 时间:2024/06/08 00:32

audio5.js 网站地址:http://zohararad.github.com/audio5js/。


效果很烂,只是简单实现了功能。

基于audio5.js 和    

<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

 <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>


    <script>

        function contains(str, substr) {
            return str.indexOf(substr) >= 0;
        }


        function playorpause(e, url) {
            var _btn = $(e)[0];   //取到点击的按钮
            _btnval = $(e).val();            //取到按钮的value
            _btnClass = _btn.classList;    //取到按钮的class集合。html5新。
            if (_btnClass.contains('loaded')) {             //判断class集合中是否包含loaded
                return false;
            }
            var audio5js = new Audio5js({
                ready: function () {
                    var voiceUrl = url;
                    _this = this;
                    this.load(voiceUrl);
                    this.play();
                    _btnClass.add('loaded');
                    $(e).val("pause");


                    this.on('play', function () {             //如果执行了play方法之后触发此事件
                        console.log('play');
                    }, this);
                    this.on('pause', function () {           //暂停后触发
                        console.log('pause');
                    }, this);
                    this.on('ended', function () {             //播放结束后触发
                        $(e).val("play");
                    }, this);
                    this.on('timeupdate', function (position, duration) {   播放时间节点变化后触发。
                        durationsum = duration;          //音频总时长 秒。
                        var posiarray = position.split(':');   //当前播放的位置 mm:ss 格式。
                        var positionS = parseFloat(posiarray[0]) * 60 + parseFloat(posiarray[1]);
                        var timewidth = Math.round(positionS / duration * 100); //计算当前百分比。


                        $("#progressbar").progressbar({
                            value: timewidth                    //改变进度条中的位置
                        });
                        $("#timer").html(position + " / " + Number(duration).formatTime());
                                }, this);


                    //加载了多少百分比
                    this.on('progress', function (load_percent) {


                    }, this);
                    //error event passes error object to callback
                    this.on('error', function (error) {
                        console.log(error.message);
                    }, this);


                    _btn.addEventListener('click', function () {
                        if (contains(_btnval, 'pause')) {
                            _this.pause();
                            $(e).val("play");
                        } else {
                            _this.play();
                            $(e).val("pause");
                        }
                    }, false);


                }
            });
        }


        // 格式化秒数到时间格式
        Number.prototype.formatTime = function () {
            // 计算
            var h = 0, i = 0, s = parseInt(this);
            if (s > 60) {
                i = parseInt(s / 60);
                s = parseInt(s % 60);
                if (i > 60) {
                    h = parseInt(i / 60);
                    i = parseInt(i % 60);
                }
            }
            // 补零
            var zero = function (v) {
                return (v >> 0) < 10 ? "0" + v : v;
            };
            return [zero(i), zero(s)].join(":");
        };


       //点击进度条方法
        $(function () {
            $("#progressbar").click(function () {
                _this.seek(parseInt(durationsum * clientx / $(this).width()));
                
            })
        })


        //获取鼠标点击的浏览器位置
        function getMousePos(event) {
            var e = event || window.event;
            clientx = e.clientX;
            clienty = e.clientY;
        }
        document.onmousedown = getMousePos;
    </script>

<body>


    父亲<input class="btnplay" type="button" value="play" onclick="playorpause(this,'fuqin.mp3')" />
    <br /><br /><br />




    <br /><br /><br /><br />
    <div id="progressbar"></div>
    <div id="timer"></div>
</body>

0 0