您好,登錄后才能下訂單哦!
這篇文章主要介紹js實現音樂播放器的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
效果圖:
可播放暫停繼續播放,進度條可拖動,時間展示,聲音調節
完整代碼(直接拿來用)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>audio功能開發</title> <style> * { margin:0; padding:0 } .music-range { width:350px; height:10px; background:#2386e4; border-radius:5px; -webkit-appearance:none; margin:0 auto; cursor:pointer } .music-range::-webkit-slider-thumb { width:15px; height:15px; background:#fff; border:1px solid #f18900; cursor:pointer; border-radius:5px; -webkit-appearance:none } a { text-align:center } </style> </head> <body> <div class="music-all"> <audio class='music-audio' controls> <source src="http://www.jq22.com/demo/jqueryaudio201903252328/jq22.mp3" type="audio/mpeg"> </audio> <div>總時間 <span class='music-max'></span> </div> <div>時間 <span class='music-cur'></span> </div> <input class='music-range' type="range" min=0 max=100 value=0 /> <div>音量<span class="music-voice">1</span></div> <button class='music-play'>播放</button> <button class='music-no'>停止</button> <button class="music-btnd">聲小</button> <button class="music-btne">聲大</button> <div class="music-animation"><span class="music-span"></span></div> <div class="music-list"></div> </div> <script src="js/jquery-1.10.2.js"></script> <script src="js/audio.js" type="text/javascript" charset="utf-8"></script> <script> var audios = document.getElementsByClassName("music-audio")[0]; var vol = audios.volume; audios.controls = false; $('.music-play').on('click', function () { audios.play(); var duration = audios.duration; $('.music-max').html(timeleng(duration)); $(".music-animation").addClass("play-an"); $(".music-range").attr({ 'max': duration }); function timer() { var t = parseInt(Math.round(audios.currentTime)); $(".music-range").val(t); $('.music-cur').text(timeleng(t)); t = parseInt(audios.currentTime); if (t < duration) { setTimeout(timer, 1000); } else { clearTimeout(timer); } } timer(); }); $('.music-no').on('click', function () { audios.pause(); $(".music-animation").removeClass("play-an"); }) audios.onended = function () { $(".music-animation").removeClass("play-an") }; $('.music-btnd').click(function () { vol = vol > 0 ? (vol * 10 - 1) / 10 : 0; audios.volume = vol; console.log(vol) $(".music-voice").html(vol) }) $('.music-btne').click(function () { vol = vol < 1 ? (vol * 10 + 1) / 10 : 1; audios.volume = vol; console.log(vol) $(".music-voice").html(vol) }) $(".music-range").on('change', function () { audios.currentTime = this.value; console.log(this.value) $(".music-range").val(this.value); }); function timeleng(time) { var m = 0, s = 0, ms = '00', ss = '00'; time = Math.floor(time % 3600); m = Math.floor(time / 60); s = Math.floor(time % 60); ss = s < 10 ? '0' + s : s + ''; ms = m < 10 ? '0' + m : m + ''; return ms + ":" + ss; } $(".music-qd").on("click", function () { var nameid = $(".input-text").val(); console.log(nameid) $.ajax({ type: "get", dataType: 'jsonp', success: function (d) { console.log(d) }, error: function (d) { console.log(d); } }); }) </script> </body> </html>
以上是js實現音樂播放器的方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。