chom
chom

Reputation: 285

can not hear the sound from pygame

I am using the following code to play a song using pygame library in python. It plays the song and i can hear the sound if i click directly on my python file. But if i run my program using python(command line) or python(GUI) i can not hear the sound. I checked on both python 2.6 and 2.7. I am using windows 7 OS.

My code:

import pygame,time,sys

pygame.init()

pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)

print "Mixer settings", pygame.mixer.get_init()

print "Mixer channels", pygame.mixer.get_num_channels()

pygame.mixer.music.set_volume(1.0)

pygame.mixer.music.load("2.mp3")

while 1:

    selection = raw_input()

    if selection == "play":

        print "Playing"

        pygame.mixer.music.play()

    elif selection == "rewind":

        pygame.mixer.music.rewind()

    elif selection == "pause":

        pygame.mixer.music.pause()

    elif selection == "stop":

        pygame.mixer.music.stop()

    elif selection == "queue":

        inputqueue = raw_input()

        pygame.mixer.music.queue(inputqueue)

    else:

        print "invalid selection"

        sys.stdout.flush()

Upvotes: 0

Views: 1848

Answers (1)

Julio Rodrigues
Julio Rodrigues

Reputation: 3413

You need to make a pygame loop so you can listen to the music. You should use http://www.pygame.org/docs/ref/key.html the key library to get the input

import pygame,time,sys

#pygame.init()
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
print "Mixer settings", pygame.mixer.get_init()
print "Mixer channels", pygame.mixer.get_num_channels()
pygame.mixer.music.set_volume(1.0)
pygame.mixer.music.load("2.mp3")
pygame.mixer.music.play()

clock = pygame.time.Clock()
while pygame.mixer.music.get_busy():
   # check if playback has finished
   clock.tick(30)

Upvotes: 1

Related Questions