maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   A loop inside a function or how to design a Play/Stop button (https://talk.maemo.org/showthread.php?t=50906)

El Amir 2010-04-24 08:37

A loop inside a function or how to design a Play/Stop button
 
Hi,

I've been stuck on the following for quite some time now so any help is GREATLY appreciated :)

I'm trying to create a Play/Pause button: Ive already got the play part sorted but I cant seem to get the looping part right!

My program will play the song when the "Play" button is played. Im trying to set up a loop that detect if the song is playing and it it is, it will pause it, if not, it will resume it.

Here's the code:

Code:


#initial imports
import sys, pygame, time
from PyQt4 import QtCore
from PyQt4 import QtGui
from pygame.locals import *
import pygame.mixer                                   
pygame.mixer.init()

pygame.init()

song = pygame.mixer.Sound("song.wav")

#initial conditions
playing = False
paused = True

#Starts the playback of the song
def startSong():
    print "Song Stared"
    playing = True # This changes the initial conditions
    paused = False
    print playing
    song.play()
    time.sleep(.1)

#This is where it doesn't work
def pauseSong():
    print "TEST" #This gets printed
    if playing == True:
        pygame.mixer.pause() #This *should* pause the song
        print "paused" # This doesn't get printed :(
        paused = True
    if playing == False:
        pygame.mixer.unpause()
        pause = False
       
#Main
if __name__=='__main__':

    app = QtGui.QApplication(sys.argv)

    window = QtGui.QWidget()
    window.resize(800, 400)


    window.setWindowTitle("My Pause Play Button ")

    button0 = QtGui.QPushButton("Play Song", window)
    button0.setGeometry(100, 300, 200, 70) 
    QtCore.QObject.connect(button0, QtCore.SIGNAL('clicked()'), startSong)

    buttonPause = QtGui.QPushButton("Pause", window)
    buttonPause.setGeometry(500, 300, 200, 70) 
    QtCore.QObject.connect(buttonPause, QtCore.SIGNAL('clicked()'), pauseSong)
   
    print playing # Just to test if the arguments are passed
   
    window.show()
   
    app.exec_()


As you can see, im quite stuck.

Does anyone have an idea? Should I consider nested loops?
Or maybe assign 2 Slots for 1 signal?

Joorin 2010-04-24 10:04

Re: A loop inside a function or how to design a Play/Stop button
 
I'm no Python buff, but white space is very important. In your pauseSong method, you've got an extra space on the second if-statement line.

This will give the parser trouble.

mikec 2010-04-24 13:06

Re: A loop inside a function or how to design a Play/Stop button
 
and dont forget to set the pause=false after the second if.

El Amir 2010-04-24 17:58

Re: A loop inside a function or how to design a Play/Stop button
 
Quote:

Originally Posted by Joorin (Post 625651)
I'm no Python buff, but white space is very important. In your pauseSong method, you've got an extra space on the second if-statement line.

This will give the parser trouble.

That's just a false-positive, an error that slipped when I copy-pasted, so that's not the problem I'm afraid

Quote:

Originally Posted by mikec (Post 625815)
and dont forget to set the pause=false after the second if.

just added that but it doesn't seem to change much.


ANyways, thanks for the input guys! :)

qwerty12 2010-04-24 18:20

Re: A loop inside a function or how to design a Play/Stop button
 
I'm no Python person, but shouldn't "playing" be a global?

giannoug 2010-04-24 18:27

Re: A loop inside a function or how to design a Play/Stop button
 
Add a print before pygame.mixer.pause() to see if your IFs work as expected ;)

lcuk 2010-04-24 18:30

Re: A loop inside a function or how to design a Play/Stop button
 
doesnt the python mixer have get_busy() method which can be used to check if things are in use?

pseudocode:

if pygame.mixer.get_busy()
python.mixer.pause()
else
python.mixer.unpause()


certainly sounds more feasible to me, but there might be a reason you cannot use similar
you shouldn't need to retain the state - technically it should come from each channel/the mixer

noobmonkey 2010-04-24 22:48

Re: A loop inside a function or how to design a Play/Stop button
 
Quote:

Originally Posted by lcuk (Post 626200)
doesnt the python mixer have get_busy() method which can be used to check if things are in use?

pseudocode:

if pygame.mixer.get_busy()
python.mixer.pause()
else
python.mixer.unpause()


certainly sounds more feasible to me, but there might be a reason you cannot use similar
you shouldn't need to retain the state - technically it should come from each channel/the mixer


ooo i like the pygame stuff :D - just been having a read-up :D
http://www.pygame.org/docs/ref/mixer.html

EL Amir - take a look at these ones :D
http://code.activestate.com/recipes/...ss-platform-m/

Jinux 2010-04-24 23:43

Re: A loop inside a function or how to design a Play/Stop button
 
Quote:

Originally Posted by El Amir (Post 626160)
That's just a false-positive, an error that slipped when I copy-pasted, so that's not the problem I'm afraid



just added that but it doesn't seem to change much.


ANyways, thanks for the input guys! :)

You have the wrong variable after the second if

It should be "paused = False" but at the moment you have "pause = False"

Code:

#initial conditions
playing = False
paused = True

...

#This is where it doesn't work
def pauseSong():
    print "TEST" #This gets printed
    if playing == True:
        pygame.mixer.pause() #This *should* pause the song
        print "paused" # This doesn't get printed :(
        paused = True
    if playing == False:
        pygame.mixer.unpause()
        pause = False

...

Also, according to this method:
Code:

#Starts the playback of the song
def startSong():
    print "Song Stared"
    playing = True # This changes the initial conditions
    paused = False
    print playing
    song.play()
    time.sleep(.1)

You start playback with "song.play()"
Would it not make more sense for the "pauseSong()" definition to execute "song.pause()" as opposed to "pygame.mixer.pause()"?

I'm not familiar with the mixer class so please correct me :)

Slocan 2010-04-24 23:44

Re: A loop inside a function or how to design a Play/Stop button
 
Quote:

Originally Posted by qwerty12 (Post 626191)
I'm no Python person, but shouldn't "playing" be a global?

That's right, there should be a "global playing" and "global paused" in both functions, otherwise the global variables will remain as is, and are not changed in the startSong function (only a local variable is changed).


All times are GMT. The time now is 07:03.

vBulletin® Version 3.8.8