some cleanup n stuff ig
This commit is contained in:
1 parent
aaa1468a15
commit
62eaea0182
8 files changed
+74
-38
No files matched your search
@@ -25,6 +25,7 @@ export class AudioPlayerComponent implements AfterViewInit, OnInit {
|
||||
isPlayerReady = signal(false);
|
||||
isPlaying = signal(false);
|
||||
nowPlayingTitle = signal<string>('');
|
||||
volume = signal(50)
|
||||
|
||||
constructor(private deviceService: ViewportService) {}
|
||||
|
||||
@@ -33,16 +34,40 @@ export class AudioPlayerComponent implements AfterViewInit, OnInit {
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
if (window.YT && window.YT.Player) {
|
||||
void this.loadYouTubeApi().then(() => {
|
||||
this.createPlayer();
|
||||
} else {
|
||||
window.onYouTubeIframeAPIReady = () => this.createPlayer();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private loadYouTubeApi(): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
if (window.YT?.Player) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const existingScript = document.querySelector(
|
||||
'script[src="https://www.youtube.com/iframe_api"]'
|
||||
);
|
||||
|
||||
if (existingScript) {
|
||||
window.onYouTubeIframeAPIReady = () => resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const script = document.createElement('script');
|
||||
script.src = 'https://www.youtube.com/iframe_api';
|
||||
script.async = true;
|
||||
|
||||
window.onYouTubeIframeAPIReady = () => resolve();
|
||||
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
}
|
||||
|
||||
createPlayer() {
|
||||
this.player = new window.YT.Player('youtube-player', {
|
||||
height: '0', // or something minimal
|
||||
height: '0',
|
||||
width: '0',
|
||||
playerVars: {
|
||||
listType: 'playlist',
|
||||
@@ -54,7 +79,6 @@ export class AudioPlayerComponent implements AfterViewInit, OnInit {
|
||||
onReady: () => {
|
||||
this.isPlayerReady.set(true);
|
||||
this.player.setVolume(this.volume());
|
||||
console.log('YouTube Player Ready');
|
||||
|
||||
const iframe = document.getElementById('youtube-player') as HTMLIFrameElement | null;
|
||||
if (iframe) {
|
||||
@@ -70,7 +94,13 @@ export class AudioPlayerComponent implements AfterViewInit, OnInit {
|
||||
},
|
||||
onStateChange: (event: any) => {
|
||||
if (event.data === window.YT.PlayerState.PLAYING) {
|
||||
this.isPlaying.set(true);
|
||||
this.updateNowPlayingTitle();
|
||||
} else if (
|
||||
event.data === window.YT.PlayerState.PAUSED ||
|
||||
event.data === window.YT.PlayerState.ENDED
|
||||
) {
|
||||
this.isPlaying.set(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,15 +150,14 @@ export class AudioPlayerComponent implements AfterViewInit, OnInit {
|
||||
}
|
||||
}
|
||||
|
||||
volume = signal(50);
|
||||
|
||||
onVolumeChange(event: Event) {
|
||||
const target = event.target as HTMLInputElement;
|
||||
const newVolume = parseInt(target.value, 10);
|
||||
|
||||
this.volume.set(newVolume);
|
||||
|
||||
if (this.isPlayerReady() && this.player?.setVolume) {
|
||||
this.player.setVolume(newVolume);
|
||||
this.volume.set(newVolume);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user