0

I'm trying to, in a buffer, insert some text, then play a sound. Sample code is as follows:

(progn (insert "this won't be inserted until after the sound")
       (play-sound '(sound :file "/home/zck/Documents/countdown.el/Ovation-Mike_Koenig-1061486511.wav")))

Put the path to a wav file in the argument to play-sound. I've tried multiple files, but this is the one I'm using.

The result is that I hear the sound, then the text is inserted. How can I invert that?

2

Emacs normally doesn't render the screen until it's waiting for input. From the Emacs redisplay documentation:

Emacs normally tries to redisplay the screen whenever it waits for input.

Since play-sound blocks, Emacs doesn't wait for input until the sound ends, so it won't render the screen with the inserted text until play-sound returns.

Since we don't want to wait for input between inserting the text and playing the sound, we can explicitly call redisplay to force Emacs to redraw the screen.

(progn (insert "this won't be inserted until after the sound")
       (redisplay)
       (play-sound '(sound :file "/home/zck/Documents/countdown.el/Ovation-Mike_Koenig-1061486511.wav")))

This has the desired behavior of seeing the text inserted before the sound starts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.