Skip to content

Stop playback with ESC key #17

@kaziu007

Description

@kaziu007

if you want to stop the playback with the ESC key you might want to use this change in the code.Works on macos.

voice_assistant/audio.py:

[...]
def play_audio(file_path):
"""
Play an audio file using pygame.

Args:
file_path (str): The path to the audio file to play.
"""
try:
    pygame.init()
    pygame.mixer.init()
    screen = pygame.display.set_mode((1, 1))  # Create a small window to capture events
    pygame.display.set_caption("Audio Player")

    pygame.mixer.music.load(file_path)
    pygame.mixer.music.play()
    
    print("Press ESC to stop playback.")
    
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.mixer.music.stop()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
                    pygame.mixer.music.stop()
                    print("Playback stopped.")

        if not pygame.mixer.music.get_busy():
            running = False
        
        time.sleep(0.1)  # Small delay to reduce CPU usage
        
    pygame.mixer.quit()
except pygame.error as e:
    logging.error(f"Failed to play audio: {e}")
except Exception as e:
    logging.error(f"An unexpected error occurred while playing audio: {e}")

[...]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions