New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play sounds asynchronously, but let the function wait. #29

Closed
jarvisteach opened this Issue May 30, 2016 · 7 comments

Comments

Projects
None yet
1 participant
@jarvisteach
Owner

jarvisteach commented May 30, 2016

In order to stop the GUI returning, it can be made to wait for a sound to finish playing.

This makes the UI unresponsive.

It would be better to still play the sound asynchronously, but use the UI loop to wait until it's time to return.

@jarvisteach

This comment has been minimized.

Owner

jarvisteach commented Aug 11, 2016

Currently, wait can be populated (defaults to False)

If it's False, the SND_ASYNC property is set - causing the sound to play in the background and the GUI to return straight away.

Should always call with SND_ASYNC, however if wait=True, then start an after loop to keep going until the sound finishes: http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method

Not sure how to determine if sound is finished...

SND_NOWAIT might have done it, but is no longer supported
SND_NOSTOP might do it - keep trying to play a zero length sound, until successful, at which point return

@jarvisteach jarvisteach added this to the WindowsFirstRelease milestone Aug 12, 2016

jarvisteach pushed a commit that referenced this issue Sep 26, 2016

Richard Jarvis
Fixed bug with sounds waiting (#29)
ASYNC flag was always being set on sounds - now only set if wait is
False

@jarvisteach jarvisteach modified the milestones: WindowsFirstRelease, 0.07 Feb 25, 2017

@jarvisteach jarvisteach added the delayed label May 29, 2017

@jarvisteach

This comment has been minimized.

Owner

jarvisteach commented May 29, 2017

On hold until issue #120 has been implemented

@jarvisteach jarvisteach modified the milestones: 0.08, 0.07 May 29, 2017

@jarvisteach jarvisteach modified the milestones: 0.09, 0.08 Aug 9, 2017

@jarvisteach

This comment has been minimized.

Owner

jarvisteach commented Aug 29, 2017

So, we'll play the sound in a thread - with no wait, and use after to keep checking if it's finished (the thread will be dead).

Once the sound finishes we then call the registered function.

I want to push this model, rather than trying to make the code block, to force better code organisation.

@jarvisteach jarvisteach modified the milestones: 0.90, 1.0 Dec 10, 2017

@jarvisteach

This comment has been minimized.

Owner

jarvisteach commented Feb 12, 2018

The new threading model somewhat supports this.
Calling:

def play():
    app.playSound("sound.wav", True)
    return "done"

def done(txt):
    print(txt)

app.threadCallback(play, done)

Will play the sound in a separate thread, and only when the sound finishes, will the done() function be called.

@jarvisteach

This comment has been minimized.

Owner

jarvisteach commented Feb 12, 2018

However, winsound doesn't thread too well.

Trying to call winsound again will hang the gui, while it waits for the other thread to finish & exiting the GUI will also hang until the thread finishes.

@jarvisteach

This comment has been minimized.

Owner

jarvisteach commented Feb 12, 2018

However, the exit problem is (somewhat) similar to the general thread usage - if a thread is running, and you exit the GUI, the thread carries on...

@jarvisteach

This comment has been minimized.

Owner

jarvisteach commented Feb 12, 2018

winsound no longer supports the SNDPURGE & NOWAIT flags, so it's hard to have appJar deal well with this situation.

PLAY & STOP work well.

Running ASYNC tunes in a thread, with/without a callback works fine - with caveats:

  • Trying to STOP them will hang the GUI - as it waits for the tune to stop
  • Playing a second threaded tune will queue it to play after
  • Playing a non threaded tune while a threaded the is playing, will stop immediately

I'm OK with all of the above. So will update the docs, and close this issue.

jarvisteach pushed a commit that referenced this issue Feb 12, 2018

Richard Jarvis

jarvisteach added a commit that referenced this issue Feb 12, 2018

jarvisteach added a commit that referenced this issue Feb 12, 2018

jarvisteach pushed a commit that referenced this issue Feb 12, 2018

Richard Jarvis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment