电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

原生JS實現音樂播放器的示例代碼

瀏覽:123日期:2024-04-04 17:10:48

本文主要介紹了原生JS實現音樂播放器的示例代碼,分享給大家,具體如下:

效果圖

原生JS實現音樂播放器的示例代碼

音樂播放器 播放控制 播放進度條控制 歌詞顯示及高亮 播放模式設置播放器屬性歸類

按照播放器的功能劃分,對播放器的屬性和DOM元素歸類,實現同一功能的元素和屬性保存在同一對象中,便于管理和操作

const control = { //存放播放器控制 play: document.querySelector(’#myplay’), ... index: 2,//當前播放歌曲序號 ...}const audioFile = { //存放歌曲文件及相關信息 file: document.getElementsByTagName(’audio’)[0], currentTime: 0, duration: 0,}const lyric = { // 歌詞顯示欄配置 ele: null, totalLyricRows: 0, currentRows: 0, rowsHeight: 0,}const modeControl = { //播放模式 mode: [’順序’, ’隨機’, ’單曲’], index: 0}const songInfo = { // 存放歌曲信息的DOM容器 name: document.querySelector(’.song-name’), ...}播放控制

功能:控制音樂的播放和暫停,上一首,下一首,播放完成及相應圖標修改audio所用API:audio.play() 和 audio.pause()和audio ended事件

// 音樂的播放和暫停,上一首,下一首控制control.play.addEventListener(’click’,()=>{ control.isPlay = !control.isPlay; playerHandle();} );control.prev.addEventListener(’click’, prevHandle);control.next.addEventListener(’click’, nextHandle);audioFile.file.addEventListener(’ended’, nextHandle);function playerHandle() { const play = control.play; control.isPlay ? audioFile.file.play() : audioFile.file.pause(); if (control.isPlay) { //音樂播放,更改圖標及開啟播放動畫 play.classList.remove(’songStop’); play.classList.add(’songStart’); control.albumCover.classList.add(’albumRotate’); control.albumCover.style.animationPlayState = ’running’; } else { //音樂暫停,更改圖標及暫停播放動畫 ... }}function prevHandle() { // 根據播放模式重新加載歌曲 const modeIndex = modeControl.index; const songListLens = songList.length; if (modeIndex == 0) {//順序播放 let index = --control.index; index == -1 ? (index = songListLens - 1) : index; control.index = index % songListLens; } else if (modeIndex == 1) {//隨機播放 const randomNum = Math.random() * (songListLens - 1); control.index = Math.round(randomNum); } else if (modeIndex == 2) {//單曲 } reload(songList);}function nextHandle() { const modeIndex = modeControl.index; const songListLens = songList.length; if (modeIndex == 0) {//順序播放 control.index = ++control.index % songListLens; } else if (modeIndex == 1) {//隨機播放 const randomNum = Math.random() * (songListLens - 1); control.index = Math.round(randomNum); } else if (modeIndex == 2) {//單曲 } reload(songList);}播放進度條控制

功能:實時更新播放進度,點擊進度條調整歌曲播放進度audio所用API:audio timeupdate事件,audio.currentTime

// 播放進度實時更新audioFile.file.addEventListener(’timeupdate’, lyricAndProgressMove);// 通過拖拽調整進度control.progressDot.addEventListener(’click’, adjustProgressByDrag);// 通過點擊調整進度control.progressWrap.addEventListener(’click’, adjustProgressByClick);

播放進度實時更新:通過修改相應DOM元素的位置或者寬度進行修改

function lyricAndProgressMove() { const audio = audioFile.file; const controlIndex = control.index; // 歌曲信息初始化 const songLyricItem = document.getElementsByClassName(’song-lyric-item’); if (songLyricItem.length == 0) return; let currentTime = audioFile.currentTime = Math.round(audio.currentTime); let duration = audioFile.duration = Math.round(audio.duration); //進度條移動 const progressWrapWidth = control.progressWrap.offsetWidth; const currentBarPOS = currentTime / duration * 100; control.progressBar.style.width = `${currentBarPOS.toFixed(2)}%`; const currentDotPOS = Math.round(currentTime / duration * progressWrapWidth); control.progressDot.style.left = `${currentDotPOS}px`; songInfo.currentTimeSpan.innerText = formatTime(currentTime);}

拖拽調整進度:通過拖拽移動進度條,并且同步更新歌曲播放進度

function adjustProgressByDrag() { const fragBox = control.progressDot; const progressWrap = control.progressWrap drag(fragBox, progressWrap)}function drag(fragBox, wrap) { const wrapWidth = wrap.offsetWidth; const wrapLeft = getOffsetLeft(wrap); function dragMove(e) { let disX = e.pageX - wrapLeft; changeProgressBarPos(disX, wrapWidth) } fragBox.addEventListener(’mousedown’, () => { //拖拽操作 //點擊放大方便操作 fragBox.style.width = `14px`;fragBox.style.height = `14px`;fragBox.style.top = `-7px`; document.addEventListener(’mousemove’, dragMove); document.addEventListener(’mouseup’, () => { document.removeEventListener(’mousemove’, dragMove); fragBox.style.width = `10px`;fragBox.style.height = `10px`;fragBox.style.top = `-4px`; }) });}function changeProgressBarPos(disX, wrapWidth) { //進度條狀態更新 const audio = audioFile.file const duration = audioFile.duration let dotPos let barPos if (disX < 0) { dotPos = -4 barPos = 0 audio.currentTime = 0 } else if (disX > 0 && disX < wrapWidth) { dotPos = disX barPos = 100 * (disX / wrapWidth) audio.currentTime = duration * (disX / wrapWidth) } else { dotPos = wrapWidth - 4 barPos = 100 audio.currentTime = duration } control.progressDot.style.left = `${dotPos}px` control.progressBar.style.width = `${barPos}%`}

點擊進度條調整:通過點擊進度條,并且同步更新歌曲播放進度

function adjustProgressByClick(e) { const wrap = control.progressWrap; const wrapWidth = wrap.offsetWidth; const wrapLeft = getOffsetLeft(wrap); const disX = e.pageX - wrapLeft; changeProgressBarPos(disX, wrapWidth)}歌詞顯示及高亮

功能:根據播放進度,實時更新歌詞顯示,并高亮當前歌詞(通過添加樣式)audio所用API:audio timeupdate事件,audio.currentTime

// 歌詞顯示實時更新audioFile.file.addEventListener(’timeupdate’, lyricAndProgressMove);function lyricAndProgressMove() { const audio = audioFile.file; const controlIndex = control.index; const songLyricItem = document.getElementsByClassName(’song-lyric-item’); if (songLyricItem.length == 0) return; let currentTime = audioFile.currentTime = Math.round(audio.currentTime); let duration = audioFile.duration = Math.round(audio.duration); let totalLyricRows = lyric.totalLyricRows = songLyricItem.length; let LyricEle = lyric.ele = songLyricItem[0]; let rowsHeight = lyric.rowsHeight = LyricEle && LyricEle.offsetHeight; //歌詞移動 lrcs[controlIndex].lyric.forEach((item, index) => { if (currentTime === item.time) { lyric.currentRows = index; songLyricItem[index].classList.add(’song-lyric-item-active’); index > 0 && songLyricItem[index - 1].classList.remove(’song-lyric-item-active’); if (index > 5 && index < totalLyricRows - 5) {songInfo.lyricWrap.scrollTo(0, `${rowsHeight * (index - 5)}`) } } })}播放模式設置

功能:點擊跳轉播放模式,并修改相應圖標audio所用API:無

// 播放模式設置control.mode.addEventListener(’click’, changePlayMode);function changePlayMode() { modeControl.index = ++modeControl.index % 3; const mode = control.mode; modeControl.index === 0 ? mode.setAttribute('class', 'playerIcon songCycleOrder') : modeControl.index === 1 ? mode.setAttribute('class', 'playerIcon songCycleRandom ') : mode.setAttribute('class', 'playerIcon songCycleOnly')}

項目預覽

代碼地址:https://github.com/hcm083214/audio-player

到此這篇關于原生JS實現音樂播放器的示例代碼的文章就介紹到這了,更多相關JS 音樂播放器內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 对照品_中药对照品_标准品_对照药材_「格利普」高纯中药标准品厂家-成都格利普生物科技有限公司 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 家庭教育吧-在线家庭教育平台,专注青少年家庭教育 | 雪花制冰机(实验室雪花制冰机)百科| 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 许昌奥仕达自动化设备有限公司 | 坏男孩影院-提供最新电影_动漫_综艺_电视剧_迅雷免费电影最新观看 | 水平垂直燃烧试验仪-灼热丝试验仪-漏电起痕试验仪-针焰试验仪-塑料材料燃烧检测设备-IP防水试验机 | ★店家乐|服装销售管理软件|服装店收银系统|内衣店鞋店进销存软件|连锁店管理软件|收银软件手机版|会员管理系统-手机版,云版,App | 24位ADC|8位MCU-芯易德科技有限公司| 恒温油槽-恒温水槽-低温恒温槽厂家-宁波科麦仪器有限公司 | 铝镁锰板厂家_进口钛锌板_铝镁锰波浪板_铝镁锰墙面板_铝镁锰屋面-杭州军晟金属建筑材料 | 广州中央空调回收,二手中央空调回收,旧空调回收,制冷设备回收,冷气机组回收公司-广州益夫制冷设备回收公司 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 心得体会网_心得体会格式范文模板 | 防堵吹扫装置-防堵风压测量装置-电动操作显示器-兴洲仪器 | 高压绝缘垫-红色配电房绝缘垫-绿色高压绝缘地毯-上海苏海电气 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 箱式破碎机_移动方箱式破碎机/价格/厂家_【华盛铭重工】 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 英语词典_成语词典_日语词典_法语词典_在线词典网 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | CCC验厂-家用电器|服务器CCC认证咨询-奥测世纪 | 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 水压力传感器_数字压力传感器|佛山一众传感仪器有限公司|首页 | 头条搜索极速版下载安装免费新版,头条搜索极速版邀请码怎么填写? - 欧远全 | 锻造液压机,粉末冶金,拉伸,坩埚成型液压机定制生产厂家-山东威力重工官方网站 | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 | 杭州中央空调维修_冷却塔/新风机柜/热水器/锅炉除垢清洗_除垢剂_风机盘管_冷凝器清洗-杭州亿诺能源有限公司 | 异噻唑啉酮-均三嗪-三丹油-1227-中北杀菌剂厂家 | 防水套管-柔性防水套管-刚性防水套管-上海执品管件有限公司 | 密度电子天平-内校-外校电子天平-沈阳龙腾电子有限公司 | 冲锋衣滑雪服厂家-冲锋衣定制工厂-滑雪服加工厂-广东睿牛户外(S-GERT) | 石家庄装修设计_室内家装设计_别墅装饰装修公司-石家庄金舍装饰官网 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 众品家具网-家具品牌招商_家具代理加盟_家具门户的首选网络媒体。 | 北京开源多邦科技发展有限公司官网| 加气混凝土砌块设备,轻质砖设备,蒸养砖设备,新型墙体设备-河南省杜甫机械制造有限公司 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | Duoguan 夺冠集团|