找回密码
 立即注册
首页 业界区 业界 实现一个简单的基于APlayer.js/.ts的支持网易云远程音乐 ...

实现一个简单的基于APlayer.js/.ts的支持网易云远程音乐的音乐播放器

孙淼淼 前天 19:22
最近写个人网站的时候有这个需求,然而 metting.js 支持不稳定,于是便想自己实现一个。由于我是拿 TS 进行开发的,故使用 APlayer.ts 。
首先 APlayer.ts 音乐是可以直接从远程获取的,请求网易云歌曲只需要请求 https://music.163.com/song/media/outer/url?id=${id}.mp3 。
比较麻烦的是获取歌曲信息和歌词,需要绕开跨域请求限制,考虑到我的网页在 Netlify 上部署,添加一个 netlify.toml 配置文件:
  1. [[redirects]]
  2. from = "/music/*"
  3. to = "https://music.163.com/:splat"
  4. status = 200
  5. force = true
复制代码
请求歌曲信息和歌词:
  1. async fetchSongDetail(songId: number) {
  2.     try {
  3.         const response = await fetch(
  4.             `/music/api/song/detail/?id=${songId}&ids=%5B${songId}%5D`,
  5.         );
  6.         if (!response.ok) {
  7.             throw new Error(`HTTP error! status: ${response.status}`);
  8.         }
  9.         const data = await response.json();
  10.         return data.songs[0];
  11.     } catch (error) {
  12.         console.error('Error fetching song detail:', error);
  13.         return null;
  14.     }
  15. }
  16. async fetchSongLyric(songId: number) {
  17.     try {
  18.         const response = await fetch(
  19.             `/music/api/song/lyric?id=${songId}&lv=1&kv=1&tv=-1`,
  20.         );
  21.         if (!response.ok) {
  22.             throw new Error(`HTTP error! status: ${response.status}`);
  23.         }
  24.         const data = await response.json();
  25.         return data.lrc.lyric;
  26.     } catch (error) {
  27.         console.error('Error fetching song lyric:', error);
  28.         return null;
  29.     }
  30. }
复制代码
最后我单独注册了一个组件实现网易云音乐的播放:
  1. import './music_player.scss';
  2. import APlayer from 'aplayer-ts';
  3. export class MusicPlayerElement extends HTMLElement {
  4.     constructor() {
  5.         super();
  6.         const id = parseInt(this.id);
  7.         this.fetchSongDetail(id).then((detail) => {
  8.             if (detail) {
  9.                 this.fetchSongLyric(id).then((lyric) => {
  10.                     if (lyric) {
  11.                         try {
  12.                             APlayer().init({
  13.                                 container: this,
  14.                                 lrcType: 1,
  15.                                 audio: [
  16.                                     {
  17.                                         name: detail.name,
  18.                                         artist: detail.artists
  19.                                             .map(
  20.                                                 (artist: { name: any }) =>
  21.                                                     artist.name,
  22.                                             )
  23.                                             .join(', '),
  24.                                         url: `https://music.163.com/song/media/outer/url?id=${id}.mp3`,
  25.                                         cover: detail.album.picUrl,
  26.                                         lrc: lyric,
  27.                                     },
  28.                                 ],
  29.                             });
  30.                         } catch (err) {
  31.                             console.error('Error initializing APlayer:', err);
  32.                         }
  33.                     }
  34.                 });
  35.             }
  36.         });
  37.     }
  38.     async fetchSongDetail(songId: number) {
  39.         // ...
  40.     }
  41.     async fetchSongLyric(songId: number) {
  42.         // ...
  43.     }
  44. }
  45. customElements.define('music-player', MusicPlayerElement);
复制代码
另外还有一个自建的暗色播放器样式:
  1. @use '../styles/share.scss' as share;
  2. @import url('aplayer-ts/src/css/base.css');
  3. @media (prefers-color-scheme: dark) {
  4.     .aplayer {
  5.         .aplayer-body {
  6.             background: share.$main-color;
  7.         }
  8.         .aplayer-lrc {
  9.             &:before {
  10.                 background: linear-gradient(180deg,
  11.                         share.$main-color 0,
  12.                         hsla(0, 0%, 100%, 0));
  13.             }
  14.             &:after {
  15.                 background: linear-gradient(180deg,
  16.                         hsla(0, 0%, 100%, 0) 0,
  17.                         share.$main-color );
  18.             }
  19.             p {
  20.                 color: white;
  21.                 &.aplayer-lrc-current {
  22.                     color: #66ccff;
  23.                 }
  24.             }
  25.         }
  26.         .aplayer-info {
  27.             .aplayer-music {
  28.                 .aplayer-author {
  29.                     color: #66ccff;
  30.                 }
  31.             }
  32.         }
  33.     }
  34.     .aplayer-title {
  35.         color: white;
  36.     }
  37.     @media (max-width: calc(#{share.$screen-limit} - 1px)) {
  38.         music-player {
  39.             display: none;
  40.         }
  41.     }
  42. }
复制代码
使用方法:
[code][/code]
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册