le_marseillais
le_marseillais

Reputation: 39

The problem is that I can't play 2 sounds in a row (even with music.queue) it only plays 1 music and the programme stops, any idea?

I'm trying to make a program that plays a list of music in a row until I press a button to stop it, for this I want to use pygame.mixer.music. The problem is that I can't play 2 sounds in a row (even with music.queue) it only plays 1 music and the programme stops: I have tried it:

pygame.mixer.music.load("music1")
pygame.mixer.music.queue("music2")
pygame.mixer.music.play()

but nothing's working.

Upvotes: 4

Views: 126

Answers (1)

DCA-
DCA-

Reputation: 1292

pygame.mixer.music.play() is not blocking

so the program is finishing and exiting before the second item in the queue is processed.

try:

import time

pygame.mixer.music.load("music1")
pygame.mixer.music.queue("music2")
pygame.mixer.music.play()
time.sleep(60) # or however long you need to wait for the next song to play

If you want the program to exit when the music stops you can either poll the get_busy state of the mixer or you can register a callback to the end_event that is called when a queue finishes

What you want is a loop that continues running regardless of the state of the queue, so:

import pygame
pygame.init()
pygame.mixer.music.load("music1.mp3")
pygame.mixer.music.queue("music2.mp3")
pygame.mixer.music.play()

screen = pygame.display.set_mode((400,400))
clock = pygame.time.Clock()
paused = False
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.mixer.music.stop()
            done = True
        elif event.type == pygame.KEYDOWN: #press a key to pause and unpause
            if paused:
                pygame.mixer.music.unpause()
                paused = False
            else:
                pygame.mixer.music.pause()
                paused = True
        clock.tick(25)

pygame.quit()

Upvotes: 1

Related Questions