一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - js教程 - 原生JS實現音樂播放器的示例代碼

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

2022-01-22 20:15單線程_01 js教程

這篇文章主要介紹了原生JS實現音樂播放器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

效果圖

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

音樂播放器

  • 播放控制
  • 播放進度條控制
  • 歌詞顯示及高亮
  • 播放模式設置

播放器屬性歸類

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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事件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// 音樂的播放和暫停,上一首,下一首控制
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

?
1
2
3
4
5
6
// 播放進度實時更新
audioFile.file.addEventListener('timeupdate', lyricAndProgressMove);
// 通過拖拽調整進度
control.progressDot.addEventListener('click', adjustProgressByDrag);
// 通過點擊調整進度
control.progressWrap.addEventListener('click', adjustProgressByClick);

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
 
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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}%`
}

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

?
1
2
3
4
5
6
7
8
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 歌詞顯示實時更新
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:無

?
1
2
3
4
5
6
7
8
9
10
11
12
// 播放模式設置
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 音樂播放器內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://juejin.cn/post/6932662088250687501

延伸 · 閱讀

精彩推薦
  • js教程原生js 實現表單驗證功能

    原生js 實現表單驗證功能

    這篇文章主要介紹了原生js如何實現表單驗證功能,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下...

    蔣偉平7042022-01-19
  • js教程在JavaScript中查找字符串中最長單詞的三種方法(推薦)

    在JavaScript中查找字符串中最長單詞的三種方法(推薦)

    這篇文章主要介紹了在JavaScript中查找字符串中最長單詞的三種方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋...

    Hunter網絡安全7342022-01-04
  • js教程JavaScript canvas實現雨滴特效

    JavaScript canvas實現雨滴特效

    這篇文章主要為大家詳細介紹了JavaScript canvas實現雨滴特效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    huangdong19317022021-12-29
  • js教程JS中錨點鏈接點擊平滑滾動并自由調整到頂部位置

    JS中錨點鏈接點擊平滑滾動并自由調整到頂部位置

    這篇文章主要介紹了JS中錨點鏈接點擊平滑滾動并自由調整到頂部位置,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的...

    是這樣的三月6362022-01-19
  • js教程js實現驗證碼干擾(靜態)

    js實現驗證碼干擾(靜態)

    這篇文章主要為大家詳細介紹了js實現驗證碼干擾,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    lzh~5172022-01-22
  • js教程原生JavaScript實現隨機點名表

    原生JavaScript實現隨機點名表

    這篇文章主要為大家詳細介紹了原生JavaScript實現隨機點名表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    棟棟很優秀啊9822021-12-31
  • js教程通過滑動翻頁效果實現和移動端click事件問題

    通過滑動翻頁效果實現和移動端click事件問題

    這篇文章主要介紹了滑動翻頁效果實現和移動端click事件問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需...

    行舟客5392022-01-07
  • js教程js事件模型與自定義事件實例解析

    js事件模型與自定義事件實例解析

    JavaScript一個最簡單的事件模型,需要有事件綁定與觸發,還有事件刪除。本文將對其具體實現代碼進行解析,需要的朋友一起來看下吧...

    caihg5652021-12-15
主站蜘蛛池模板: 日本在线亚州精品视频在线 | 欧美多gayxxxx| 91麻豆国产福利精品 | 四虎永久在线精品波多野结衣 | 国产特黄一级一片免费 | 四虎在线永久免费视频网站 | 99九九精品免费视频观看 | 天天干天天操天天爽 | 日本护士撒尿xxxxhd | 男男gaygays18中国 | 国产高清国内精品福利 | 国产午夜永久福利视频在线观看 | 精品久久洲久久久久护士免费 | 国产专区亚洲欧美另类在线 | 国产成+人+综合+亚洲欧美丁香花 | 日韩在线观看一区二区不卡视频 | 精品国偷自产在线 | 日韩欧美一区二区三区免费看 | 99久久精品国产免看国产一区 | 黄漫在线播放 | h版欧美大片免费观看 | 动漫美女日批 | 狠狠色婷婷丁香六月 | 小夫妻天天恶战 | 妹妹你插的我好爽 | 成年人免费看的视频 | 亚洲国产成人久久午夜 | 久久青青草原综合伊人 | 欧美一区二区三区免费观看视频 | 国产日韩欧美一区 | 荷兰艾优apiyoo | 全彩成人18h漫画 | 男人的j插入女人的p | 亚欧洲乱码视频一二三区 | 色欧美在线 | 精品一久久香蕉国产二月 | 草莓视频旧版本 | 国产一区二区三区高清视频 | 国色天香社区在线视频免费观看 | 国产成人在线小视频 | 国产亚洲综合久久 |