webrtc/public/music.js

173 lines
5.8 KiB
JavaScript

import { Button, List, ListItem } from './weigets.js'
export default class MusicList {
constructor({ list = [], EventListeners = {}, onplay, onstop, onadd, onremove, onlike, onban, onload }) {
this.event = { onplay, onstop, onadd, onremove, onlike, onban, onload }
this.ul = List({})
this.ul.classList.add('music-list')
this.EventListeners = EventListeners
this.list = []
list.forEach(item => this.add(item)) // 列表逐一添加
document.body.appendChild(this.ul) // 元素加入页面
// 添加音乐播放器
this.audio = new Audio()
this.audio.addEventListener('ended', () => {
this.next()
})
//this.audio.addEventListener('timeupdate', () => {
// console.log(this.audio.currentTime)
//})
// 本地添加音乐按钮
const input = document.createElement('input')
input.type = 'file'
input.multiple = true
input.accept = 'audio/*'
input.onchange = event => {
for (const file of event.target.files) {
const id = 'music' + Date.now()
const { name, size, type } = file
const reader = new FileReader()
reader.onload = async event => {
const arrayBuffer = event.target.result
this.add({ id, name, size, type, arrayBuffer })
}
reader.readAsArrayBuffer(file)
}
}
// 写入 css 样式到 head
const style = document.createElement('style')
style.innerText = `
ul.music-list > li {
cursor: pointer;
}
ul.music-list > li.cache::marker {
color: #02be08;
font-size: 1em;
contentx: '⚡';
}
ul.music-list > li.disable {
color: #999999;
}
ul.music-list > li > button {
margin-left: 10px;
border: none;
cursor: pointer;
}
ul.music-list > li > button:hover {
background-color: #ccc;
}
`
document.head.appendChild(style)
document.body.appendChild(input)
}
// 添加回调函数
on(name, callback) {
this.EventListeners[name] = callback
}
add(item) {
// 将字节转换为可读的单位
const bytesToSize = bytes => {
if (bytes === 0) return '0 B'
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i]
}
this.list.push(item)
this.ul.appendChild(ListItem({
id: item.id,
classList: item.arrayBuffer ? ['cache'] : [],
innerText: `${item.name} - ${bytesToSize(item.size)}`,
children: [
Button({
innerText: '播放',
onclick: event => {
event.stopPropagation()
if (event.target.dataset.play === 'play') {
event.target.dataset.play = 'stop'
event.target.innerText = '播放'
this.stop()
} else {
event.target.dataset.play = 'play'
event.target.innerText = '停止'
this.play(item)
}
}
}),
Button({
innerText: '移除',
onclick: event => {
event.stopPropagation()
this.remove(item)
}
}),
Button({
innerText: '喜欢',
onclick: event => {
event.stopPropagation()
this.like(item)
}
}),
Button({
innerText: '禁止',
onclick: event => {
event.stopPropagation()
if (event.target.dataset.ban === 'ban') {
event.target.dataset.ban = 'unban'
event.target.innerText = '禁止'
} else {
event.target.dataset.ban = 'ban'
event.target.innerText = '解禁'
this.stop()
}
}
})
]
}))
this.event.onadd(item, this.list)
}
async remove(item) {
this.ul.querySelector(`#${item.id}`)?.remove()
this.stop() // 停止播放
this.event.onremove(item)
}
async load(item) {
await this.event.onload(item)
}
async play(item) {
if (!item.arrayBuffer) {
await this.load(item)
}
this.playing = item
this.audio.src = URL.createObjectURL(new Blob([item.arrayBuffer], { type: item.type }))
this.audio.play()
this.event.onplay(item)
}
async stop() {
this.audio.pause()
this.audio.src = ''
this.event.onstop(this.playing)
}
async like(item) {
if (!item.arrayBuffer) {
await this.load(item)
return
}
if (item.arrayBuffer) {
return
}
// TODO: 添加喜欢和取消喜欢
this.event.onlike(item)
}
async ban(item) {
this.event.onban(item)
}
//like(user_id, item_id) {
// //if (!item.like) item.like = []
// //item.like.push(user_id)
//}
next() { }
prev() { }
}