(spotify) play and pause when controlling

This commit is contained in:
2020-07-22 22:39:24 +02:00
parent d5402fe154
commit cc6efc49dd
4 changed files with 41 additions and 3 deletions

View File

@@ -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<string | null>(null)
const spotify = reactive({
@@ -21,6 +21,9 @@ export const useSpotify = () => {
const { accessToken, hasValidAccessToken } = useGetters<
GetterTree<State, State>
>(['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()
}