首页投稿(暂停使用,暂停投稿)程序员饥人谷技术博客

使用HTML5 audio标签自制音乐播放器

2017-06-10  本文已影响0人  柏龙

声明:此文章版权归饥人谷和本人所有,转载须得到本人同意,项目为开源项目,不能用于商业应用,仅供学习。

预览地址: https://boloog.github.io/music/
GitHub地址: https://github.com/boloog/music/

相关技能

实现的功能

audio 标签使用

播放和暂停代码

_Music.prototype.playMusic = function(){
    var _this = this;
    this.play.on('click', function(){
        if (_this.audio.paused) {
            _this.audio.play();
            $(this).html('');
        } else {
            _this.audio.pause();
            $(this).html('')
        }
    });
}

音乐进度条代码

_Music.prototype.volumeDrag = function(){
    var _this = this;
    this.btn.on('mousedown', function(){
        _this.clicking = true;
        _this.audio.pause();
    })
    this.btn.on('mouseup', function(){
        _this.clicking = false;
        _this.audio.play();
    })
    this.progress.on('mousemove click', function(e){
        if(_this.clicking || e.type === 'click'){
            var len = $(this).width(),
                left = e.pageX - $(this).offset().left,
                volume = left / len;
            if(volume <= 1 || volume >= 0){
                _this.audio.currentTime =  volume * _this.audio.duration;
                _this.progressLine.css('width', volume *100 +'%');
            }
        }
    });
}

歌词添加代码

_Music.prototype.readyLyric = function(lyric){
    this.lyricBox.empty();
    var lyricLength = 0;
    var html = '<div class="lyric-ani" data-height="20">';
    lyric.forEach(function(element,index) {
        var ele = element[1] === undefined ? '^_^歌词错误^_^' :  element[1];
        html += '<p class="lyric-line" data-id="'+index+'" data-time="' + element[0] + '"> ' +  ele + ' </p>';
        lyricLength++;
    });
    html += '</div>';
    this.lyricBox.append(html);
    this.onTimeUpdate(lyricLength);
}

代码还有很多就不一一添加了,觉得还行的话可以点下喜欢(也可以在我的GitHub给个Star),你的喜欢和Star是我继续创作的动力,非常感谢!!!

有什么疑问或问题,欢迎大家指出。

上一篇 下一篇

猜你喜欢

热点阅读