(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

@@ -23,8 +23,19 @@ export default defineComponent({
required: true required: true
} }
}, },
setup(props) { setup(props, { emit }) {
const { ready, play, pause } = useSpotify() const onPlay = () => {
if (!props.play) {
emit('play')
}
}
const onPause = () => {
if (props.play) {
emit('pause')
}
}
const { ready, play, pause } = useSpotify(onPlay, onPause)
watchEffect(() => { watchEffect(() => {
props.play ? play() : pause() props.play ? play() : pause()

View File

@@ -12,7 +12,7 @@ import {
} from '@/utils/spotify' } from '@/utils/spotify'
import { ref, reactive } from '@vue/composition-api' import { ref, reactive } from '@vue/composition-api'
export const useSpotify = () => { export const useSpotify = (onPlay: () => void, onPause: () => void) => {
const ready = ref(false) const ready = ref(false)
const deviceId = ref<string | null>(null) const deviceId = ref<string | null>(null)
const spotify = reactive({ const spotify = reactive({
@@ -21,6 +21,9 @@ export const useSpotify = () => {
const { accessToken, hasValidAccessToken } = useGetters< const { accessToken, hasValidAccessToken } = useGetters<
GetterTree<State, State> GetterTree<State, State>
>(['accessToken', 'hasValidAccessToken']) >(['accessToken', 'hasValidAccessToken'])
const { setAccessToken, setTokenExpire, setHasSpotify } = useActions<
RootActions
>(['setAccessToken', 'setTokenExpire', 'setHasSpotify'])
window.onSpotifyWebPlaybackSDKReady = async () => { window.onSpotifyWebPlaybackSDKReady = async () => {
if (!hasValidAccessToken.value) { if (!hasValidAccessToken.value) {
@@ -36,6 +39,15 @@ export const useSpotify = () => {
deviceId.value = device_id deviceId.value = device_id
ready.value = true 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() await spotify.player.connect()
} }

View File

@@ -17,6 +17,7 @@ export interface State {
export interface RootActions extends ActionTree<State, State> { export interface RootActions extends ActionTree<State, State> {
setInterval: (ctx: ActionContext<State, State>, payload: number) => void setInterval: (ctx: ActionContext<State, State>, payload: number) => void
setHasMusic: (ctx: ActionContext<State, State>, payload: boolean) => void setHasMusic: (ctx: ActionContext<State, State>, payload: boolean) => void
setHasSpotify: (ctx: ActionContext<State, State>, payload: boolean) => void
setDev1: (ctx: ActionContext<State, State>, payload: string) => void setDev1: (ctx: ActionContext<State, State>, payload: string) => void
setDev2: (ctx: ActionContext<State, State>, payload: string) => void setDev2: (ctx: ActionContext<State, State>, payload: string) => void
setAccessToken: (ctx: ActionContext<State, State>, payload: string) => void setAccessToken: (ctx: ActionContext<State, State>, payload: string) => void

View File

@@ -20,6 +20,20 @@ export const redirectToSpotifyConnect = () => {
} }
export const playOnSpotify = async (token: string, deviceId: string) => { 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( await fetch(
`https://api.spotify.com/v1/me/player/play?device_id=${deviceId}`, `https://api.spotify.com/v1/me/player/play?device_id=${deviceId}`,
{ {