Feat(Audio) - Enhance audio handling during video playback; pause main music, play extracted audio on a separate channel, and ensure cleanup of temporary audio files.

# Conflicts:
#	temp_audio.mp3
This commit is contained in:
Félix MARQUET
2025-04-10 09:32:11 +02:00
parent bca7cc0cbc
commit f0b237326f
5 changed files with 38 additions and 16 deletions

BIN
assets/sound/main_music.mp3 Normal file

Binary file not shown.

View File

@@ -1,4 +1,5 @@
import pygame
import os
from src.Entity.Entity import Entity
from moviepy import VideoFileClip
import moviepy as mp
@@ -75,9 +76,16 @@ class Exit(Entity):
# Extract audio from the video
audio = mp.AudioFileClip(video_path)
audio.write_audiofile("temp_audio.mp3")
pygame.mixer.init()
pygame.mixer.music.load("temp_audio.mp3")
pygame.mixer.music.play()
# Pause the main music without stopping it
main_music_pos = (
pygame.mixer.music.get_pos() / 1000 if pygame.mixer.get_init() else 0
)
pygame.mixer.music.pause()
# Load and play the audio on a separate channel
temp_sound = pygame.mixer.Sound("temp_audio.mp3")
sound_channel = temp_sound.play()
for frame in clip.iter_frames(fps=24, dtype="uint8"):
frame_surface = pygame.surfarray.make_surface(frame.swapaxes(0, 1))
@@ -85,11 +93,22 @@ class Exit(Entity):
pygame.display.flip()
clock.tick(24)
clip.close()
pygame.mixer.music.stop()
pygame.mixer.quit()
# Check if the sound channel is still playing
if sound_channel and not sound_channel.get_busy():
break
# Create and post a return to menu event
clip.close()
# Play the main music again from the last position
pygame.mixer.music.unpause()
# Remove the temporary audio file
try:
os.remove("temp_audio.mp3")
except Exception as e:
print(f"Error removing temporary audio file: {e}")
# Return to the menu
return_event = pygame.event.Event(
pygame.USEREVENT, {"action": "return_to_menu"}
)

View File

@@ -76,16 +76,16 @@ class SpeedBoost(Entity):
Args:
player: The player object to apply the boost to
game_ressources: Game resources object containing player speed
game_resources: Game resources object containing player speed
"""
if not self.collected:
self.collected = True
# Store original movement speed
original_ACC = game_ressources.ACC
original_ACC = game_resources.ACC
# Apply boost effect
game_ressources.ACC *= self.boost_factor
game_resources.ACC *= self.boost_factor
# Set visual feedback
player.speed_boost_active = True

View File

@@ -74,6 +74,13 @@ def initialize_game_resources():
game_resources.WIDTH, game_resources.HEIGHT, game_resources.font, leaderboard_db
)
try:
pygame.mixer.music.load("assets/sound/main_music.mp3")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
except Exception as e:
print(f"Error loading main music: {e}")
return (
game_resources,
displaysurface,
@@ -458,9 +465,7 @@ def draw_playing_state(
checkpoint.activate()
# Handle exit collisions
result = handle_exits(
P1, exits, game_resources, level_file, speedrun_timer, collectibles
)
result = handle_exits(P1, exits, game_resources, level_file, speedrun_timer)
# Handle collectibles
collectibles_hit = pygame.sprite.spritecollide(P1, collectibles, False)
@@ -487,9 +492,7 @@ def draw_playing_state(
return result
def handle_exits(
P1, exits, game_resources, level_file, speedrun_timer=None, collectibles=[]
):
def handle_exits(P1, exits, game_resources, level_file, speedrun_timer=None):
"""Handle collisions with level exits"""
exits_hit = pygame.sprite.spritecollide(P1, exits, False) if exits else []
for exit in exits_hit:

Binary file not shown.