Update sounds.py
Browse files
sounds.py
CHANGED
@@ -28,60 +28,40 @@ class AudioManager:
|
|
28 |
|
29 |
def get_audio_html(self) -> str:
|
30 |
"""Generate HTML for all audio elements"""
|
31 |
-
|
32 |
-
|
33 |
-
# Background music (with loop)
|
34 |
-
if 'bgm' in self._audio_cache:
|
35 |
-
audio_elements.append(f"""
|
36 |
-
<audio id="bgm" loop>
|
37 |
-
<source src="data:audio/mp3;base64,{self._audio_cache['bgm']}" type="audio/mp3">
|
38 |
-
</audio>
|
39 |
-
""")
|
40 |
-
|
41 |
-
# Sound effects (without loop)
|
42 |
-
for name, base64_data in self._audio_cache.items():
|
43 |
-
if name != 'bgm':
|
44 |
-
audio_elements.append(f"""
|
45 |
-
<audio id="sound_{name}">
|
46 |
-
<source src="data:audio/mp3;base64,{base64_data}" type="audio/mp3">
|
47 |
-
</audio>
|
48 |
-
""")
|
49 |
-
|
50 |
-
# Add JavaScript controls
|
51 |
-
audio_elements.append("""
|
52 |
-
<script>
|
53 |
-
// Play specific sound
|
54 |
-
function playSound(id) {
|
55 |
-
const sound = document.getElementById(id);
|
56 |
-
if (sound) {
|
57 |
-
sound.currentTime = 0;
|
58 |
-
sound.play();
|
59 |
-
}
|
60 |
-
}
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
bgm.pause();
|
70 |
-
}
|
71 |
-
}
|
72 |
-
}
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
def get_sound_commands() -> Dict[str, str]:
|
87 |
"""Get JavaScript commands for playing sounds"""
|
|
|
28 |
|
29 |
def get_audio_html(self) -> str:
|
30 |
"""Generate HTML for all audio elements"""
|
31 |
+
try:
|
32 |
+
audio_elements = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# Background music (with loop)
|
35 |
+
if 'bgm' in self._audio_cache:
|
36 |
+
audio_elements.append(
|
37 |
+
f'<audio id="bgm" loop preload="auto" style="display:none;">'
|
38 |
+
f'<source src="data:audio/mp3;base64,{self._audio_cache["bgm"]}" type="audio/mp3">'
|
39 |
+
f'</audio>'
|
40 |
+
)
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Sound effects (without loop)
|
43 |
+
for name, base64_data in self._audio_cache.items():
|
44 |
+
if name != 'bgm':
|
45 |
+
audio_elements.append(
|
46 |
+
f'<audio id="sound_{name}" preload="auto" style="display:none;">'
|
47 |
+
f'<source src="data:audio/mp3;base64,{base64_data}" type="audio/mp3">'
|
48 |
+
f'</audio>'
|
49 |
+
)
|
50 |
+
|
51 |
+
# Add JavaScript controls in a more concise way
|
52 |
+
audio_elements.append("""
|
53 |
+
<script>
|
54 |
+
const playSound=e=>{const t=document.getElementById(e);t&&(t.currentTime=0,t.play())},
|
55 |
+
toggleBGM=e=>{const t=document.getElementById("bgm");t&&(e?t.play():t.pause())},
|
56 |
+
setVolume=e=>{document.getElementsByTagName("audio").forEach(t=>t.volume=e)};
|
57 |
+
</script>
|
58 |
+
""".strip())
|
59 |
+
|
60 |
+
return "\n".join(audio_elements)
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
logging.error(f"Error generating audio HTML: {str(e)}")
|
64 |
+
return ""
|
65 |
|
66 |
def get_sound_commands() -> Dict[str, str]:
|
67 |
"""Get JavaScript commands for playing sounds"""
|