fix: no animals sound

This commit is contained in:
Krzysztof Rudnicki 2025-08-01 18:46:00 +02:00
parent cef204a973
commit 8d7e7e1080
3 changed files with 99 additions and 6 deletions

View File

@ -32,6 +32,7 @@
<div class="controls">
<button id="newGameBtn">🎲 New Game</button>
<button id="debugToggle">🐛 Debug: OFF</button>
<button id="testAudioBtn">🔊 Test Audio</button>
<button id="soundToggle">🔊 Sound: ON</button>
<button id="speechToggle">🗣️ Speech: ON</button>
<button id="vibrationToggle">📳 Vibration: ON</button>

View File

@ -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') {
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();
});
}

View File

@ -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