Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop playback with ESC key #17

Open
kaziu007 opened this issue Aug 21, 2024 · 0 comments
Open

Stop playback with ESC key #17

kaziu007 opened this issue Aug 21, 2024 · 0 comments

Comments

@kaziu007
Copy link

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}")

[...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant