# Play a sound in Jupyter Notebook

**URL:** <https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870>\
**Category:** General Usage\
**Tags:** jupyter, sound\
**Created:** [October 4, 2018, 12:03pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870 "2018-10-04T12:03:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [October 4, 2018, 12:03pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/1 "2018-10-04T12:03:14Z")

</div>

I want my Jupyter notebook to play a sound when a cell is done, what is the easiest way to do this?

```julia
long_running_function()
play_sound()

```

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [October 4, 2018, 1:36pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/2 "2018-10-04T13:36:02Z")

</div>

Haven’t tried myself, but if you have e.g. an `IOStream` of an mp3, you might be  
able to just `display` it with the appropriate mimetype. Something like:

```julia
open("mymp3.mp3", "r") do f
    display("audio/mpeg", f)
end

```

Don’t have access to a working jupyter installation at the moment, so I can’t  
try it out. Let me know if that works.

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [October 4, 2018, 1:51pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/3 "2018-10-04T13:51:36Z")

</div>

That would probably just give you a widget with the audio file, though maybe there’s a way to set an auto-play flag on it or something?

If the notebook is running on the same machine as Julia, you can use PortAudio/LibSndFile to load the sound and play it, though things are a bit of a mess right now with respect to the 1.0 update. You’ll need to use `master` for most of the JuliaAudio packages.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [October 4, 2018, 2:37pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/4 "2018-10-04T14:37:43Z")

</div>

that uses the julia process, right? In my case that process runs on another machine.

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [October 4, 2018, 2:44pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/5 "2018-10-04T14:44:09Z")

</div>

What I thought it would do is send the jupyter process a bytestream for the mp3 file and the mimetype associated with the bytestream. The notebook then chooses how to display it itself. But I think @ssfrr is correct that it will give a widget rather than an auto-play. I know that jupyter itself has an option to trigger auto-playing of audio, but I don’t know how to do that through IJulia.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [October 4, 2018, 2:45pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/6 "2018-10-04T14:45:29Z")

</div>

```julia
open("http://audiosoundclips.com/wp-content/uploads/2011/12/Bleep4.mp3", "r") do f
    display("audio/mpeg", f)
end

```

also complains about not being able to open the file.

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [October 4, 2018, 4:51pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/7 "2018-10-04T16:51:04Z")

</div>

Well you’re trying to open a URL with something that is only looking at your local file-system. You’re gonna need an http lib if you want to play something from a website.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [October 4, 2018, 4:58pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/8 "2018-10-04T16:58:46Z")

</div>

You probably need to:

- use `WebIO.jl` (and host sound file in `/assets`)
- add some javascript that plays music (and handle it with Julia)

?

---

<div class="post-metadata">

**Author:** ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)\
**Post date:** [October 5, 2018, 7:44am UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/9 "2018-10-05T07:44:50Z")

</div>

```julia
using WebIO
playsound() = dom"script"("var audio = new Audio('http://audiosoundclips.com/wp-content/uploads/2011/12/Bleep4.mp3');audio.play();")

playsound()

```

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [August 14, 2019, 7:10pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/10 "2019-08-14T19:10:34Z")

</div>

Found this thread actually looking for how to have a player widget on a notebook, ended up with this after a [python example](https://nbviewer.jupyter.org/gist/Carreau/5507501/the%20sound%20of%20hydrogen.ipynb):

```julia
using WAV
wavwrite(0.4*rand(2000, 2), "example2.wav", Fs=44100.0)

audioplayer(filepath) = """
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Simple Test</title>
    </head>
    
    <body>
    <audio controls="controls" style="width:600px" >
      <source src="$filepath" type="audio/wav" />
      Your browser does not support the audio element.
    </audio>
    </body>
    """

display("text/html", audioplayer("example2.wav"))

```

---

<div class="post-metadata">

**Author:** ![neighthan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neighthan/32/37248_2.png) [@neighthan](https://discourse.julialang.org/u/neighthan)\
**Post date:** [April 8, 2020, 9:13pm UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/11 "2020-04-08T21:13:35Z")

</div>

I just made a Package here ([https://github.com/neighthan/IJuliaBell.jl](https://github.com/neighthan/IJuliaBell.jl)) which exports the macro @bell for easy notification when a command is done running. You can customize the sound of the bell by replacing the bell.wav file that the package installs. This is my first Julia package, so let me know if you try using it and there are any issues. (I’ve submitted it to the General package repository, I think, so it should be easily installable in a few days).

---

<div class="post-metadata">

**Author:** ![neighthan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neighthan/32/37248_2.png) [@neighthan](https://discourse.julialang.org/u/neighthan)\
**Post date:** [April 12, 2020, 2:55am UTC](https://discourse.julialang.org/t/play-a-sound-in-jupyter-notebook/15870/12 "2020-04-12T02:55:41Z")

</div>

The package is in the general repository now, so you can install it with

```julia
using Pkg
Pkg.add("IJuliaBell")

```

and use it like

```julia
using IJuliaBell

@bell sleep(3)

```
