Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I play more than one song at a time in PyGame?

Tags:

python

pygame

I've got it working now but with the time delay is there a better way because I want two different scripts to be working I want to have these playing in this order and have my images come up in order and the images are a long script and have time delays on them too.

#!/usr/bin/env python
import pygame
pygame.mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
print "hey I finaly got this working!"
sounda= pygame.mixer.Sound('D:/Users/John/Music/Music/FUN.OGG')
soundb= pygame.mixer.Sound('D:/Users/John/Music/Music/Still Alive.OGG')
soundc= pygame.mixer.Sound('D:/Users/John/Music/Music/turret.OGG')
soundd= pygame.mixer.Sound('D:/Users/John/Music/Music/portalend.OGG')
sounda.play()
pygame.time.delay(11000)
soundb.play()<P>
pygame.time.delay(180000)
soundc.play()
pygame.time.delay(90000)
soundd.play()
like image 521
user754010 Avatar asked May 14 '11 21:05

user754010


People also ask

How do you play two sounds at once in Python?

to play several (upto 8) sounds concurrently, you could use pygame. mixer. Sound() . pygame also provides a portable way to get keyboard input.

How do you resume a song on pygame?

It can be resumed with the unpause() function. This will resume the playback of a music stream after it has been paused.

How do you play background music in Pygame?

To load a background music file, call the pygame. mixer. music. load() function and pass it a string argument of the sound file to load.


1 Answers

Did you check the pygame.Mixer module ? On default, you can play 8 songs simultaneously

If you use the pygame.mixer.music, you'll be able to play only one song at the time.

If you use the pygame.mixer.sound, you'll be able to play up to 8 songs at the time.

The music module is here to stream music (it doesn't load all the music file at once).

The sound module is here to play differents sounds during game (sounds are completely loaded in memory).

So, in your example if you want to play the 4 songs at the same time :

import pygame
pygame.mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
print "hey I finaly got this working!"
sounds = []
sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/FUN.OGG'))
sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/Still Alive.OGG'))
sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/turret.OGG'))
sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/portalend.OGG'))
for sound in sounds:
    sound.play()
like image 119
Cédric Julien Avatar answered Oct 02 '22 16:10

Cédric Julien