I'm trying to shuffle a list of mp3 files and then play through them with pyglet. Once the player runs out of sources I want to re-shuffle the list, and then play through them again. This process would happen infinitely. I have been able to get pyglet to play through the sources once, but the on_player_eos event is never fired when I run out of sources. I'm not sure exactly what I am doing wrong, or if I am approaching it in the wrong way. I've tried a couple ways of doing it, but have had no luck getting it to work. Here is the code I have also tried replacing the 'player.on_player_eos = on_player_eos' with But it doesnt seem to do anything either. Any help appreciated. |
import os, sys, random, pyglet
from random import shuffle
song1 = pyglet.media.load('jump.wav', streaming=False)
song2 = pyglet.media.load('pickup.wav', streaming=False)
song3 = pyglet.media.load('laser.wav', streaming=False)
player = pyglet.media.Player()
# Play beatz
def beatz():
playlist = [song1, song2, song3]
shuffle(playlist)
player.queue(playlist[0])
player.queue(playlist[1])
player.queue(playlist[2])
player.play()
def on_player_eos():
print('repeating')
beatz()
player.on_player_eos = on_player_eos
if __name__ == "__main__":
beatz()
pyglet.app.run()Ah, I realize that my reply wasn't clear. The code I posted works fine. If it doesn't work for you, please share your error message.
--
You received this message because you are subscribed to a topic in the Google Groups "pyglet-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyglet-users/uxgMsN-i6Ro/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyglet-users...@googlegroups.com.
To post to this group, send email to pyglet...@googlegroups.com.
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.


Yeah, I kept on getting the same error you mentioned of the source being queued already if they weren't static. So setting them as static resources was vital. I did have some problems with certain sounds, for some reason one mp3 files on_player_eos event wasnt firing, but all the other sources I had worked fine.I'm now trying to add a few other things to my code, running Terminal's 'say' command on top of the music being played with pyglet. I play the songs I have, and then have terminal pick random words out of a list that then 'says' them repeatedly. It seems to work at first, but then freezes, running the code forever and not responding to any window events until I force quit the Python interpreter.So my question is, is it possible to run commands in the command line while a pyglet application is running without the application freezing?Here is my code
StreamingSource
A source that is decoded as it is being played, and can only be queued once."--
Subprocess.popen is non blocking, fyi.
Subprocess.popen is non blocking, fyi.
An update - I was able to find a solution to the hang that was happening when I would try closing my pyglet application. I replaced subprocess.call with subprocess.popen, which seems better, but this wasn't what was actually causing the problem. It was the timer I was using from the threading module. It looks like the timer was never being stopped on close, and that was affecting how pyglet was closing. So i added 't.cancel' on closing events to fix it. Here is the updated code:
pyglet.options['audio'] = ('openal', 'silent')
window = pyglet.window.Window()
be = ['is', 'was', 'will be', 'is not']
song2 = pyglet.media.load('beats/knock.mp3', streaming=False)
song12 = pyglet.media.load('beats/drop-2.mp3', streaming=False)
player = pyglet.media.Player()
def beatz():
playlist = [song2, song12]
shuffle(playlist)
player.queue(playlist[0])
player.queue(playlist[1])
player.play()
def on_player_eos():
print('repeating')
beatz()
player.push_handlers(on_player_eos)
#### WORDS STUFF #####
barLen = list(range(1,10))
timez = [.5, .75, 1.0, 1.5, 2.0]
def rap():
t = Timer(random.choice(timez), rap)
@window.event
def on_key_press(symbol, modifiers):
if symbol == key.A:
r.terminate()
t.cancel()
print('exiting')
pyglet.app.exit()
@window.event
def on_close():
r.terminate()
t.cancel()
print('exiting')
pyglet.app.exit()
bar = ' '.join(random.choice(be) for _ in range(random.choice(barLen)))
r = subprocess.Popen('say ' + bar, shell=True)
r.wait()
sys.stdout.flush()
t.start()
def freestyle():
beatz()
t = Timer(1.5, rap)
t.start()
if __name__ == "__main__":
freestyle()
pyglet.app.run()
This works great. Thanks so much for all your help!