// Баркер плеер с поддержкой изменения громкости в реальном времени const { UniversalAudioPlayer } = require('./universal-audio-player'); const path = require('path'); class BarkerPlayer { constructor() { this.players = new Map(); this.currentPlayer = null; this.currentSoundId = null; this.volume = 70; this.isPlaying = false; // Загружаем все звуки баркера this.sounds = { '1-1': './audio/barker/1-1.wav', '1-2': './audio/barker/1-2.wav', '1-3': './audio/barker/1-3.wav', '1-4': './audio/barker/1-4.wav', '1-5': './audio/barker/1-5.wav', '2-1': './audio/barker/2-1.wav', '2-2': './audio/barker/2-2.wav', '2-3': './audio/barker/2-3.wav', '2-4': './audio/barker/2-4.wav' }; console.log('[BARKER-PLAYER] Инициализирован с поддержкой изменения громкости'); } play(soundId, onStart, onEnd) { // Останавливаем текущее воспроизведение this.stop(); if (!this.sounds[soundId]) { console.error('[BARKER-PLAYER] Неизвестный звук:', soundId); return; } console.log('[BARKER-PLAYER] Воспроизведение звука:', soundId); // Создаем новый плеер для этого звука const player = new UniversalAudioPlayer(); player.setVolume(this.volume); this.currentPlayer = player; this.currentSoundId = soundId; this.isPlaying = true; // Получаем абсолютный путь к файлу const filePath = path.resolve(__dirname, this.sounds[soundId]); // Вызываем callback начала if (onStart) onStart(); // Запускаем воспроизведение player.play(filePath); // Отслеживаем окончание воспроизведения const checkInterval = setInterval(() => { if (!player.isPlaying) { clearInterval(checkInterval); this.isPlaying = false; this.currentPlayer = null; this.currentSoundId = null; console.log('[BARKER-PLAYER] Воспроизведение завершено:', soundId); if (onEnd) onEnd(); } }, 100); } stop() { if (this.currentPlayer) { console.log('[BARKER-PLAYER] Остановка воспроизведения'); this.currentPlayer.stop(); this.currentPlayer = null; this.currentSoundId = null; this.isPlaying = false; } } setVolume(volume) { this.volume = Math.max(0, Math.min(100, volume)); console.log('[BARKER-PLAYER] Установка громкости:', this.volume); // Применяем громкость к текущему плееру если он играет if (this.currentPlayer && this.isPlaying) { this.currentPlayer.setVolume(this.volume); console.log('[BARKER-PLAYER] Громкость применена к активному звуку'); } } getVolume() { return this.volume; } } module.exports = BarkerPlayer;