現(xiàn)效果如下:
由于我這邊不需要其他按鈕,就沒寫
數(shù)據(jù)是由后臺提供,在這做了個(gè)小列子
后臺代碼
public ActionResult MusicPlayer(int musicId=0) { MusicPlayerModel model = new MusicPlayerModel(); switch (musicId) { default: model.MusicName = "Believe-動(dòng)畫《海賊王》"; model.CoverImg = "/Content/Music/Believe-cover.jpg"; model.FileUrl = "/Content/Music/Believe.mp3"; model.MusicStartSecond = 0; model.MusicEndSecond = 227; break; case 1: model.MusicName = "夢回還-動(dòng)畫《狐妖小紅娘》"; 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> <p class="img-wapper"> <img src="@Model.CoverImg" /> </p> <p id="player-wapper" class="player-wapper"> <p class="cover-wapper"> <img src="@Model.CoverImg" /> <p class="play"> <i></i> </p> </p> <p class="info-wapper"> <p class="title">@Model.MusicName</p> <audio id="audio-player" src="@Model.FileUrl" src="@Model.FileUrl" data-start="@Model.MusicStartSecond" data-end="@Model.MusicEndSecond" ></audio> <p class="audio-progress"> <span id="start-time" class="start-time">00:00</span> <p id="progress" class="progress"> <span id="player-progress-bar" class="bar"> <i></i> </span> </p> <span id="end-time" class="end-time">00:00</span> </p> </p> </p>
Js
;$(function () { var $playerWapper = $("#player-wapper"), $audioPlay = $("#audio-player"), startSecond = $audioPlay.data("start"),//默認(rèn)開始時(shí)間(秒) endSecond = $audioPlay.data("end"),//默認(rèn)結(jié)束時(shí)間(秒) playSecond = startSecond,//已播放時(shí)間(秒) surplusSecond = endSecond,//剩余時(shí)間(秒) audoiTimer = null; LoadingTime(); Playing(); //通過點(diǎn)擊進(jìn)度條實(shí)現(xiàn)播放跳轉(zhuǎn) $(".progress").click(function (e) { //獲取當(dāng)前鼠標(biāo)相對進(jìn)度條的X坐標(biāo) var positionX = e.pageX - $(this).offset().left; var width = $(this).width(); //進(jìn)度條的X坐標(biāo)/進(jìn)度條寬度獲取播放占比 var progess = (positionX / width).toFixed(2); $("#player-progress-bar").css("width", progess); //播放占比*總時(shí)間獲取已播放時(shí)間 playSecond = parseInt(progess * endSecond); surplusSecond = endSecond - playSecond; //播放器跳轉(zhuǎn)/跟新播放時(shí)間 $audioPlay[0].currentTime = playSecond; LoadingTime(); }) //播放按鈕點(diǎn)擊事件 $(".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); //每個(gè)1秒執(zhí)行一次 } //暫停播放 function Pause() { $playerWapper.removeClass("playing"); $playerWapper.addClass("pause"); window.clearInterval(audoiTimer); $audioPlay[0].pause(); } //加載時(shí)間和進(jìn)度條 function LoadingTime() { $("#start-time").html(secondToTime(playSecond)); $("#end-time").html(secondToTime(surplusSecond)); $("#player-progress-bar").css("width", Percentage(playSecond, endSecond)); } //計(jì)算百分比 function Percentage(second1, second2) { return (Math.round(second1 / second2 * 10000) / 100+ "%");// 小數(shù)點(diǎn)后兩位百分比 } //時(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)文章:
詳細(xì)解讀Vue如何配置打包工具
使用Parcel如何打包
詳細(xì)解答vue的變化對組件有什么影響?
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com