From 8d7e7e1080218f1b9ad10590e505d43e8f58c596 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Fri, 1 Aug 2025 18:46:00 +0200 Subject: [PATCH] fix: no animals sound --- index.html | 1 + src/audio.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++----- src/game.js | 33 ++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 4448012..45853e7 100644 --- a/index.html +++ b/index.html @@ -32,6 +32,7 @@
+ diff --git a/src/audio.js b/src/audio.js index 0d8ad34..e200533 100644 --- a/src/audio.js +++ b/src/audio.js @@ -14,6 +14,31 @@ export class AudioSystem { // Update animal data when animals are randomized updateAnimalData(selectedAnimals) { this.animalData = selectedAnimals; + console.log('Updated animal data in audio system:', this.animalData); + } + + // Test method to check if audio files are accessible + async testAudioFile(audioPath) { + console.log(`Testing audio file: ${audioPath}`); + try { + const audio = new Audio(audioPath); + return new Promise((resolve, reject) => { + audio.addEventListener('canplaythrough', () => { + console.log(`✅ Audio file test successful: ${audioPath}`); + resolve(true); + }); + + audio.addEventListener('error', (error) => { + console.error(`❌ Audio file test failed: ${audioPath}`, error); + reject(error); + }); + + audio.load(); + }); + } catch (error) { + console.error(`❌ Audio file test exception: ${audioPath}`, error); + throw error; + } } async initializeAudio() { @@ -36,7 +61,13 @@ export class AudioSystem { } if (this.audioContext && this.audioContext.state === 'suspended') { - await this.audioContext.resume(); + console.log('Audio context suspended, attempting to resume...'); + try { + await this.audioContext.resume(); + console.log('Audio context resumed successfully'); + } catch (error) { + console.error('Failed to resume audio context:', error); + } } } @@ -247,21 +278,29 @@ export class AudioSystem { async playAnimalSound(direction) { await this.ensureAudioContext(); + console.log(`Attempting to play animal sound for direction: ${direction}`); + console.log(`Animal data available:`, this.animalData); + // Try to load and play actual audio file if available if (this.animalData && this.animalData[direction]) { const animal = this.animalData[direction]; + console.log(`Animal found:`, animal); // Try to play the actual audio file try { await this.playAudioFile(animal.audioFile); - console.log(`${animal.emoji} Playing ${animal.names[0]} sound: ${animal.audioFile}`); + console.log(`${animal.emoji} Successfully played ${animal.names[0]} sound: ${animal.audioFile}`); return; } catch (error) { - console.warn(`Could not play audio file ${animal.audioFile}, using placeholder sound`); + console.warn(`Could not play audio file ${animal.audioFile}:`, error); + console.log('Falling back to placeholder sound'); } + } else { + console.log('No animal data available, using placeholder sound'); } // Fallback to placeholder sounds for different directions + console.log(`Playing placeholder sound for direction: ${direction}`); switch (direction) { case 'up': // High pitched chirp @@ -289,6 +328,7 @@ export class AudioSystem { console.log('🐎 Playing horse sound placeholder'); break; default: + console.log('Playing default move sound'); this.playMoveSound(); } } @@ -359,16 +399,35 @@ export class AudioSystem { return new Promise((resolve, reject) => { const audio = new Audio(audioPath); + console.log(`Attempting to load audio file: ${audioPath}`); + audio.addEventListener('canplaythrough', () => { + console.log(`Audio file loaded successfully: ${audioPath}`); audio.play() - .then(() => resolve()) - .catch(reject); + .then(() => { + console.log(`Audio file played successfully: ${audioPath}`); + resolve(); + }) + .catch(error => { + console.error(`Failed to play audio file ${audioPath}:`, error); + reject(error); + }); }); - audio.addEventListener('error', reject); + audio.addEventListener('error', (error) => { + console.error(`Failed to load audio file ${audioPath}:`, error); + reject(error); + }); + + audio.addEventListener('loadstart', () => { + console.log(`Started loading audio file: ${audioPath}`); + }); // Set volume audio.volume = this.gainNode ? this.gainNode.gain.value : 0.3; + + // Try to load the audio file + audio.load(); }); } diff --git a/src/game.js b/src/game.js index a17d55c..398a08a 100644 --- a/src/game.js +++ b/src/game.js @@ -35,6 +35,7 @@ export class Game { setupControls() { const newGameBtn = document.getElementById('newGameBtn'); const debugToggle = document.getElementById('debugToggle'); + const testAudioBtn = document.getElementById('testAudioBtn'); const soundToggle = document.getElementById('soundToggle'); const speechToggle = document.getElementById('speechToggle'); const vibrationToggle = document.getElementById('vibrationToggle'); @@ -64,6 +65,19 @@ export class Game { } }); + testAudioBtn.addEventListener('click', async () => { + console.log('🔊 Manual audio test triggered'); + await this.audioSystem.ensureAudioContext(); + try { + await this.audioSystem.testAudioFile('sounds/bee_danger.mp3'); + console.log('✅ Manual audio test successful'); + alert('Audio test successful! Check console for details.'); + } catch (error) { + console.error('❌ Manual audio test failed:', error); + alert('Audio test failed! Check console for details.'); + } + }); + soundToggle.addEventListener('click', () => { this.soundEnabled = !this.soundEnabled; soundToggle.textContent = `🔊 Sound: ${this.soundEnabled ? 'ON' : 'OFF'}`; @@ -265,6 +279,13 @@ export class Game { // Initialize the UI with current animals this.updateAnimalDisplay(); + // Initialize audio system with current animals + const selectedAnimals = this.inputHandler.getSelectedAnimals(); + this.audioSystem.updateAnimalData(selectedAnimals); + + // Test audio file accessibility + this.testAudioSystem(); + // Initialize debug info if debug mode is on if (this.debugMode) { this.updateDebugInfo(); @@ -273,6 +294,18 @@ export class Game { requestAnimationFrame((time) => this.gameLoop(time)); } + // Test audio system + async testAudioSystem() { + console.log('🎵 Testing audio system...'); + try { + await this.audioSystem.testAudioFile('sounds/bee_danger.mp3'); + console.log('🎵 Audio system test passed!'); + } catch (error) { + console.error('🎵 Audio system test failed:', error); + console.log('🎵 Will use fallback synthesized sounds'); + } + } + // Restart game with new randomized animals newGame() { // Reset player position