この記事では主に、次のように共有されるネイティブ JS 音楽プレーヤーのサンプル コードを紹介します。 レンダリング音楽プレーヤー
選手属性分類プレーヤーの機能に応じてプレーヤーの属性とDOM要素を分類し、同じ機能を実装する要素と属性を同じオブジェクトに保存して、管理と操作を容易にします。 const control = { //プレーヤーコントロールを保存 play: document.querySelector('#myplay'), ... index: 2, //現在再生中の曲の番号... } const audioFile = { // 曲ファイルと関連情報を保存するファイル: document.getElementsByTagName('audio')[0], 現在の時刻: 0, 期間: 0, } const lyric = { // 歌詞表示バーの設定 ele: null, 歌詞行数: 0, 現在の行数: 0, 行の高さ: 0, } const modeControl = { //再生モード mode: ['sequential', 'random', 'single'], インデックス: 0 } const songInfo = { // 曲情報を格納するDOMコンテナ name: document.querySelector('.song-name'), ... } 再生コントロール機能: 音楽の再生と一時停止、前の曲、次の曲、再生の完了、対応するアイコンの変更を制御します。 // 音楽の再生と一時停止、前の曲と次の曲の制御 control.play.addEventListener('click',()=>{ コントロールを再生します。 プレイヤーハンドル(); } ); control.prev.addEventListener('click', prevHandle); control.next.addEventListener('click', nextHandle); audioFile.file.addEventListener('終了', nextHandle); 関数 playerHandle() { コントロールを再生します。 control.isPlay ? audioFile.file.play() : audioFile.file.pause(); コントロールが再生されない場合 //音楽を再生し、アイコンを変更してアニメーションの再生を開始します。play.classList.remove('songStop'); play.classList.add('songStart'); control.albumCover.classList.add('albumRotate'); control.albumCover.style.animationPlayState = '実行中'; } それ以外 { //音楽を一時停止し、アイコンを変更し、アニメーションを一時停止します... } } function prevHandle() { // 再生モードに応じて曲をリロードします const modeIndex = modeControl.index; 定数 songListLens = songList.length; if (modeIndex == 0){//順番に再生 let index = --control.index; インデックス == -1 ? (インデックス = songListLens - 1) : インデックス; control.index = インデックス % songListLens; } else if (modeIndex == 1){//ランダム再生 const randomNum = Math.random() * (songListLens - 1); control.index = Math.round(randomNum); } else if (modeIndex == 2){// シングル曲} reload(songList); } 関数 nextHandle() { 定数 modeIndex = modeControl.index; 定数 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); } 再生進行状況バーのコントロール機能: 再生の進行状況をリアルタイムで更新し、進行状況バーをクリックして曲の再生進行状況を調整します。 // 再生の進行状況はリアルタイムで更新されます audioFile.file.addEventListener('timeupdate', lyricAndProgressMove); // ドラッグして進行状況を調整します control.progressDot.addEventListener('click', adjustProgressByDrag); // クリックして進行状況を調整します control.progressWrap.addEventListener('click', adjustProgressByClick); 再生の進行状況は、対応するDOM要素の位置や幅を変更することでリアルタイムに更新されます。 関数 lyricAndProgressMove() { 定数 audio = audioFile.file; コントロールインデックスをコントロールのインデックスに格納します。 // 曲情報を初期化します const songLyricItem = document.getElementsByClassName('song-lyric-item'); songLyricItem.length == 0 の場合、戻り値は次のようになります。 currentTime を audioFile.currentTime = Math.round(audio.currentTime); とします。 継続時間を audioFile.duration = Math.round(audio.duration); とします。 // 進捗バーの移動 const progressWrapWidth = control.progressWrap.offsetWidth; const currentBarPOS = currentTime / 継続時間 * 100; control.progressBar.style.width = `${currentBarPOS.toFixed(2)}%`; const currentDotPOS = Math.round(currentTime / 継続時間 * progressWrapWidth); control.progressDot.style.left = `${currentDotPOS}px`; songInfo.currentTimeSpan.innerText = formatTime(currentTime); } ドラッグして進行状況を調整: ドラッグして進行状況バーを移動し、曲の再生進行状況を同期して更新します 関数adjustProgressByDrag() { const fragBox = control.progressDot; const progressWrap = control.progressWrap ドラッグ(fragBox、progressWrap) } 関数ドラッグ(fragBox, wrap) { wrapWidth は、オフセットの幅を 0 から 10 の範囲で設定します。 wrapLeft は、次の式で定義されます。 関数dragMove(e) { 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('マウスアップ', () => { document.removeEventListener('mousemove', dragMove); fragBox.style.width = `10px`;fragBox.style.height = `10px`;fragBox.style.top = `-4px`; }) }); } function changeProgressBarPos(disX, wrapWidth) { // プログレスバーのステータス更新 const audio = audioFile.file 定数期間 = audioFile.duration ドットポス barPos を (disX < 0)の場合{ ドット位置 = -4 バー位置 = 0 オーディオ.currentTime = 0 } そうでない場合 (disX > 0 && disX < wrapWidth) { ドット位置 = ディスX barPos = 100 * (disX / wrapWidth) audio.currentTime = 継続時間 * (disX / wrapWidth) } それ以外 { ドット位置 = ラップ幅 - 4 バー位置 = 100 audio.currentTime = 継続時間 } control.progressDot.style.left = `${dotPos}px` control.progressBar.style.width = `${barPos}%` } 進行状況バーをクリックして調整します。進行状況バーをクリックすると、曲の再生進行が同期して更新されます。 関数adjustProgressByClick(e) { const wrap = control.progressWrap; wrapWidth は、オフセットの幅を 0 から 10 の範囲で設定します。 wrapLeft は、次の式で定義されます。 const disX = e.pageX - wrapLeft; changeProgressBarPos(disX, wrapWidth) } 歌詞の表示とハイライト機能: 再生の進行に応じて歌詞の表示をリアルタイムで更新し、現在の歌詞を強調表示します (スタイルを追加することにより) // 歌詞の表示がリアルタイムで更新されます audioFile.file.addEventListener('timeupdate', lyricAndProgressMove); 関数 lyricAndProgressMove() { 定数 audio = audioFile.file; コントロールインデックスをコントロールのインデックスに格納します。 const songLyricItem = document.getElementsByClassName('song-lyric-item'); songLyricItem.length == 0 の場合、戻り値は次のようになります。 currentTime を audioFile.currentTime = Math.round(audio.currentTime); とします。 継続時間を audioFile.duration = Math.round(audio.duration); とします。 totalLyricRows = lyric.totalLyricRows = songLyricItem.length とします。 LyricEle = lyric.ele = songLyricItem[0]とします。 lyricEle を LyricEle.offsetHeight に設定します。 //歌詞の動き lrcs[controlIndex].lyric.forEach((item, index) => { (現在の時刻 === アイテムの時刻) の場合 { lyric.currentRows = インデックス; songLyricItem[index].classList.add('song-lyric-item-active'); インデックス > 0 && songLyricItem[インデックス - 1].classList.remove('song-lyric-item-active'); if (インデックス > 5 && インデックス < totalLyricRows - 5) { songInfo.lyricWrap.scrollTo(0, `${rowsHeight * (index - 5)}`) } } }) } 再生モード設定機能: クリックすると再生モードにジャンプし、対応するアイコンを変更します // 再生モードの設定 control.mode.addEventListener('click', changePlayMode); 関数 changePlayMode() { modeControl.index = ++modeControl.index % 3; 定数モード = 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 音楽プレーヤーの詳細については、123WORDPRESS.COM の以前の記事を検索するか、以下の関連記事を引き続き参照してください。今後とも 123WORDPRESS.COM をよろしくお願いいたします。 以下もご興味があるかもしれません:
|
<<: Linux システムでデプロイメント プロジェクトを設定する方法
目次1. コンポーネント切り替え方式方法1: v-ifとv-elseを使用する方法 2: 組み込みコ...
目次1. 概要2. 名前フィールド3. バージョンフィールド4. 説明フィールド5. キーワードフィ...
目次トリガーとは何かトリガーを作成する表は次のようになります。さらにいくつかの単語を挙げます。制限と...
マウスの位置をマッピングしたり、ドラッグ効果を実装したりすることは、 JavaScriptで行うこと...
Tomcatはプロジェクトにアクセスします。通常はIP + ポート + プロジェクト名です。 Ngi...
序文Linux カーネルでは、元のコードとの互換性を保つため、または特定の仕様に準拠するため、また現...
この記事では、docker 経由で Jenkins+Maven+SVN+Tomcat をデプロイし、...
Raspberry Pi は ARM アーキテクチャをベースとしているため、Docker のインスト...
<br />原文: http://andymao.com/andy/post/102.h...
この記事では、MySQL 5.6.37のダウンロード、インストール、設定のチュートリアルを参考までに...
<テンプレート> <div id="ルート"> <...
このチュートリアルでは、LinuxにMySQLをインストールする詳細な手順を参考までに紹介します。具...
1. コンテナに入るdocker run [オプション] イメージ名 [起動コンテナに渡されるコマン...
Docker Toolbox は、Windows 10 Professional より前のバージョン...
この記事では、効率を向上させ、時間を節約することを願って、最も効果的な 6 つの方法を紹介します。 ...