Active Topics

 


Reply
Thread Tools
El Amir's Avatar
Posts: 487 | Thanked: 152 times | Joined on Aug 2007 @ London, UK
#1
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?
__________________
Follow me on twitter HERE!

Applications I've made:
- Vuvuzela
- LTM: London Tube Map

Last edited by El Amir; 2010-04-24 at 18:01.
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#2
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's Avatar
Posts: 1,366 | Thanked: 1,185 times | Joined on Jan 2006
#3
and dont forget to set the pause=false after the second if.
__________________
N900_Email_Options Wiki Page
 
El Amir's Avatar
Posts: 487 | Thanked: 152 times | Joined on Aug 2007 @ London, UK
#4
Originally Posted by Joorin View Post
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

Originally Posted by mikec View Post
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!
__________________
Follow me on twitter HERE!

Applications I've made:
- Vuvuzela
- LTM: London Tube Map
 
qwerty12's Avatar
Posts: 4,274 | Thanked: 5,358 times | Joined on Sep 2007 @ Looking at y'all and sighing
#5
I'm no Python person, but shouldn't "playing" be a global?
 
giannoug's Avatar
Posts: 334 | Thanked: 171 times | Joined on Dec 2009
#6
Add a print before pygame.mixer.pause() to see if your IFs work as expected
 
lcuk's Avatar
Posts: 1,635 | Thanked: 1,816 times | Joined on Apr 2008 @ Manchester, England
#7
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
__________________
liqbase sketching the future.
like what i say? hit the Thanks, thanks!
twitter.com/lcuk
 
noobmonkey's Avatar
Posts: 3,203 | Thanked: 1,391 times | Joined on Nov 2009 @ Worthing, England
#8
Originally Posted by lcuk View Post
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 - just been having a read-up
http://www.pygame.org/docs/ref/mixer.html

EL Amir - take a look at these ones
http://code.activestate.com/recipes/...ss-platform-m/
__________________
----------- Follow me on Twitter here
----------- My Photography Website and Blog is here
----------- Author of the N900 Health Check Application ----------- New Version in Extras Devel (Dec 2010 - 2.9.10)
----------- Are you on the N900 World Map? - http://pininthemap.com/maemo - masterpin: shotgun
----------- What apps do you want to see on the n900 or in MeeGo in the future? -
 
Posts: 47 | Thanked: 22 times | Joined on Apr 2010
#9
Originally Posted by El Amir View Post
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

Last edited by Jinux; 2010-04-24 at 23:47.
 
Posts: 324 | Thanked: 371 times | Joined on Dec 2009 @ Vancouver, BC
#10
Originally Posted by qwerty12 View Post
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).
 

The Following 2 Users Say Thank You to Slocan For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 23:06.