47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { useAudioStore } from '~/stores/audio'
|
|
import { useUserStore } from '~/stores/user'
|
|
import { getSoundscapeByTitle } from '~/tracks.config'
|
|
|
|
const { waitForCanPlay } = useAudioReplacer()
|
|
|
|
/** Dieses Plugin deckt die Applikationslogik für den Player ab, dadurch soll verhindert werden, dass Logik in Componenten verbaut ist,
|
|
* die durch die mount und unmount Mechanismen durcheinander kommen, würden. Das Plugin arbeitet eng mit dem audio-Store zusammen.
|
|
*
|
|
* Die benötigten Funktionen für die Navigationbar sowie das PlayerControls-Composable sind changeTrack zum Wechseln des Soundscapes
|
|
* inklusive der Interaktion mit dem Backend.
|
|
*
|
|
**/
|
|
|
|
export default defineNuxtPlugin(() => {
|
|
const audioStore = useAudioStore()
|
|
const { $axios } = useNuxtApp() as any
|
|
|
|
// Initialize audio context on first user interaction
|
|
const initAudioOnInteraction = () => {
|
|
audioStore.initializeAudioContext()
|
|
document.removeEventListener('click', initAudioOnInteraction)
|
|
}
|
|
|
|
// Method to handle track changes
|
|
/**
|
|
* Change to the given track name. This function should be called when audio is stopped already.
|
|
* @param title
|
|
*/
|
|
const changeTrack = async (title:string) => {
|
|
const src = getSoundscapeByTitle(title)
|
|
const currentAudio = document.getElementById(title) as HTMLAudioElement
|
|
if(currentAudio) {
|
|
await waitForCanPlay(currentAudio)
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|
|
// Expose methods to components
|
|
return {
|
|
provide: {
|
|
changeTrack
|
|
}
|
|
}
|
|
})
|