🐛 (spotify)
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
content="width=device-width,initial-scale=1.0,user-scalable=no"
|
||||
/>
|
||||
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
|
||||
<script src="https://sdk.scdn.co/spotify-player.js"></script>
|
||||
<title>Binôme</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -9,9 +9,7 @@ import {
|
||||
playOnSpotify,
|
||||
pauseOnSpotify
|
||||
} from '@/utils/spotify'
|
||||
import { ref, reactive } from '@vue/composition-api'
|
||||
|
||||
window.onSpotifyWebPlaybackSDKReady = () => {}
|
||||
import { ref, reactive, onMounted } from '@vue/composition-api'
|
||||
|
||||
export const useSpotify = (onPlay: () => void, onPause: () => void) => {
|
||||
const ready = ref(false)
|
||||
@@ -26,43 +24,46 @@ export const useSpotify = (onPlay: () => void, onPause: () => void) => {
|
||||
RootActions
|
||||
>(['setAccessToken', 'setTokenExpire', 'setHasSpotify'])
|
||||
|
||||
window.onSpotifyWebPlaybackSDKReady = async () => {
|
||||
if (!hasValidAccessToken.value) {
|
||||
redirectToSpotifyConnect()
|
||||
return
|
||||
}
|
||||
|
||||
spotify.player = new Spotify.Player({
|
||||
name: 'binome',
|
||||
getOAuthToken: (callback) => callback(accessToken.value)
|
||||
})
|
||||
spotify.player.addListener('ready', ({ device_id }) => {
|
||||
deviceId.value = device_id
|
||||
ready.value = true
|
||||
})
|
||||
spotify.player.addListener('player_state_changed', (state) => {
|
||||
console.log('player state changed', state)
|
||||
if (!state) {
|
||||
onMounted(() => {
|
||||
window.onSpotifyWebPlaybackSDKReady = async () => {
|
||||
if (!hasValidAccessToken.value) {
|
||||
redirectToSpotifyConnect()
|
||||
return
|
||||
}
|
||||
!state.paused ? onPlay() : onPause()
|
||||
})
|
||||
spotify.player.addListener('not_ready', () => {
|
||||
setAccessToken(null)
|
||||
setTokenExpire(null)
|
||||
setHasSpotify(false)
|
||||
})
|
||||
spotify.player.on('playback_error', (error) => {
|
||||
console.error('Failed to perform playback', error)
|
||||
})
|
||||
spotify.player.on('account_error', (error) => {
|
||||
console.error('account_error', error)
|
||||
})
|
||||
spotify.player.on('initialization_error', (error) => {
|
||||
console.error('initialization_error', error)
|
||||
})
|
||||
await spotify.player.connect()
|
||||
}
|
||||
|
||||
spotify.player = new Spotify.Player({
|
||||
name: 'binome',
|
||||
getOAuthToken: (callback) => callback(accessToken.value)
|
||||
})
|
||||
spotify.player.addListener('ready', ({ device_id }) => {
|
||||
deviceId.value = device_id
|
||||
ready.value = true
|
||||
})
|
||||
spotify.player.addListener('player_state_changed', (state) => {
|
||||
console.log('player state changed', state)
|
||||
if (!state) {
|
||||
return
|
||||
}
|
||||
!state.paused ? onPlay() : onPause()
|
||||
})
|
||||
spotify.player.addListener('not_ready', () => {
|
||||
setAccessToken(null)
|
||||
setTokenExpire(null)
|
||||
setHasSpotify(false)
|
||||
})
|
||||
spotify.player.on('playback_error', (error) => {
|
||||
console.error('Failed to perform playback', error)
|
||||
})
|
||||
spotify.player.on('account_error', (error) => {
|
||||
console.error('account_error', error)
|
||||
})
|
||||
spotify.player.on('initialization_error', (error) => {
|
||||
console.error('initialization_error', error)
|
||||
})
|
||||
await spotify.player.connect()
|
||||
}
|
||||
import('@/lib/spotify-player')
|
||||
})
|
||||
|
||||
const play = async () => {
|
||||
try {
|
||||
|
||||
@@ -13,7 +13,10 @@ export const redirectToSpotifyConnect = () => {
|
||||
)
|
||||
authorizeURL.searchParams.append('response_type', 'token')
|
||||
authorizeURL.searchParams.append('show_dialog', 'false')
|
||||
authorizeURL.searchParams.append('scope', 'user-modify-playback-state')
|
||||
authorizeURL.searchParams.append(
|
||||
'scope',
|
||||
'streaming,user-modify-playback-state'
|
||||
)
|
||||
authorizeURL.searchParams.append('redirect_uri', `${location.origin}/spotify`)
|
||||
|
||||
location.href = authorizeURL.toString()
|
||||
|
||||
Reference in New Issue
Block a user