2

me and my friend are working on a game and we want our music to loop as long as the game is running. Help please there seems to be no function to put music on repeat

Jay
  • 25
  • 1
  • 3

6 Answers6

3

In current versions of pyglet, you should use a SourceGroup, setting the loop attribute to True. You can then queue it into a Player to play it:

snd = pyglet.media.load('sound.wav')
looper = pyglet.media.SourceGroup(snd.audio_format, None)
looper.loop = True
looper.queue(snd)
p = pyglet.media.Player()
p.queue(looper)
p.play()

Not sure if there's a more compact way of doing this but it seems to work...

mgalgs
  • 15,671
  • 11
  • 61
  • 74
1

To make a sound play in a loop, you can use a Player:

# create a player and queue the song
player = pyglet.media.Player()
sound = pyglet.media.load('lines.mp3')
player.queue(sound) 

# keep playing for as long as the app is running (or you tell it to stop):
player.eos_action = pyglet.media.SourceGroup.loop

player.play()

To play more background sounds simultaneously, just start up another player for each of the sounds, with the same EOS_LOOP "eos_action" setting as above for each of them.

David.Jones
  • 1,413
  • 8
  • 16
  • [_"Warning: Deprecated. Use SourceGroup.loop"_](https://pyglet.readthedocs.org/en/pyglet-1.2-maintenance/api/pyglet/media/pyglet.media.Player.html#pyglet.media.Player.EOS_LOOP) Can you perhaps update the answer for Pyglet 1.2.3? Thanks! – Daniel Darabos Aug 14 '15 at 13:02
1

For playing continuously you can use this code

This will allow you to play files from your root directory

import pyglet
from pyglet.window import key
import glob

window = pyglet.window.Window(1280, 720, "Python Player", resizable=True)
window.set_minimum_size(400,300)
songs=glob.glob("*.wav")
player=pyglet.media.Player()

@window.event
def on_key_press(symbol, modifiers):
    if symbol == key.ENTER:
        print("A key was pressed")
        @window.event
        def on_draw():
            global player
            for i in range(len(songs)):
                source=pyglet.resource.media(songs[i])
                player.queue(source)
            player.play()

pyglet.app.run()
0

this works for me

myplayer = pyglet.media.Player()
Path = "c:/path/to/youraudio.mp3"
source = pyglet.media.load(filename=source, streaming=False)
myplayer.queue(self.slowCaseSongSource)
myplayer.eos_action = 'loop'
0

It seems that this library have changed a lot in the last years. As of 2023 the latest version available is 2.0.9, so here it goes a working example:

import pyglet

source = pyglet.media.load(filename='alarm.wav')
player = pyglet.media.Player()
player.loop = True
player.queue(source)
player.play()

pyglet.app.run()

The player.play() call will play the sound only once. After that, pyglet.app.run() will continue to play in loop, but blocking the execution. If we want to do other things while the audio loop is playing, we will have to use threads. I made a tiny play/pause example to show how to do this:

import pyglet
from threading import Thread

source = pyglet.media.load(filename='alarm.wav')
player = pyglet.media.Player()
player.loop = True
player.queue(source)

thread = Thread(target=pyglet.app.run, daemon=True)
thread.start()

print('Press any key to play or pause!')
while True:
    _ = input()  # wait for user command
    if player.playing:
        print('PAUSING')
        player.pause()
        player.seek(0)  # (optional) rewind to the start of the source
    else:
        print('PLAYING')
        player.play()

Note that daemon=True is not actually necessary; it just makes the thread stop along with the main program.

-1

this might be irrelevant:

import pyglet
import time
import random

#WARNING: You have to download your own sounds and define them like this:
#sound_1 = pyglet.resource.media("sound_file.wav", streaming=False)
#replacing "sound_file" with your own file.

# Add the sound variables here
BACKGROUND_OPTIONS = ()

player = pyglet.media.Player()

def play_background_sound():
  global player
  player.queue(random.choice(BACKGROUND_OPTIONS))
  player.play()

  # This is optional; it's just a function that keeps the player filled so there aren't any breaks.
  def queue_sounds():
    global player
    while True:
      player.queue(random.choice(BACKGROUND_OPTIONS))
      time.sleep(60) # change this if the background music you have is shorter than 3 minutes

threading.Thread(target=queue_sounds).start()
r58
  • 1