I am completely new to Gtk development, and I am struggling to find a way to play sound using gtk.
From the documentation, it seems there is a MediaFile class that inherits a MediaStream class that can play files, although I am not sure whether this suits Audio files.
/beep.ogg is an absolute path (which is probably not what you intended), and also is not a URI.
This API seems to fail silently when the file is missing. You can connect to notify::prepared on the Gtk.MediaStream and then check the has-audio property.
The example in Workbench still works for me after modifying it not to use Gtk.MediaControls.
In Workbench, you can just not set controls.media_stream, and it’ll be inactive. Simplifying it all the way to this also works.
import Gio from "gi://Gio";
import Gtk from "gi://Gtk";
let file = Gio.File.new_for_uri(workbench.resolve("./Dog.ogg"));
let media_stream = Gtk.MediaFile.new_for_file(file);
media_stream.play();
I did notice at one point that it wouldn’t play the first time (even with the original, unmodified example). I haven’t been able to make that happen again though.