added audio player to portfolio

This commit is contained in:
itzMiney committed 2025-04-06 23:09:08 +02:00
1 parent 73e35736fb
commit 4b5cb750cf
7 files changed
+157 -2

No files matched your search

@@ -0,0 +1,31 @@
.controls {
display: flex;
align-items: center;
gap: 10px;
}
.controls button {
font-family: Roboto, sans-serif;
font-size: 1rem;
font-weight: 500;
border: none;
padding: 10px;
height: 42px;
width: 42px;
border-radius: 25px;
}
.audio-player {
position: fixed;
bottom: 10px;
left: 20px;
z-index: 10;
}
.audio-player h3 {
margin-left: 15px;
text-align: center;
font-family: Roboto, sans-serif;
font-weight: 500;
color: white;
}
@@ -0,0 +1,11 @@
<div class="audio-player">
<div id="youtube-player"></div>
<div class="controls">
<button (click)="previousVideo()" [disabled]="!isPlayerReady"><i class="fa-solid fa-backward"></i></button>
<button (click)="togglePlayPause()" [disabled]="!isPlayerReady">
<i class="{{ isPlaying() ? 'fa-solid fa-pause' : 'fa-solid fa-play'}}"></i>
</button>
<button (click)="nextVideo()" [disabled]="!isPlayerReady"><i class="fa-solid fa-forward"></i></button>
<h3>{{ nowPlayingTitle() }}</h3>
</div>
</div>
@@ -0,0 +1,108 @@
import {Component, AfterViewInit, signal} from '@angular/core';
declare global {
interface Window {
onYouTubeIframeAPIReady: () => void;
YT: any;
}
}
@Component({
selector: 'app-audio-player',
imports: [],
templateUrl: './audio-player.component.html',
styleUrl: './audio-player.component.css'
})
export class AudioPlayerComponent implements AfterViewInit {
private player: any;
isPlayerReady = signal(false);
isPlaying = signal(false);
nowPlayingTitle = signal<string>('');
ngAfterViewInit() {
if (window.YT && window.YT.Player) {
this.createPlayer();
} else {
window.onYouTubeIframeAPIReady = () => this.createPlayer();
}
}
createPlayer() {
this.player = new window.YT.Player('youtube-player', {
height: '0', // or something minimal
width: '0',
playerVars: {
listType: 'playlist',
list: 'PLukBTTWD2ebbIa_RDec_razIHz6e6Wh8b',
autoplay: 0,
controls: 0,
},
events: {
onReady: () => {
this.isPlayerReady.set(true);
console.log('YouTube Player Ready');
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.updateNowPlayingTitle();
}
}
}
});
}
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();
}
}
}