I'm currently integrating Spotify's web player SDK into an Angular project. I followed the steps outlined in this particular question and answer: Installing the types package, then including the types reference at the beginning of the file where I'm utilizing the Spotify namespace
npm install --save @types/spotify-web-playback-sdk
and
/// <reference types="@types/spotify-web-playback-sdk"/>
as the initial line in the file.
Here's a snippet of my code:
//class variable
spotifyPlayer: Spotify.Player | null = null
and
createWebPlayer(){
const accessToken = this.authObject.access_token
this.spotifyPlayer = new Spotify.Player({ //line 107
name: 'Web Playback SDK Quick Start Player',
getOAuthToken: cb => {
cb(accessToken)
},
volume: 0.5
})
//ready
this.spotifyPlayer!.addListener('ready', ({ device_id }) => {
console.log('Ready with Device ID', device_id);
});
// Not Ready
this.spotifyPlayer!.addListener('not_ready', ({ device_id }) => {
console.log('Device ID has gone offline', device_id);
});
}
The code compiles without errors, but I encounter the following error when calling createWebPlayer()
:
ERROR ReferenceError: Spotify is not defined
createWebPlayer home.component.ts:107
I'm manually calling the function and ensuring I have an access token before doing so, so that shouldn't be the issue; I'm quite puzzled by this.
Things I've tried
I attempted to include an import statement like this:
import * as Spotify from 'spotify-web-playback-sdk'
However, I receive an error stating that it's not a module. The only installation I did was running
npm install --save @types/spotify-web-playback-sdk
I also experimented with changing line 107 to
this.spotifyPlayer = new window.Spotify.Player({
but encountered a similar error:
ERROR ReferenceError: window.Spotify is not defined
createWebPlayer home.component.ts:107
I'm genuinely baffled and feel like I might be overlooking something obvious. Any assistance would be greatly appreciated!!