bcxavier92
bcxavier92

Reputation: 21

Play music and get user input simultaneously?

Can I get input and play music from pygame at the same time?

The code I have now:

from pygame import mixer

mixer.init()
mixer.music.load('song.mp3')
mixer.music.play()

var = raw_input("Input: ")
print "you said ", var


while pygame.mixer.music.get_busy():
    pygame.time.Clock().tick(10)

It works fine until I enter something, and then the program stops:

Input: test
you said  test
Traceback (most recent call last):
  File "play.py", line 10, in <module>
    while pygame.mixer.music.get_busy():
NameError: name 'pygame' is not defined

Upvotes: 1

Views: 676

Answers (1)

John La Rooy
John La Rooy

Reputation: 304255

from pygame import mixer

puts mixer in your namespace, but not pygame

You can either:

while mixer.music.get_busy():

or

import pygame
from pygame import mixer

importing pygame as well as it's subpackages you need is usually a good approach

Upvotes: 2

Related Questions