I've been trying to implement the Spotify Web Player on Angular, following this tutorial on the Spotify Documentation Page (although it is shown in React). My goal is to have a web player like the one here at the bottom of the page Although I'm getting a success with my spotify authentication and the Web Player is console logging "Ready with Device ID XXXXX", the player does not show up in my component. Is there something I need to add to my component html? I'm stuck with this.
The gist is that on initialization of the player component, I am calling my sdk service to create the web player. Thats it. I think theres a missing step but i dont know what it is. I've tried assigning prop in my player component and assigning it to a return value in the service, and calling it in my html file ,but nothing shows up as well.
here is my current code:
spotify-sdk-web-player.service.ts
import { Injectable } from '@angular/core';
import { SpotifyAuthService } from '../auth/spotify-auth.service';
/// <reference types="../node_modules/@types/spotify-web-playback-sdk/index.d.ts"/>
declare global {
interface window {
onSpotifyWebPlaybackSDKReady: () => void;
spotifyReady: Promise<void>;
}
}
@Injectable({
providedIn: 'root',
})
export class SpotifySdkWebPlayerService {
spotifyPlayer!: Spotify.Player;
// private spotifyToken!: string;
constructor(private spotifyAuth: SpotifyAuthService) {}
createWebPlayer(spotifyToken: string) {
const script = document.createElement('script');
script.src = 'https://sdk.scdn.co/spotify-player.js';
script.type = 'text/javascript';
script.addEventListener('load', (e) => {
console.log(e);
});
document.head.appendChild(script);
window.onSpotifyWebPlaybackSDKReady = () => {
console.log(
'The Web Playback SDK is ready. We have access to Spotify.Player'
);
this.spotifyPlayer = new Spotify.Player({
name: 'Web Playback SDK Quick Start Player',
getOAuthToken: (cb) => {
cb(spotifyToken);
},
volume: 0.5,
});
this.spotifyPlayer.addListener('ready', ({ device_id }) => {
console.log('Ready with Device ID', device_id);
});
this.spotifyPlayer.addListener('not_ready', ({ device_id }) => {
console.log('Device ID has gone offline', device_id);
});
this.spotifyPlayer.addListener('initialization_error', ({ message }) => {
console.error(message);
});
this.spotifyPlayer.addListener('authentication_error', ({ message }) => {
console.error(message);
});
this.spotifyPlayer.addListener('account_error', ({ message }) => {
console.error(message);
});
this.spotifyPlayer.connect();
};
}
}
player.component.ts
import { Component, OnDestroy, OnInit } from '@angular/core';
import { SpotifyAuthService } from 'src/app/services/auth/spotify-auth.service';
import { SpotifySdkWebPlayerService } from 'src/app/services/spotify/spotify-sdk-web-player.service';
/// <reference path="../node_modules/@types/spotify-web-playback-sdk/index.d.ts"/>
@Component({
selector: 'app-player',
templateUrl: './player.component.html',
styleUrls: ['./player.component.css'],
})
export class PlayerComponent implements OnInit {
constructor( private spotifyAuth: SpotifyAuthService,
private spotifyWebSDK: SpotifySdkWebPlayerService
) {}
ngOnInit(): void {
this.spotifyAuth
.getSpotifyToken()
.then((token: any) => this.spotifyWebSDK.createWebPlayer(token['token']));
}
}
here are my console logs
Appreciate any help and /or direction with this