2023-09-28 15:20:02 +08:00
|
|
|
import IndexedDB from './database.js'
|
|
|
|
import { Button, List, ListItem } from './weigets.js'
|
|
|
|
|
|
|
|
export default class MusicList {
|
2023-09-29 21:21:26 +08:00
|
|
|
constructor({ list = [], EventListeners = {} }) {
|
2023-09-28 15:20:02 +08:00
|
|
|
this.ul = List({})
|
2023-09-29 21:21:26 +08:00
|
|
|
this.EventListeners = EventListeners
|
2023-09-29 17:31:37 +08:00
|
|
|
this.list = []
|
2023-09-29 21:21:26 +08:00
|
|
|
list.forEach(item => this.add(item)) // 列表逐一添加
|
|
|
|
document.body.appendChild(this.ul) // 元素加入页面
|
2023-09-28 15:20:02 +08:00
|
|
|
|
|
|
|
// 添加音乐播放器
|
|
|
|
this.audio = new Audio()
|
|
|
|
this.audio.addEventListener('ended', () => {
|
|
|
|
this.next()
|
|
|
|
})
|
2023-09-28 17:59:23 +08:00
|
|
|
//this.audio.addEventListener('timeupdate', () => {
|
|
|
|
// console.log(this.audio.currentTime)
|
|
|
|
//})
|
2023-09-28 15:20:02 +08:00
|
|
|
|
2023-09-29 21:21:26 +08:00
|
|
|
// 本地添加音乐按钮
|
2023-09-28 15:20:02 +08:00
|
|
|
const input = document.createElement('input')
|
|
|
|
input.type = 'file'
|
|
|
|
input.multiple = true
|
|
|
|
input.accept = 'audio/*'
|
|
|
|
input.onchange = event => {
|
|
|
|
for (const file of event.target.files) {
|
2023-09-28 18:15:12 +08:00
|
|
|
const id = 'music' + Date.now()
|
2023-09-28 15:20:02 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
document.body.appendChild(input)
|
|
|
|
}
|
2023-09-29 21:21:26 +08:00
|
|
|
// 添加回调函数
|
|
|
|
on(name, callback) {
|
|
|
|
this.EventListeners[name] = callback
|
|
|
|
}
|
|
|
|
add(item) {
|
|
|
|
this.list.push(item)
|
|
|
|
this.ul.appendChild(ListItem({
|
2023-09-28 15:20:02 +08:00
|
|
|
id: item.id,
|
|
|
|
innerText: `${item.name} - ${item.size} - ${item.type} - ${item.id}`,
|
|
|
|
children: [
|
|
|
|
Button({
|
|
|
|
innerText: '播放',
|
|
|
|
onclick: event => {
|
|
|
|
event.stopPropagation()
|
2023-09-28 17:59:23 +08:00
|
|
|
this.play(item)
|
2023-09-28 15:20:02 +08:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
Button({
|
|
|
|
innerText: '停止',
|
|
|
|
onclick: event => {
|
|
|
|
event.stopPropagation()
|
|
|
|
this.stop(item.id)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
Button({
|
|
|
|
innerText: '移除',
|
|
|
|
onclick: event => {
|
|
|
|
event.stopPropagation()
|
2023-09-29 21:21:26 +08:00
|
|
|
this.remove(item)
|
2023-09-28 15:20:02 +08:00
|
|
|
}
|
2023-09-29 18:14:18 +08:00
|
|
|
}),
|
|
|
|
Button({
|
|
|
|
innerText: '喜欢',
|
|
|
|
onclick: event => {
|
|
|
|
event.stopPropagation()
|
2023-09-29 18:36:05 +08:00
|
|
|
this.like(item)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
Button({
|
|
|
|
innerText: '禁止',
|
|
|
|
onclick: event => {
|
|
|
|
event.stopPropagation()
|
|
|
|
// BAN
|
2023-09-29 18:14:18 +08:00
|
|
|
}
|
2023-09-28 15:20:02 +08:00
|
|
|
})
|
|
|
|
]
|
2023-09-29 21:21:26 +08:00
|
|
|
}))
|
2023-09-29 17:31:37 +08:00
|
|
|
}
|
2023-09-29 21:21:26 +08:00
|
|
|
remove(item) {
|
2023-09-28 17:59:23 +08:00
|
|
|
this.ul.removeChild(this.ul.querySelector(`#${item.id}`))
|
|
|
|
this.stop() // 停止播放
|
2023-09-29 21:21:26 +08:00
|
|
|
// 执行回调函数
|
|
|
|
if (this.EventListeners['remove']) {
|
|
|
|
this.EventListeners['remove'](item)
|
|
|
|
}
|
2023-09-28 17:59:23 +08:00
|
|
|
}
|
2023-09-29 20:20:00 +08:00
|
|
|
async play(item) {
|
|
|
|
// 如果没有arrayBuffer则从对方获取
|
|
|
|
console.log('play:', item)
|
|
|
|
if (!item.arrayBuffer) {
|
|
|
|
console.log('从对方获取:', item)
|
|
|
|
return
|
|
|
|
}
|
2023-09-28 17:59:23 +08:00
|
|
|
this.audio.src = URL.createObjectURL(new Blob([item.arrayBuffer], { type: item.type }))
|
|
|
|
this.audio.play()
|
2023-09-29 21:21:26 +08:00
|
|
|
// 执行回调函数
|
|
|
|
if (this.EventListeners['play']) {
|
|
|
|
this.EventListeners['play'](item)
|
|
|
|
}
|
2023-09-28 17:59:23 +08:00
|
|
|
}
|
|
|
|
stop() {
|
|
|
|
this.audio.pause()
|
|
|
|
this.audio.src = ''
|
2023-09-29 21:21:26 +08:00
|
|
|
// 执行回调函数
|
|
|
|
if (this.EventListeners['stop']) {
|
|
|
|
this.EventListeners['stop']()
|
|
|
|
}
|
2023-09-28 15:20:02 +08:00
|
|
|
}
|
2023-09-29 18:36:05 +08:00
|
|
|
like(item) {
|
|
|
|
if (!item.arrayBuffer) {
|
|
|
|
console.log('载入缓存:', item)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
console.log('移除缓存:', item)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 15:20:02 +08:00
|
|
|
next() { }
|
|
|
|
prev() { }
|
|
|
|
}
|