Working on getting the menu music to actually loop. Menu music is no longer tied to ogg specifically.
This commit is contained in:
parent
4aa3475ac4
commit
8264db4a2f
2
menu.py
2
menu.py
@ -26,7 +26,7 @@ class Menu:
|
||||
try:
|
||||
if self.game.sound.currentBgm:
|
||||
self.game.sound.currentBgm.pause()
|
||||
self.game.sound.play_bgm("sounds/music_menu.ogg")
|
||||
self.game.sound.play_bgm("music_menu")
|
||||
except:
|
||||
pass
|
||||
|
||||
|
53
sound.py
53
sound.py
@ -125,25 +125,54 @@ class Sound:
|
||||
|
||||
return filtered_sounds
|
||||
|
||||
def play_bgm(self, music_file):
|
||||
"""Play background music with proper volume.
|
||||
|
||||
def play_bgm(self, music_name):
|
||||
"""Play background music with proper volume and looping.
|
||||
|
||||
Args:
|
||||
music_file (str): Path to music file
|
||||
music_name (str): Name of the music file in the sounds directory
|
||||
Can be just the name (e.g., "music_menu")
|
||||
or include subdirectory (e.g., "music/title")
|
||||
"""
|
||||
try:
|
||||
# Clean up old player
|
||||
if self.currentBgm:
|
||||
self.currentBgm.pause()
|
||||
|
||||
# Load and play new music
|
||||
music = pyglet.media.load(music_file, streaming=True)
|
||||
player = pyglet.media.Player()
|
||||
player.queue(music)
|
||||
player.loop = True
|
||||
player.volume = self.bgmVolume * self.masterVolume
|
||||
player.play()
|
||||
self.currentBgm = None
|
||||
|
||||
# Check if the music is in the loaded sounds library
|
||||
if music_name in self.sounds:
|
||||
# Use the already loaded sound
|
||||
music = self.sounds[music_name]
|
||||
music_file = None
|
||||
else:
|
||||
# Try to load with extensions if not found
|
||||
for ext in ['.ogg', '.wav', '.opus']:
|
||||
if music_name.endswith(ext):
|
||||
music_file = f"sounds/{music_name}"
|
||||
break
|
||||
elif os.path.exists(f"sounds/{music_name}{ext}"):
|
||||
music_file = f"sounds/{music_name}{ext}"
|
||||
break
|
||||
|
||||
# If we didn't find a file, try the direct path
|
||||
if not music_file:
|
||||
music_file = f"sounds/{music_name}"
|
||||
if not os.path.exists(music_file):
|
||||
music_file += ".ogg" # Default to .ogg if no extension
|
||||
|
||||
# Load the music file
|
||||
music = pyglet.media.load(music_file, streaming=True)
|
||||
|
||||
# Create and configure player
|
||||
player = pyglet.media.Player()
|
||||
player.volume = self.bgmVolume * self.masterVolume
|
||||
player.queue(music)
|
||||
player.play()
|
||||
player.on_eos = lambda: (player.queue(music), player.play())
|
||||
|
||||
# Store reference
|
||||
self.currentBgm = player
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error playing background music: {e}")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user