I have a simple code to play audios but it seems pyglet saves all audios and every time it tries to play a new audio all previous audios are being played at the same time.
import pyglet
def plays(name):
file = pyglet.resource.media(name + '.mp3')
file.play()
pyglet.app.event_loop.sleep(1)
plays('definite')
plays('filling-your-inbox')
is there any way to play the second one without replaying the first?
The sound isn't cached, and the code you're using aren't really that useful either. If you listen carefully, both of your files are actually played and overlapped with each other. The easiest way to distinguish this is to have the first one be a slow mellow tune, and then the second a sharp short burst of sound - and you'll notice it easier.
The other problem is that you're looping the play function and not really using the proper event loops. A more proper example would be to do something like this:
import pyglet, signal
media_player = pyglet.media.Player()
def queue(name):
file = pyglet.resource.media(name + '.mp3')
media_player.queue(file)
def on_eos(*args, **kwargs):
pyglet.app.exit()
media_player.on_player_eos = on_eos
queue('definite')
queue('filling-your-inbox')
media_player.play()
pyglet.app.run()
Side note is that you should probably use the push-event handler instead of just replacing the function like I did, I'm a bit in a hurry so it's a bit ugly, but it works :)