HCLonely
HCLonely
文章32
标签54
分类14
为网页添加一个音乐播放器插件

为网页添加一个音乐播放器插件

分站内容可能或出现渲染错误或内容过时等问题,请以主站为准!
分站禁止评论,评论请前往主站

本篇文章教大家如何给自己的网页添加一个音乐播放器。

插件名称:APlayer

使用方法

在header标签引入css样式文件

<link rel="stylesheet" href="https://cdn.bootcss.com/aplayer/1.10.1/APlayer.min.css">

在body标签内加入div标签并引入js文件

<script src="https://cdn.bootcss.com/aplayer/1.10.1/APlayer.min.js"></script>

加载播放器

const ap = new APlayer({
container: document.getElementById('aplayer'),
fixed: true,//开启吸底模式
autoplay: true,//自动播放
lrcType: 1,//歌词加载方式
audio: audioArr,//歌曲列表
});
//更多详细参数请前往https://aplayer.js.org/#/zh-Hans/查看

引入autoPlay.js自动加载网易云音乐(可选,需jQuery支持)

try {
$.ajax({url: "https://cdn.bootcss.com/bluebird/3.5.5/bluebird.min.js", dataType:"script", cache: true, success: function() {
$.ajax({url: "https://cdn.bootcss.com/fetch/2.0.4/fetch.min.js", dataType:"script", cache: true, success: function() {
function fetch163Playlist(playlist_id) {
return new Promise(function (ok, err) {
fetch("https://v1.hitokoto.cn/nm/playlist/" + playlist_id)
.then(function (response) {
return response.json();
})
.then(function (data) {
var arr = [];
data.playlist.tracks.map(function (value) {
arr.push(value.id);
});
return arr;
})
.then(function (ids) {
return fetch163Songs(ids);
})
.then(function (data) {
ok(data);
})
.catch(function (e) {
err(e);
});
})
}
function fetch163Songs(IDS) {
return new Promise(function (ok, err) {
var ids;
switch (typeof IDS) {
case 'number':
ids = [IDS];
break;
case 'object':
if (!Array.isArray(IDS)) {
err(new Error("Please enter array or number"));
}
ids = IDS;
break;
default:
err(new Error("Please enter array or number"));
break;
}
fetch("https://v1.hitokoto.cn/nm/summary/" + ids.join(",") + "?lyric=true&common=true")
.then(function (response) {
return response.json();
})
.then(function (data) {
var songs = [];
data.songs.map(function (song) {
songs.push({
name: song.name,
url: song.url,
artist: song.artists.join("/"),
album: song.album.name,
pic: song.album.picture,
lrc: song.lyric
});
});
return songs;
})
.then(function (result) {
ok(result);
})
.catch(function (e) {
err(e);
});
});
}
/****加载多个网易云音乐单曲****/
fetch163Songs(songArr)//此处"songArr"为网易云音乐id数组
.then(function (data) {
let audioArr=[];
for(let i=0;i<data.length;i++){
audioArr.push({'name':data[i].name,'artist':data[i].artist,'url':data[i].url,'cover':data[i].pic,'lrc':data[i].lrc.base});
}
const ap = new APlayer({
container: document.getElementById('aplayer'),
fixed: true,
autoplay: true,
lrcType: 1,
audio: audioArr,
});
})
.catch(function (err) {
console.error('网易云音乐获取失败!'+err);
})
/****加载网易云音乐歌单****/
fetch163Playlist(listId)//此处"listId"为网易云音乐歌单id
.then(function (data) {
let audioArr=[];
for(let i=0;i<data.length;i++){
audioArr.push({'name':data[i].name,'artist':data[i].artist,'url':data[i].url,'cover':data[i].pic,'lrc':data[i].lrc.base});
}
const ap = new APlayer({
container: document.getElementById('aplayer'),
fixed: true,
autoplay: true,
lrcType: 1,
audio: audioArr,
});
})
.catch(function (err) {
console.error('网易云音乐获取失败!'+err);
})
}});
}});
} catch(err) { console.log("[Error]"+err) }

说明