Skip to content

Commit cde7bd6

Browse files
committed
Implement notification sounds using HTMLAudioElement
1 parent 3a10efa commit cde7bd6

File tree

1 file changed

+20
-2
lines changed
  • src/jupyter_contrib_nbextensions/nbextensions/notify

1 file changed

+20
-2
lines changed

src/jupyter_contrib_nbextensions/nbextensions/notify/notify.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ define([
1919
sticky: false,
2020
play_sound: false
2121
};
22+
var audio_file = "./notify.mp3";
2223

2324
var current_time = function() {
2425
return new Date().getTime() / 1000;
@@ -101,6 +102,23 @@ define([
101102
}
102103
};
103104

105+
var play_notification_sound = function(opts) {
106+
/**
107+
* NB: the Web Notification API specifies a mechanism for playing sound
108+
* with notifications. As of 2017-08-22, it is unsupported in all browsers.
109+
* This is a workaround. It should be updated to an implementation like
110+
* this when browser support is available:
111+
*
112+
* opts["sound"] = require.toUrl(audio_file);
113+
*/
114+
try {
115+
var audio = new Audio(require.toUrl(audio_file));
116+
audio.play();
117+
} catch(e) {
118+
console.log('HTML5 Audio not supported in browser.');
119+
}
120+
};
121+
104122
var notify = function () {
105123
var elapsed_time = current_time() - start_time;
106124
if (enabled && !first_start && !busy_kernel && elapsed_time >= min_time) {
@@ -109,8 +127,8 @@ define([
109127
icon: Jupyter.notebook.base_url + "static/base/images/favicon.ico",
110128
requireInteraction: params.sticky
111129
};
112-
if (params.sound) {
113-
opts["sound"] = require.toUrl("./notify.mp3");
130+
if (params.play_sound) {
131+
play_notification_sound(opts);
114132
}
115133
var n = new Notification(Jupyter.notebook.notebook_name, opts);
116134
n.onclick = function(event){ window.focus(); }

0 commit comments

Comments
 (0)