1

I'm creating a helper GNOME extension for my theme. The helper extension was meant to be used to add some sounds when I click a button, however, I can't find a solution (and an extension) that shows how to add the sounds and how to call them.

Actually, I found some questions and articles that explains how to add an audio function, but most of it requires HTML.

I've tried using this code (through the Looking Glass and the extension.js file) from here, which says that it doesn't require HTML things:

function playSound() {
    var audio = new Audio('/path/to/audio/file');
    audio.play();
}

However, it returned unknown function error for Audio():

ReferenceError: Audio is not defined

Can someone help me? Thanks!

1 Answers1

1

If you are using GNOME Shell >= 3.32 you can use MetaSoundPlayer:

const Gio = imports.gi.Gio;

let player = global.display.get_sound_player();

// Themed sound
player.play_from_theme('phone-incoming-call', 'arbitrary description', null);

// Sound File
let soundFile = Gio.File.new_for_path('/some/path/sound.ogg');
player.play_from_file(soundFile, 'arbitrary description', null);

Or there are global functions in GNOME Shell <= 3.30 (old docs):

// Themed sound
global.play_theme_sound(0, 'phone-incoming-call', 'arbitrary description', null);


// File name
global.play_sound_file(0, '/some/path/sound.ogg', 'arbitrary description', null);

There is a gnome-shell commit showing examples of both here.

andy.holmes
  • 3,383
  • 17
  • 28
  • I'm using Ubuntu 19.04 with GNOME 3.32 installed. I tried to use the MetaSoundPlayer function, but it returned invalid argument type error: `Expected type interface for Argument 'file' but got type 'string'`. I also tried the global player function, but it seems that it's no longer working. Not sure with vanilla GNOME, though. – WihayaPatrik Oct 02 '19 at 04:00
  • Sorry, apparently the newer method expects a `GFile` (or an object implementing that interface) as its first argument. I've updated the answer accordingly. – andy.holmes Oct 03 '19 at 07:33
  • Okay, your edited answer works, but it must be disabled on every log out or it's giving `TypeError: player is null`. Also, another question: how to trigger this on every click? – WihayaPatrik Oct 03 '19 at 13:49
  • Sorry, I'm not sure what you mean by "disabled on every logout"? It sounds like you would benefit from a more general introduction to shell extensions: https://wiki.gnome.org/Projects/GnomeShell/Extensions/Writing – andy.holmes Oct 03 '19 at 16:57
  • Sorry, I mean the user must disable the extension (through gnome-shell-extension-prefs or GNOME Tweaks) before logging out, otherwise the extension will fail to load on the next login. For my last question, I mean how to trigger the sound when the user clicks a button (Activities button for example) or launches an app? – WihayaPatrik Oct 06 '19 at 02:36
  • That probably means you aren't using the `init()`, `enable()` and `disable()` hooks properly; you should review the link above for details about how to use those. To call a function in response to user input, you need to set a callback for the given object by calling the `connect('signal-name', callback)` method on it. – andy.holmes Oct 07 '19 at 02:24