toggle between play pause and unpause

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

toggle between play pause and unpause

toufik
Hello.
This is my first e-mail in this mailling list.
I've a project using python 2.7 and pygame. The problem is
I've a button when I press it the music played and when I press it again
the music paused , but when I press the button in a third time the music
does not unpause
Any help please and I'm so sorry for the mistakes in writing .

played = True

def holder():
     time.sleep(0.001)

def play(track):
     global played
     pygame.mixer.init()
     pygame.mixer.music.load(track)
     t = wx.PyTimer(holder)
     if played :
         pygame.mixer.music.play()
         played = False
     elif pygame.mixer.music.get_busy():
         pygame.mixer.music.pause()
     elif not pygame.mixer.music.get_busy():
         pygame.mixer.music.unpause()
     else:
         played = True
     t.Start(10)


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: toggle between play pause and unpause

claudio canepa
watch your logic, unpause never executes (except in the rare cases when the calls to get_busy gets a different response) 

On Sun, May 7, 2017 at 2:34 PM, toufik <[hidden email]> wrote:
Hello.
This is my first e-mail in this mailling list.
I've a project using python 2.7 and pygame. The problem is
I've a button when I press it the music played and when I press it again the music paused , but when I press the button in a third time the music does not unpause
Any help please and I'm so sorry for the mistakes in writing .

played = True

def holder():
    time.sleep(0.001)

def play(track):
    global played
    pygame.mixer.init()
    pygame.mixer.music.load(track)
    t = wx.PyTimer(holder)
    if played :
        pygame.mixer.music.play()
        played = False
    elif pygame.mixer.music.get_busy():
        pygame.mixer.music.pause()
    elif not pygame.mixer.music.get_busy():
        pygame.mixer.music.unpause()
    else:
        played = True
    t.Start(10)



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: toggle between play pause and unpause

Sam Bull
In reply to this post by toufik
From the comments on the docs page:

    Yes, I've got the same problem. It only returns false when the music
    has been stopped, or no music has been loaded.
    Maybe there should be an is_paused() method...
    http://www.pygame.org/docs/ref/music.html#comment_pygame_mixer_music
    _get_busy

So, don't switch on get_busy(), perhaps just keep a record of the
button state.

e.g.

    if not started:
        pygame.mixer.music.play()
       started = True
        playing = True
    elif playing:
        pygame.mixer.music.pause()
        playing = False
    else:
        pygame.mixer.music.unpause()
        playing = True


Perhaps also using set_endevent() to monitor when it has finished
playing, and reset started to False.


Note that in your original code, the else part is unlikely to ever run.


On Sun, 2017-05-07 at 18:34 +0100, toufik wrote:

> played = True
>
> def holder():
>      time.sleep(0.001)
>
> def play(track):
>      global played
>      pygame.mixer.init()
>      pygame.mixer.music.load(track)
>      t = wx.PyTimer(holder)
>      if played :
>          pygame.mixer.music.play()
>          played = False
>      elif pygame.mixer.music.get_busy():
>          pygame.mixer.music.pause()
>      elif not pygame.mixer.music.get_busy():
>          pygame.mixer.music.unpause()
>      else:
>          played = True
>      t.Start(10)

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: toggle between play pause and unpause

Sam Bull
On Sun, 2017-05-07 at 13:41 -0700, [hidden email] wrote:
> Thank you for your reply. But nothing changed .
> the else block did not run .

This is where you need to do a little debugging on your own. Add a
print(started, playing) line into the play function and view what state
it is at each time you press the button, the output I would expect to
see from the first few clicks would be:

False False
True True
True False
True True

If that is the output you see, then the else block should be running.
You can test that by adding a print("FOO") line in the else block and
seeing if it runs on the 3rd button press.

> def play(track):
>     global playing, started 
>     pygame.mixer.init()
>     pygame.mixer.music.load(track)
>     t = wx.PyTimer(holder)
>     if not started :
>         pygame.mixer.music.play()
>         playing = True
>         started = True
>     elif playing:
>         pygame.mixer.music.pause()
>         playing = False
>     else:
>         paused()
>         playing = True
>     t.Start(10)
>
>
>
> > From the comments on the docs page: 
> >
> >     Yes, I've got the same problem. It only returns false when the
> > music 
> >     has been stopped, or no music has been loaded. 
> >     Maybe there should be an is_paused() method... 
> >     http://www.pygame.org/docs/ref/music.html#comment_pygame_mixer_
> > music 
> >     _get_busy 
> >
> > So, don't switch on get_busy(), perhaps just keep a record of the 
> > button state. 
> >
> > e.g. 
> >
> >     if not started: 
> >         pygame.mixer.music.play() 
> >        started = True 
> >         playing = True 
> >     elif playing: 
> >         pygame.mixer.music.pause() 
> >         playing = False 
> >     else: 
> >         pygame.mixer.music.unpause() 
> >         playing = True 
> >
> >
> > Perhaps also using set_endevent() to monitor when it has finished 
> > playing, and reset started to False. 
> >
> >
> > Note that in your original code, the else part is unlikely to ever
> > run. 
> >
> >
> > On Sun, 2017-05-07 at 18:34 +0100, toufik wrote: 
> > > played = True 
> > > 
> > > def holder(): 
> > >      time.sleep(0.001) 
> > > 
> > > def play(track): 
> > >      global played 
> > >      pygame.mixer.init() 
> > >      pygame.mixer.music.load(track) 
> > >      t = wx.PyTimer(holder) 
> > >      if played : 
> > >          pygame.mixer.music.play() 
> > >          played = False 
> > >      elif pygame.mixer.music.get_busy(): 
> > >          pygame.mixer.music.pause() 
> > >      elif not pygame.mixer.music.get_busy(): 
> > >          pygame.mixer.music.unpause() 
> > >      else: 
> > >          played = True 
> > >      t.Start(10)

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: toggle between play pause and unpause

