Search code examples
pythonpyglet

Python pyglet repeats playing the audio non stop


The play sound (should be a short beep) doesn't stop. I made a short test case below. The IDE is VS2019.

import cv2
import numpy as np
import dlib
from math import hypot
import pyglet
import time

# Load sounds
sound = pyglet.media.load("sound.wav", streaming=False)
left_sound = pyglet.media.load("left.wav", streaming=False)
right_sound = pyglet.media.load("right.wav", streaming=False)
sound.play()
while True:
    print("***")

How can I make it play once the beep? (without time measuring)


Solution

  • Instead of while True loop use pyglet.app.run():

    import pyglet
    
    sound = pyglet.resource.media('sound.wav')
    sound.play()
    
    pyglet.app.run()
    

    See pyglet documentation