這篇文章將為大家詳細講解有關(guān)基于JavaScript實現(xiàn)音頻播放功能的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了邛崍免費建站歡迎大家使用!
現(xiàn)效果如下:
由于我這邊不需要其他按鈕,就沒寫
數(shù)據(jù)是由后臺提供,在這做了個小列子
后臺代碼
public ActionResult MusicPlayer(int musicId=0) { MusicPlayerModel model = new MusicPlayerModel(); switch (musicId) { default: model.MusicName = "Believe-動畫《海賊王》"; model.CoverImg = "/Content/Music/Believe-cover.jpg"; model.FileUrl = "/Content/Music/Believe.mp3"; model.MusicStartSecond = 0; model.MusicEndSecond = 227; break; case 1: model.MusicName = "夢回還-動畫《狐妖小紅娘》"; model.CoverImg = "/Content/Music/夢回還-cover.jpg"; model.FileUrl = "/Content/Music/夢回還.mp3"; model.MusicStartSecond = 0; model.MusicEndSecond = 250; break; } return View(model); }
頁面代碼
@using FunctionTest.Web.Areas.Function.Models; @model MusicPlayerModel @{ ViewBag.Title = "MusicPlayer"; Layout = "~/Areas/Function/Views/Shared/_FunctionLayout.cshtml"; } <link href="~/Assets/Function/MusicPlayer/musicPlayer.css" rel="external nofollow" rel="stylesheet" /> <script src="~/Assets/Function/MusicPlayer/musicPlayer.js"></script> <div class="img-wapper"> <img src="@Model.CoverImg" /> </div> <div id="player-wapper" class="player-wapper"> <div class="cover-wapper"> <img src="@Model.CoverImg" /> <div class="play"> <i></i> </div> </div> <div class="info-wapper"> <div class="title">@Model.MusicName</div> <audio id="audio-player" src="@Model.FileUrl" data-src="@Model.FileUrl" data-start="@Model.MusicStartSecond" data-end="@Model.MusicEndSecond" ></audio> <div class="audio-progress"> <span id="start-time" class="start-time">00:00</span> <div id="progress" class="progress"> <span id="player-progress-bar" class="bar"> <i></i> </span> </div> <span id="end-time" class="end-time">00:00</span> </div> </div> </div>
Js
;$(function () { var $playerWapper = $("#player-wapper"), $audioPlay = $("#audio-player"), startSecond = $audioPlay.data("start"),//默認開始時間(秒) endSecond = $audioPlay.data("end"),//默認結(jié)束時間(秒) playSecond = startSecond,//已播放時間(秒) surplusSecond = endSecond,//剩余時間(秒) audoiTimer = null; LoadingTime(); Playing(); //通過點擊進度條實現(xiàn)播放跳轉(zhuǎn) $(".progress").click(function (e) { //獲取當前鼠標相對進度條的X坐標 var positionX = e.pageX - $(this).offset().left; var width = $(this).width(); //進度條的X坐標/進度條寬度獲取播放占比 var progess = (positionX / width).toFixed(2); $("#player-progress-bar").css("width", progess); //播放占比*總時間獲取已播放時間 playSecond = parseInt(progess * endSecond); surplusSecond = endSecond - playSecond; //播放器跳轉(zhuǎn)/跟新播放時間 $audioPlay[0].currentTime = playSecond; LoadingTime(); }) //播放按鈕點擊事件 $(".play").click(function () { if ($playerWapper.hasClass("playing")) { Pause(); } else { Playing(); } }) //開始/繼續(xù)播放 function Playing() { $playerWapper.addClass("playing"); $playerWapper.removeClass("pause"); $audioPlay[0].play(); audoiTimer = setInterval(function () { playSecond++; surplusSecond--; LoadingTime(); if (surplusSecond <= 0) { playSecond = startSecond; surplusSecond = endSecond; Pause(); } }, 1000); //每個1秒執(zhí)行一次 } //暫停播放 function Pause() { $playerWapper.removeClass("playing"); $playerWapper.addClass("pause"); window.clearInterval(audoiTimer); $audioPlay[0].pause(); } //加載時間和進度條 function LoadingTime() { $("#start-time").html(secondToTime(playSecond)); $("#end-time").html(secondToTime(surplusSecond)); $("#player-progress-bar").css("width", Percentage(playSecond, endSecond)); } //計算百分比 function Percentage(second1, second2) { return (Math.round(second1 / second2 * 10000) / 100+ "%");// 小數(shù)點后兩位百分比 } //時間轉(zhuǎn)換,將秒轉(zhuǎn)為00:00:00格式 function secondToTime(s) { var t; if (s > -1) { var hour = Math.floor(s / 3600); var min = Math.floor(s / 60) % 60; var sec = s % 60; if (hour < 10) { t = '0' + hour + ":"; } else { t = hour + ":"; } if (min < 10) { t += "0"; } t += min + ":"; if (sec < 10) { t += "0"; } t += sec; } return t; } })
關(guān)于“基于JavaScript實現(xiàn)音頻播放功能的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
網(wǎng)站名稱:基于JavaScript實現(xiàn)音頻播放功能的方法
鏈接URL:http://jinyejixie.com/article40/gdpseo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、品牌網(wǎng)站設(shè)計、微信小程序、網(wǎng)站營銷、網(wǎng)站設(shè)計、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)