aplayer播放器对接alist

24 年 11 月 5 日 星期二 (已编辑)
225 字
2 分钟

如果你不是通过官方的方式引入aplayer,那么建议你先看看这篇文章网页接入aplayer音乐播放器 | butlanys的博客

只需要在aplayer的配置js中改为下面的js

javascript
const urlParams = new URLSearchParams(window.location.search);
const path = urlParams.get('path') || '/music'; // alist中访客可看到音乐的路径

fetch(`https://alist.hqycloud.top/api/fs/list`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    path: path,
    password: '', // 如果用的是访客权限,这里可以为空
    page: 1,
    per_page: 100, //获取的最大音乐数量,此值可大不可小,否则获取的音乐不全
    refresh: false
  })
})
  .then(response => response.json())
  .then(data => {
    if (data.code === 200) {
      const audioList = data.data.content
        .filter(item => !item.is_dir)
        .map(item => ({
          name: item.name.replace(/\.[^/.]+$/, ""),
          url: `https://alist.hqycloud.top/d/onedrive-data${path}/${encodeURIComponent(item.name)}`, 
          cover: item.thumb // 可选:设置封面图片
        }));

      const ap = new APlayer({
        container: document.getElementById('aplayer'),
        fixed: true,
        autoplay: true,
        audio: audioList
      });
    } else {
      console.error('获取音乐列表失败:', data.message);
    }
  })
  .catch(error => {
    console.error('获取音乐列表出错:', error);
  });

文章标题:aplayer播放器对接alist

文章作者:butlanys

文章链接:https://blog.butlanys.de/posts/51355[复制]

最后修改时间:


商业转载请联系站长获得授权,非商业转载请注明本文出处及文章链接,您可以自由地在任何媒体以任何形式复制和分发作品,也可以修改和创作,但是分发衍生作品时必须采用相同的许可协议。
本文采用CC BY-NC-SA 4.0进行许可。