How to play a sound or tone when a program ends?

Sbromberger, the sound is nothing fancy, but it does work when executed as a program (not just from the REPL).

Cormullion, thanks, but no Mac, I’m working from 2 Windows computers (one running Win 7.0 and one running Win 10.0).

Ideally, it would be nice to take some interesting mp3 or wav soundclip I could find & download from the web, and play it at when the program ends. For now I’m repeat-playing println("\007") in a while loop.

I am also using Windows (10) and I use a powershell script to accomplish that:

function sound_done()
    script = ```\$sound = New-Object System.Media.SoundPlayer\;
                \$sound.SoundLocation = "<sound file location>"\;
    run(`powershell /c $script`)

You need to have a wav audio file and then replace <sound file location> with the file location.

I just looked it up, you can also use \$sound.PlayLooping\(\) and other commands. You can find the documentation here.

Ludwig, I looked at your 2nd suggestion (PlayLooping), and it led to about 10 pages of documentation with no clear examples to try out (not clear to me anyway).

Trying out your first method gives me an error no matter how I try to locate or specify the file. The directory I wanted to put the wav file in had a space in the path, and using quotes or slashes to keep the space in the path name didn’t work. Then I put it in the “home” directory (according to Julia at startup), and it still didn’t work:

function sound_done()
    script = ```\$sound = New-Object System.Media.SoundPlayer\;
                \$sound.SoundLocation = "file_example_WAV_1MG.wav"\;
    run(`powershell /c $script`)


I get the error:

file_example_WAV_1MG.wav : The term 'file_example_WAV_1MG.wav' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:70
+ ... .SoundPlayer; $sound.SoundLocation = file_example_WAV_1MG.wav; $sound ...
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (file_example_WAV_1MG.wav:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

That file, "file_example_WAV_1MG.wav", is a sample audio file that plays fine when I click on it in Windows 7.

Actually I DID finally get this working:

function Play_A_Tune(SoundFileName1)
    script = ```\$sound = New-Object System.Media.SoundPlayer\;
                \$sound.SoundLocation = $(SoundFileName1)\;
    run(`powershell /c $script`)

With a call like:


But I still can’t get it to run the audio file from a directory path with spaces in it. For example, none of these kinds of things seem to be working:

Play_A_Tune("C:\\Data Area\\Folder\\file_example_WAV_1MG.wav") Play_A_Tune("C:\\Data\ Area\\Folder\\file_example_WAV_1MG.wav") Play_A_Tune("C:\\"Data Area"\\Folder\\file_example_WAV_1MG.wav")

…no matter what I’ve tried.
(It’s possible that one problem may be that my directory path is a really long path name with many subdirectories.)

Works on Windows.


Did you try typing raw before the filepath? For example:

Play_A_Tune(raw"C:\\Data Area\\Folder\\file_example_WAV_1MG.wav")

When I want to target a specific file on Windows, I usually Shift+Right Click on the file in Windows Explorer, select Copy as Path, and then paste the path after first typing raw (as above).

Hi Joseph M., good idea trying “raw”, I didn’t know about that option, but it didn’t work.

PetrKryslUCSD, I already implemented that, see above.

That doesn’t look right to me. If you have raw"", then you shouldn’t need to do \\ to get a single backslash. As written, this will put literal double backslashes into the path. I’m not sure how Windows handles that.

julia> for character in raw"C:\\Data Area"

Edit: unless powershell also needs the \\ to escape the backslashes itself? In which case what you’ve written is probably correct.

Good idea Joseph, but changing the \\ back to \ doesn’t fix it.

Oddly, something like FileName = "C:\\Data Area\\Folder\\file_example_WAV_1MG.wav" IS working for commands like writedlm and readdlm, but NOT for Play_A_Tune here. Strange!

Right, I suspect this is related to spaces in a path with powershell (see this discussion). I think the following will catch Windows folders with and without spaces:

function play_a_tune2(SoundFileName1)
    script = ```\$sound = New-Object System.Media.SoundPlayer\;
                \$sound.SoundLocation = \& \"$SoundFileName1\" \;
    run(`powershell /c $script`)

I tested this with a .wav file with two paths on my Windows 10 machine:


play_a_tune2("C:\\folder with spaces\\elephant.wav")

I hear an elephant with both function calls. Or I could also be going crazy.


Yes Joseph, that does work in both cases, thanks!

Not sure why you’re hearing elephants though, might want to get your ears checked.

using RCall

beep() = begin


AFAIK R’s beep simply sends an \a to the terminal — see @anon94023334’s much simper solution for this above.


I’m pretty much good on this topic, jm’s “play_a_tune2” solution above worked; the only thing I can’t seem to do is set the filepath equal to a String variable, and then call it with play_a_tune2(variable); I have to call it with play_a_tune(<whole messy file path>). (But that’s not a big problem.)

Thanks everyone!

println("\007") doesn’t work for me on Ubuntu 18.04, but after:

sudo apt install beep
sudo modprobe pcspkr

the awesome power of beep is available, assuming that you have an old fashion motherboard speaker installed:

run(`beep -l 1000`)

Most distros don’t enable the bell events on a desktop by default. Eg for PulseAudio something like PulseAudio - ArchWiki can be used to re-enable it.

On Linux, I would just call a command line player with a path.

There are Linux commands such as spd-say:

run(`spd-say 'Stay alert!'`)

Although using this is not advisable if you are afraid of robotic voices.

1 Like

Paul, Tamas, Zlatan, I am running Julia in atom in Windows 7 & Windows 10 on my 2 computers. I don’t know how to implement any of those last 3 suggestions in Windows.

I don’t use Windows so I can’t provide specific help, but I would just search for solutions playing audio from the command line on Windows, eg