diff --git a/src/components/SpotifyMusic.vue b/src/components/SpotifyMusic.vue index 6b20c33..fc533b9 100644 --- a/src/components/SpotifyMusic.vue +++ b/src/components/SpotifyMusic.vue @@ -23,8 +23,19 @@ export default defineComponent({ required: true } }, - setup(props) { - const { ready, play, pause } = useSpotify() + setup(props, { emit }) { + const onPlay = () => { + if (!props.play) { + emit('play') + } + } + + const onPause = () => { + if (props.play) { + emit('pause') + } + } + const { ready, play, pause } = useSpotify(onPlay, onPause) watchEffect(() => { props.play ? play() : pause() diff --git a/src/hooks/useSpotify.ts b/src/hooks/useSpotify.ts index fa1d060..1c1825b 100644 --- a/src/hooks/useSpotify.ts +++ b/src/hooks/useSpotify.ts @@ -12,7 +12,7 @@ import { } from '@/utils/spotify' import { ref, reactive } from '@vue/composition-api' -export const useSpotify = () => { +export const useSpotify = (onPlay: () => void, onPause: () => void) => { const ready = ref(false) const deviceId = ref(null) const spotify = reactive({ @@ -21,6 +21,9 @@ export const useSpotify = () => { const { accessToken, hasValidAccessToken } = useGetters< GetterTree >(['accessToken', 'hasValidAccessToken']) + const { setAccessToken, setTokenExpire, setHasSpotify } = useActions< + RootActions + >(['setAccessToken', 'setTokenExpire', 'setHasSpotify']) window.onSpotifyWebPlaybackSDKReady = async () => { if (!hasValidAccessToken.value) { @@ -36,6 +39,15 @@ export const useSpotify = () => { deviceId.value = device_id ready.value = true }) + spotify.player.addListener('player_state_changed', (state) => { + console.log('player state changed', state) + !state.paused ? onPlay() : onPause() + }) + spotify.player.addListener('not_ready', () => { + setAccessToken(null) + setTokenExpire(null) + setHasSpotify(false) + }) await spotify.player.connect() } diff --git a/src/store/index.ts b/src/store/index.ts index fcf1634..8ab149e 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -17,6 +17,7 @@ export interface State { export interface RootActions extends ActionTree { setInterval: (ctx: ActionContext, payload: number) => void setHasMusic: (ctx: ActionContext, payload: boolean) => void + setHasSpotify: (ctx: ActionContext, payload: boolean) => void setDev1: (ctx: ActionContext, payload: string) => void setDev2: (ctx: ActionContext, payload: string) => void setAccessToken: (ctx: ActionContext, payload: string) => void diff --git a/src/utils/spotify.ts b/src/utils/spotify.ts index 9a350dd..81ae43b 100644 --- a/src/utils/spotify.ts +++ b/src/utils/spotify.ts @@ -20,6 +20,20 @@ export const redirectToSpotifyConnect = () => { } export const playOnSpotify = async (token: string, deviceId: string) => { + // const response = await fetch( + // `https://api.spotify.com/v1/me/player/shuffle?device_id=${deviceId}&state=true`, + // { + // method: 'PUT', + // headers: { + // accept: 'application/json', + // 'content-type': 'application/json', + // authorization: `Bearer ${token}` + // } + // } + // ) + + // console.log(await response.json()) + await fetch( `https://api.spotify.com/v1/me/player/play?device_id=${deviceId}`, {