eS v1
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// ~/store/player.ts
|
||||
import { defineStore } from 'pinia'
|
||||
import type { Track } from '~/../types/types'
|
||||
import type { Track, Box } from '~/../types/types'
|
||||
import { useDataStore } from '~/store/data'
|
||||
|
||||
export const usePlayerStore = defineStore('player', {
|
||||
@@ -30,22 +30,23 @@ export const usePlayerStore = defineStore('player', {
|
||||
const track = this.currentTrack
|
||||
if (!track) return
|
||||
const dataStore = useDataStore()
|
||||
if (track.compilationId.length === 6) {
|
||||
if (track.type === 'playlist') {
|
||||
const next = dataStore.getNextPlaylistTrack(track)
|
||||
if (next && next.compilationId === track.compilationId) {
|
||||
if (next && next.boxId === track.boxId) {
|
||||
this.playTrack(next)
|
||||
}
|
||||
} else {
|
||||
console.log('ended')
|
||||
this.currentTrack = null
|
||||
}
|
||||
})
|
||||
},
|
||||
async playCompilation(compilationId: string) {
|
||||
if (this.currentTrack?.compilationId === compilationId) {
|
||||
async playBox(box: Box) {
|
||||
if (this.currentTrack?.boxId === box.id) {
|
||||
this.togglePlay()
|
||||
} else {
|
||||
const dataStore = useDataStore()
|
||||
const first = (compilationId.length === 6) ? dataStore.getFirstTrackOfPlaylist(compilationId) : dataStore.getFirstTrackOfCompilation(compilationId)
|
||||
const first = dataStore.getFirstTrackOfBox(box)
|
||||
if (first) {
|
||||
await this.playTrack(first)
|
||||
}
|
||||
@@ -53,7 +54,7 @@ export const usePlayerStore = defineStore('player', {
|
||||
},
|
||||
async playTrack(track: Track) {
|
||||
// mettre à jour la piste courante uniquement après avoir géré le toggle
|
||||
if (this.currentTrack?.id === track.id) {
|
||||
if (this.currentTrack && this.currentTrack?.id === track.id) {
|
||||
this.togglePlay()
|
||||
} else {
|
||||
this.currentTrack = track
|
||||
@@ -83,10 +84,18 @@ export const usePlayerStore = defineStore('player', {
|
||||
const onLoaded = () => resolve()
|
||||
audio.addEventListener('loadedmetadata', onLoaded, { once: true })
|
||||
})
|
||||
|
||||
// Appliquer le temps de départ
|
||||
audio.currentTime = wantedStart > 0 ? wantedStart : 0
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
const onCanPlay = () => {
|
||||
if (wantedStart > 0 && audio.currentTime < wantedStart - 0.05) {
|
||||
audio.currentTime = wantedStart
|
||||
}
|
||||
resolve()
|
||||
}
|
||||
if (audio.readyState >= 3) return resolve()
|
||||
audio.addEventListener('canplay', onCanPlay, { once: true })
|
||||
})
|
||||
this.isPaused = false
|
||||
await audio.play()
|
||||
} catch (err: any) {
|
||||
@@ -133,61 +142,37 @@ export const usePlayerStore = defineStore('player', {
|
||||
if (!track) return
|
||||
const dataStore = useDataStore()
|
||||
const t = audio.currentTime
|
||||
|
||||
// For playlists (id length 6), do NOT forward auto-advance here; rely on 'ended'
|
||||
if (track.compilationId.length === 6) {
|
||||
const from = track.start ?? 0
|
||||
const prevTrack = dataStore.getPrevTrack(track)
|
||||
if (t < from + 0.05) {
|
||||
if (prevTrack && prevTrack.compilationId === track.compilationId) {
|
||||
this.currentTrack = prevTrack
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// For compilations, forward auto-advance at segment boundary
|
||||
const nextTrack = dataStore.getNextTrack(track)
|
||||
const to = (track.order === 0) ? Math.round(this.audio?.duration ?? 0) : nextTrack?.start
|
||||
if (!to || isNaN(to)) return
|
||||
if (t >= to - 0.05) {
|
||||
if (nextTrack && nextTrack.compilationId === track.compilationId) {
|
||||
this.currentTrack = nextTrack
|
||||
}
|
||||
} else {
|
||||
const from = track.start ?? 0
|
||||
const prevTrack = dataStore.getPrevTrack(track)
|
||||
if (t < from + 0.05) {
|
||||
if (prevTrack && prevTrack.compilationId === track.compilationId) {
|
||||
this.currentTrack = prevTrack
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
getters: {
|
||||
isCurrentCompilation: (state) => {
|
||||
return (compilationId: string) => compilationId === state.currentTrack?.compilationId
|
||||
isCurrentBox: (state) => {
|
||||
return (boxId: string) => boxId === state.currentTrack?.boxId
|
||||
},
|
||||
|
||||
isPlaylistTrack: () => {
|
||||
return (track: Track) => {
|
||||
return track.compilationId.length === 6
|
||||
return track.type === 'playlist'
|
||||
}
|
||||
},
|
||||
|
||||
getCurrentTrack: (state) => state.currentTrack,
|
||||
|
||||
getCurrentCompilation: (state) => {
|
||||
getCurrentBox: (state) => {
|
||||
return state.currentTrack ? state.currentTrack.url : null
|
||||
},
|
||||
|
||||
currentProgression(state) {
|
||||
getCurrentProgression(state) {
|
||||
if (!state.audio) return 0
|
||||
const duration = state.audio.duration
|
||||
const progression = (state.position / duration) * 100
|
||||
return isNaN(progression) ? state.progressionLast : progression
|
||||
},
|
||||
|
||||
getCurrentCoverUrl(state) {
|
||||
const id = state.currentTrack?.coverId
|
||||
if (!id) return null
|
||||
return id.startsWith('http') ? id : `https://f4.bcbits.com/img/${id}_4.jpg`
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user