Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

python pygame question


ynori7's Avatar
Future Emperor of Earth
0 0

i'm trying to make my own media player, but i have a problem. i can't allow the user to input (play, pause, stop, etc) and make it so it automatically plays the next song when one ends. if i put it in a while loop i can make it play one song after another, but then i can't have any user input. the only way i can see to do it is to create my own event which would be triggered when a song ends, but i can't find anything helpful on google. can anyone help me with this?


spyware's Avatar
Banned
0 0

Which command(s) do you use to play your "media"? Why not post some code (disable smilies & [code]).


richohealey's Avatar
Python Ninja
0 0

use a select loop,

OR (better option)

Create a try: except:

and raise an error if the song ends, or if the user does something,


ynori7's Avatar
Future Emperor of Earth
0 0

this is what i originally had, but it just sits and waits for the user input, so if it gets to the end of a song it waits. i added the pygame.mixer.music.queue which will make it play the next song, but after that it stops unless you input another command.

def GetSong(musicList, x):
    if not pygame.mixer.music.get_busy():
        pygame.mixer.music.load(musicList[x])
        pygame.mixer.music.play()
    else:
        pygame.mixer.music.stop()
        pygame.mixer.music.load(musicList[x])
        pygame.mixer.music.play()

def Stop():
    pygame.mixer.music.stop()
def Pause():
    pygame.mixer.music.pause()
def Resume():
    pygame.mixer.music.unpause()

while 1:
    command=raw_input('Whatcha wanna do?: ')
    if command=="play":
        GetSong(musicList, x)
    if command=="stop":
        Stop()
    if command=="pause":
        Pause()
    if command=="resume":
        Resume()
    if command=="skip":
        if x<len(musicList)-1:
            x+=1
            GetSong(musicList, x)
        else:
            x=0
            GetSong(musicList, x)
    if pygame.mixer.music.get_busy():
        if x<len(musicList)-1:
            x+=1
        else:
            x=0
        pygame.mixer.music.queue(musicList[x])

i tried switching to a key listener with tkinter frames, but the only way it'll move on to the next song is if i make an event and bind it to the frame or maybe threading(which i also couldnt find any help on)


richohealey's Avatar
Python Ninja
0 0

don't thread this… just don't…

You want a select loop.

Or ideally, just use the events loop, and events.pump() every now and hten…


ynori7's Avatar
Future Emperor of Earth
0 0

richohealey wrote: don't thread this… just don't…

You want a select loop.

Or ideally, just use the events loop, and events.pump() every now and hten…

i'm using windows. looks like select is only for unix. i couldnt find anything on events.pump() (google only got 4 results and they were no good)


ynori7's Avatar
Future Emperor of Earth
0 0

okay, i found some stuff on msvcrt.getch() and events loops. i think it'll work. thanks


ynori7's Avatar
Future Emperor of Earth
0 0

apparantly msvcrt.getch() doesnt work right in IDLE. is there another way to keyboard interrupt?


reaper4334's Avatar
Member
0 0

If it doesn't work in IDLE, just run in by double clicking on it (in Windows, right?) or by using "python programname.py" in command prompt/terminal(linux)

I think that's what you mean anyway


ghost's Avatar
0 0

reaper4334 is right.

You need to run it from the commandline. lots of things dont work right in idle.

GUI and espace chars (\b, etc)

this is because idle itself is coded in python and uses the eval() function to run the code.

use idle as an IDE and run it at command line… i do this anyways.. just makes for a better testbed


ynori7's Avatar
Future Emperor of Earth
0 0

okay, i got msvcrt.getch() to work, but now i'm back where i started cuz the interpreter still sits there and waits for key input when i call up msvcrt.getch(). so does anyone know if there's a simple way to just check to see if the keyboard gets used, that way i can just call up getch() only if someone types something on the keyboard