5 Replies - 4256 Views - Last Post: 16 November 2016 - 11:54 AM Rate Topic: -----

#1 Hoaz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 09-April 15

[pygame] Music Queue ?

Posted 15 November 2016 - 02:48 PM

Hi. I'd like to know how to do a music queue in pygame. I have tried following various guides, but have not been successfull in doing so.
I have two files (sounds.py and game.py), I load my music in sounds.py using the following

import pygame
import os

main_dir = os.path.split(os.path.abspath(__file__))[0]
def load_music(folder, file):
    "loads music, prepares it for play"
    file = os.path.join(main_dir, folder, file)
    try:
       music = pygame.mixer.music.load(file)
    except pygame.error:
       raise SystemExit('Could not load music "%s" %s'%(file, pygame.get_error()))
    return music

def add_queue(folder, file):
    "loads music, adds it to queue"
    file = os.path.join(main_dir, folder, file)
    try:
       music = pygame.mixer.music.queue(file)
    except pygame.error:
       raise SystemExit('Could not load music "%s" %s'%(file, pygame.get_error()))
    return music

music = load_music('data/music', 'mm.wav')

msic2 = add_queue('data/music', 'mm2.wav')



In game.py I initialize the sound mixer with this
pygame.mixer.pre_init( 22050, -16, 2, 512 )
pygame.mixer.init()

and I also import sounds here. Further on I play the sound on startup using
def load_menu():
  create_background(screen_size[0], screen_size[1], images.menu_tile, (0, 0))
  if pygame.mixer.music.get_busy():
    pygame.mixer.music.set_volume(1.0)
  else:
    pygame.mixer.music.play(1)
    sounds.msic2


I want to move your attention to the last two lines of code.
Pygame.mixer.music.play(1) obviously plays the music that I initialized and the very last line makes sure the second song is added to the queue (?). Note: "sounds." is from the imported sounds file

From what I understand having a 1 will ensure that the first song is played once followed by the queued song.
This does not happen. The first song is played but not the queued.

I do not get any errors and I have tried every search result on google to no avail.

I appreciate any help!
Thanks in advance,
Hoaz

Is This A Good Question/Topic? 0
  • +

Replies To: [pygame] Music Queue ?

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 607
  • View blog
  • Posts: 1,927
  • Joined: 27-December 13

Re: [pygame] Music Queue ?

Posted 15 November 2016 - 11:29 PM

Are you sure that you actually ever run the else-part of the last code snippet?
Try to insert a print statement to be sure.
You should expect the first sound to play twice - the parameter (1) is number of repeats (after first play), not number of total play.

You don't show the full program, but please check that load_menu() is called repeatedly.
If you only run the function once and the music is already started, then you never get to the else-part.
Was This Post Helpful? 0
  • +
  • -

#3 Hoaz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 09-April 15

Re: [pygame] Music Queue ?

Posted 16 November 2016 - 03:06 AM

View PostDK3250, on 15 November 2016 - 11:29 PM, said:

Are you sure that you actually ever run the else-part of the last code snippet?
Try to insert a print statement to be sure.
You should expect the first sound to play twice - the parameter (1) is number of repeats (after first play), not number of total play.

You don't show the full program, but please check that load_menu() is called repeatedly.
If you only run the function once and the music is already started, then you never get to the else-part.

Yes as soon as I started the game the else function is run (tested it with a print function) so everything should be fine.
And every time I go into the menu, it loads the menu and if the music is busy, it'll jut raise the volume. If not, else happens and it just restarts the music.

But it is run on-startup so it does pass over the "sounds.misc2" line (it also does the print statement).
Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 607
  • View blog
  • Posts: 1,927
  • Joined: 27-December 13

Re: [pygame] Music Queue ?

Posted 16 November 2016 - 05:04 AM

Hm, I'm not sure here, never tried using the music queue; but if music is queued, why do you need to start it more than once?
As I see it, every time you enter the else-block, you re-start the current music.
Was This Post Helpful? 0
  • +
  • -

#5 Hoaz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 09-April 15

Re: [pygame] Music Queue ?

Posted 16 November 2016 - 08:41 AM

View PostDK3250, on 16 November 2016 - 05:04 AM, said:

Hm, I'm not sure here, never tried using the music queue; but if music is queued, why do you need to start it more than once?
As I see it, every time you enter the else-block, you re-start the current music.

Yes, but it only does that if there's no music playing and I have returned to the menu. If there's music playing it won't do that.

However, thank you anyway ;-)
Was This Post Helpful? 0
  • +
  • -

#6 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 607
  • View blog
  • Posts: 1,927
  • Joined: 27-December 13

Re: [pygame] Music Queue ?

Posted 16 November 2016 - 11:54 AM

From the Pygame documentation about mixer.music.queue():

Quote

This will load a music file and queue it. A queued music file will begin as soon as the current music naturally ends. If the current music is ever stopped or changed, the queued song will be lost.

So mixer.music.queue only works if (first) music is playing - but you only enter the else-block when no music is active.
The queue will be lost if you stop or change the current music - which is what happens in the else-block.

Try experimenting with the queue function in a separate small test program that does nothing else than play queued music. This will give you a good feeling of how it is used.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1