50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { ref, type Ref } from "vue";
|
|
import { useRoute } from "vue-router";
|
|
import axios, { CancelToken } from "axios";
|
|
import { api } from "@";
|
|
|
|
export const usePlayer = defineStore("player", () => {
|
|
const station = ref(null);
|
|
const playing = ref(false);
|
|
const instance = ref(null);
|
|
|
|
const register = (_instance) => {
|
|
instance.value = _instance;
|
|
};
|
|
|
|
const load = (station: api.StationInfo, start: bool = false) => {
|
|
station.value = station;
|
|
instance.value.src = station.value.url;
|
|
instance.value.load();
|
|
if (start) {
|
|
play();
|
|
}
|
|
};
|
|
|
|
const play = () => {
|
|
if (!instance.value.src)
|
|
return;
|
|
instance.value.play();
|
|
playing.value = true;
|
|
};
|
|
|
|
const pause = () => {
|
|
if (!instance.value.src)
|
|
return;
|
|
instance.value.pause();
|
|
playing.value = false;
|
|
};
|
|
|
|
const toggle = () => {
|
|
if (playing.value) {
|
|
pause();
|
|
} else {
|
|
play();
|
|
}
|
|
};
|
|
|
|
|
|
return { station, playing, load, instance, register, play, pause, toggle };
|
|
});
|