164 lines
3.9 KiB
TypeScript
164 lines
3.9 KiB
TypeScript
import {Component, AfterViewInit, signal, OnInit} from '@angular/core';
|
|
import {ViewportService} from '../services/viewport.service';
|
|
import {NgClass} from '@angular/common';
|
|
|
|
|
|
declare global {
|
|
interface Window {
|
|
onYouTubeIframeAPIReady: () => void;
|
|
YT: any;
|
|
}
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-audio-player',
|
|
imports: [
|
|
NgClass
|
|
],
|
|
templateUrl: './audio-player.component.html',
|
|
styleUrl: './audio-player.component.css'
|
|
})
|
|
export class AudioPlayerComponent implements AfterViewInit, OnInit {
|
|
isNarrow: boolean = false;
|
|
private player: any;
|
|
|
|
isPlayerReady = signal(false);
|
|
isPlaying = signal(false);
|
|
nowPlayingTitle = signal<string>('');
|
|
volume = signal(50)
|
|
|
|
constructor(private deviceService: ViewportService) {}
|
|
|
|
ngOnInit() {
|
|
this.isNarrow = this.deviceService.isNarrow;
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
void this.loadYouTubeApi().then(() => {
|
|
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',
|
|
width: '0',
|
|
playerVars: {
|
|
listType: 'playlist',
|
|
list: 'PLukBTTWD2ebbIa_RDec_razIHz6e6Wh8b',
|
|
autoplay: 0,
|
|
controls: 0,
|
|
},
|
|
events: {
|
|
onReady: () => {
|
|
this.isPlayerReady.set(true);
|
|
this.player.setVolume(this.volume());
|
|
|
|
const iframe = document.getElementById('youtube-player') as HTMLIFrameElement | null;
|
|
if (iframe) {
|
|
iframe.style.width = '0px';
|
|
iframe.style.height = '0px';
|
|
iframe.style.opacity = '0';
|
|
iframe.style.pointerEvents = 'none';
|
|
iframe.style.position = 'absolute';
|
|
iframe.style.zIndex = '-1';
|
|
}
|
|
|
|
this.updateNowPlayingTitle();
|
|
},
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
updateNowPlayingTitle() {
|
|
if (this.player) {
|
|
const videoData = this.player.getVideoData();
|
|
this.nowPlayingTitle.set(videoData.title);
|
|
}
|
|
}
|
|
|
|
playVideo() {
|
|
if (this.isPlayerReady() && this.player?.playVideo) {
|
|
this.player.playVideo();
|
|
this.isPlaying.set(true);
|
|
}
|
|
}
|
|
|
|
pauseVideo() {
|
|
if (this.isPlayerReady() && this.player?.pauseVideo) {
|
|
this.player.pauseVideo();
|
|
this.isPlaying.set(false);
|
|
}
|
|
}
|
|
|
|
togglePlayPause() {
|
|
if (!this.isPlayerReady()) return;
|
|
|
|
if (this.isPlaying()) {
|
|
this.pauseVideo();
|
|
} else {
|
|
this.playVideo();
|
|
}
|
|
}
|
|
|
|
nextVideo() {
|
|
if (this.isPlayerReady() && this.player?.nextVideo) {
|
|
this.player.nextVideo();
|
|
}
|
|
}
|
|
|
|
previousVideo() {
|
|
if (this.isPlayerReady() && this.player?.previousVideo) {
|
|
this.player.previousVideo();
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|