Sam Bull
On Sun, 2017-05-07 at 14:15 -0700, [hidden email] wrote:
> I added the print "toufik" and my name was printed , but the music
> did not unpause so what is the problem .

And the paused() function you've put in the new code calls
pygame.mixer.music.unpause()? If so, then I don't know what the problem
is, it looks like the code should be correct now.

Perhaps share the complete code, so someone can try running it
themselves and see if it works for them.


> > On Sun, 2017-05-07 at 13:41 -0700, [hidden email] wrote: 
> > > Thank you for your reply. But nothing changed . 
> > > the else block did not run . 
> >
> > This is where you need to do a little debugging on your own. Add a 
> > print(started, playing) line into the play function and view what
> > state 
> > it is at each time you press the button, the output I would expect
> > to 
> > see from the first few clicks would be: 
> >
> > False False 
> > True True 
> > True False 
> > True True 
> >
> > If that is the output you see, then the else block should be
> > running. 
> > You can test that by adding a print("FOO") line in the else block
> > and 
> > seeing if it runs on the 3rd button press. 
> >
> > > def play(track): 
> > >     global playing, started  
> > >     pygame.mixer.init() 
> > >     pygame.mixer.music.load(track) 
> > >     t = wx.PyTimer(holder) 
> > >     if not started : 
> > >         pygame.mixer.music.play() 
> > >         playing = True 
> > >         started = True 
> > >     elif playing: 
> > >         pygame.mixer.music.pause() 
> > >         playing = False 
> > >     else: 
> > >         paused() 
> > >         playing = True 
> > >     t.Start(10) 
> > > 
> > > 
> > > 
> > > > From the comments on the docs page:  
> > > > 
> > > >     Yes, I've got the same problem. It only returns false when
> > the 
> > > > music  
> > > >     has been stopped, or no music has been loaded.  
> > > >     Maybe there should be an is_paused() method...  
> > > >     http://www.pygame.org/docs/ref/music.html#comment_pygame_mi
> > xer_ 
> > > > music  
> > > >     _get_busy  
> > > > 
> > > > So, don't switch on get_busy(), perhaps just keep a record of
> > the  
> > > > button state.  
> > > > 
> > > > e.g.  
> > > > 
> > > >     if not started:  
> > > >         pygame.mixer.music.play()  
> > > >        started = True  
> > > >         playing = True  
> > > >     elif playing:  
> > > >         pygame.mixer.music.pause()  
> > > >         playing = False  
> > > >     else:  
> > > >         pygame.mixer.music.unpause()  
> > > >         playing = True  
> > > > 
> > > > 
> > > > Perhaps also using set_endevent() to monitor when it has
> > finished  
> > > > playing, and reset started to False.  
> > > > 
> > > > 
> > > > Note that in your original code, the else part is unlikely to
> > ever 
> > > > run.  
> > > > 
> > > > 
> > > > On Sun, 2017-05-07 at 18:34 +0100, toufik wrote:  
> > > > > played = True  
> > > > >  
> > > > > def holder():  
> > > > >      time.sleep(0.001)  
> > > > >  
> > > > > def play(track):  
> > > > >      global played  
> > > > >      pygame.mixer.init()  
> > > > >      pygame.mixer.music.load(track)  
> > > > >      t = wx.PyTimer(holder)  
> > > > >      if played :  
> > > > >          pygame.mixer.music.play()  
> > > > >          played = False  
> > > > >      elif pygame.mixer.music.get_busy():  
> > > > >          pygame.mixer.music.pause()  
> > > > >      elif not pygame.mixer.music.get_busy():  
> > > > >          pygame.mixer.music.unpause()  
> > > > >      else:  
> > > > >          played = True  
> > > > >      t.Start(10) 

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: toggle between play pause and unpause

Sam Bull
On Sun, 2017-05-07 at 21:02 -0700, [hidden email] wrote:
> Yes I did just as you said. In the else block the print statement
> execute but the paused() function did not execute.
> Any help please.
> the complete code is in the test.py. 

OK, one last issue I can see:

    pygame.mixer.init()
    pygame.mixer.music.load(track)

This should only be run once. So, either run at the beginning of the
program or in the Test: __init__() method.

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: toggle between play pause and unpause

Sam Bull
From the docs:

    get_endevent(): Returns the event type to be sent every time the
    music finishes playback. If there is no endevent the function
    returns pygame.NOEVENT.


Therefore this has nothing to do with whether the music has stopped or
not.

With your other init code, you will want to run something like:

    from pygame.locals import *
    MUSICENDEVENT = USEREVENT

    set_endevent(MUSICENDEVENT)

And in your event loop:

    if event.type == MUSICENDEVENT:
        started = False

On Mon, 2017-05-08 at 02:47 -0700, [hidden email] wrote:
> Thank you very much now the track is pausing .But there is final
> problem I want to replay the music more than onece 
> How can I fix it, I used this code after else block :
>     if pygame.mixer.music.get_endevent():
>         playing = False
>         started = False 

signature.asc (188 bytes) Download Attachment
Loading...