Spaces:
Runtime error
Runtime error
File size: 5,888 Bytes
05b45a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
export class PlayerControls {
constructor(audioService, playerState) {
this.audioService = audioService;
this.playerState = playerState;
this.elements = {
playPauseBtn: document.getElementById('play-pause-btn'),
seekSlider: document.getElementById('seek-slider'),
volumeSlider: document.getElementById('volume-slider'),
speedSlider: document.getElementById('speed-slider'),
speedValue: document.getElementById('speed-value'),
timeDisplay: document.getElementById('time-display'),
cancelBtn: document.getElementById('cancel-btn')
};
this.setupEventListeners();
this.setupAudioEvents();
this.setupStateSubscription();
this.timeUpdateInterval = null;
}
formatTime(secs) {
const minutes = Math.floor(secs / 60);
const seconds = Math.floor(secs % 60);
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
startTimeUpdate() {
this.stopTimeUpdate(); // Clear any existing interval
this.timeUpdateInterval = setInterval(() => {
this.updateTimeDisplay();
}, 100); // Update every 100ms for smooth tracking
}
stopTimeUpdate() {
if (this.timeUpdateInterval) {
clearInterval(this.timeUpdateInterval);
this.timeUpdateInterval = null;
}
}
updateTimeDisplay() {
const currentTime = this.audioService.getCurrentTime();
const duration = this.audioService.getDuration();
// Update time display
this.elements.timeDisplay.textContent =
`${this.formatTime(currentTime)} / ${this.formatTime(duration || 0)}`;
// Update seek slider
if (duration > 0 && !this.elements.seekSlider.dragging) {
this.elements.seekSlider.value = (currentTime / duration) * 100;
}
// Update state
this.playerState.setTime(currentTime, duration);
}
setupEventListeners() {
// Play/Pause button
this.elements.playPauseBtn.addEventListener('click', () => {
if (this.audioService.isPlaying()) {
this.audioService.pause();
} else {
this.audioService.play();
}
});
// Seek slider
this.elements.seekSlider.addEventListener('mousedown', () => {
this.elements.seekSlider.dragging = true;
});
this.elements.seekSlider.addEventListener('mouseup', () => {
this.elements.seekSlider.dragging = false;
});
this.elements.seekSlider.addEventListener('input', (e) => {
const duration = this.audioService.getDuration();
const seekTime = (duration * e.target.value) / 100;
this.audioService.seek(seekTime);
this.updateTimeDisplay();
});
// Volume slider
this.elements.volumeSlider.addEventListener('input', (e) => {
const volume = e.target.value / 100;
this.audioService.setVolume(volume);
this.playerState.setVolume(volume);
});
// Speed slider
this.elements.speedSlider.addEventListener('input', (e) => {
const speed = parseFloat(e.target.value);
this.elements.speedValue.textContent = speed.toFixed(1);
this.playerState.setSpeed(speed);
});
// Cancel button
this.elements.cancelBtn.addEventListener('click', () => {
this.audioService.cancel();
this.playerState.reset();
this.updateControls({ isGenerating: false });
this.stopTimeUpdate();
});
}
setupAudioEvents() {
this.audioService.addEventListener('play', () => {
this.elements.playPauseBtn.textContent = 'Pause';
this.playerState.setPlaying(true);
this.startTimeUpdate();
});
this.audioService.addEventListener('pause', () => {
this.elements.playPauseBtn.textContent = 'Play';
this.playerState.setPlaying(false);
this.stopTimeUpdate();
});
this.audioService.addEventListener('ended', () => {
this.elements.playPauseBtn.textContent = 'Play';
this.playerState.setPlaying(false);
this.stopTimeUpdate();
});
// Initial time display
this.updateTimeDisplay();
}
setupStateSubscription() {
this.playerState.subscribe(state => this.updateControls(state));
}
updateControls(state) {
// Update button states
this.elements.playPauseBtn.disabled = !state.duration && !state.isGenerating;
this.elements.seekSlider.disabled = !state.duration;
this.elements.cancelBtn.style.display = state.isGenerating ? 'block' : 'none';
// Update volume and speed if changed externally
if (this.elements.volumeSlider.value !== state.volume * 100) {
this.elements.volumeSlider.value = state.volume * 100;
}
if (this.elements.speedSlider.value !== state.speed.toString()) {
this.elements.speedSlider.value = state.speed;
this.elements.speedValue.textContent = state.speed.toFixed(1);
}
}
cleanup() {
this.stopTimeUpdate();
if (this.audioService) {
this.audioService.pause();
}
if (this.playerState) {
this.playerState.reset();
}
// Reset UI elements
this.elements.playPauseBtn.textContent = 'Play';
this.elements.playPauseBtn.disabled = true;
this.elements.seekSlider.value = 0;
this.elements.seekSlider.disabled = true;
this.elements.timeDisplay.textContent = '0:00 / 0:00';
}
}
export default PlayerControls; |