add cards & tracks
This commit is contained in:
57
app/store/player.ts
Normal file
57
app/store/player.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
// ~/store/player.ts
|
||||
import { defineStore } from 'pinia'
|
||||
import type { Track } from '~/types/types'
|
||||
|
||||
export const usePlayerStore = defineStore('player', {
|
||||
state: () => ({
|
||||
currentTrack: null as Track | null,
|
||||
isPlaying: false,
|
||||
position: 0,
|
||||
audio: null as HTMLAudioElement | null,
|
||||
}),
|
||||
|
||||
actions: {
|
||||
setTrack(track: Track) {
|
||||
this.currentTrack = track
|
||||
if (!this.audio) this.audio = new Audio(this.getCompilationUrlFromTrack(track))
|
||||
else this.audio.src = this.getCompilationUrlFromTrack(track)
|
||||
|
||||
// Commencer à start secondes
|
||||
this.audio.currentTime = track.start || 0
|
||||
},
|
||||
|
||||
playTrack(track?: Track) {
|
||||
if (track) this.setTrack(track)
|
||||
if (!this.currentTrack || !this.audio) return
|
||||
|
||||
this.audio.play()
|
||||
this.isPlaying = true
|
||||
},
|
||||
|
||||
pauseTrack() {
|
||||
if (this.audio) this.audio.pause()
|
||||
this.isPlaying = false
|
||||
},
|
||||
|
||||
togglePlay(track?: Track) {
|
||||
if (track && (!this.currentTrack || track.id !== this.currentTrack.id)) {
|
||||
this.playTrack(track)
|
||||
} else {
|
||||
this.isPlaying ? this.pauseTrack() : this.playTrack()
|
||||
}
|
||||
},
|
||||
|
||||
setPosition(time: number) {
|
||||
if (this.audio) this.audio.currentTime = time
|
||||
this.position = time
|
||||
},
|
||||
},
|
||||
|
||||
getters: {
|
||||
getCurrentTrack: (state) => state.currentTrack,
|
||||
getPlaying: (state) => state.isPlaying,
|
||||
getCompilationUrlFromTrack: (state) => {
|
||||
return (track: Track) => `https://files.erudi.fr/evilspins/${track.compilationId}.mp3`
|
||||
}
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user