It's been a while now that Gtk4 shipped out, with a new Gtk.Video()
component that allows to display a video in a Gtk window without resorting to using Gstreamer.
It's a very simple class with only a handful of subclasses (4) methods (10) & attributes (2).
But no matter how hard I try, I can't find a single example (? not even in C) ; I got this far :
#!/usr/bin/env python3
import sys
import gi
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw, Gio
class MainWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(title="Mini Player", *args, **kwargs)
player = Gtk.Video.new()
player.set_autoplay(True)
print('file: ', player.props.file) # => None
file_to_play = Gio.File.new_for_path('/my/valid/video/file.mp4')
player.set_file(file_to_play) # => this is supposed to start the playing
self.set_child(player)
print('file: ', player.props.file) # => file: __gi__.GLocalFile object
print('autoplay: ', player.props.autoplay) # => True
# self.show() # I tried this too, it does nothing
class MyApp(Adw.Application):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.connect('activate', self.on_activate)
self.connect('open', self.on_open)
self.set_flags(Gio.ApplicationFlags.HANDLES_OPEN) # Need to tell GApplication we can handle this
self.win = None
def on_activate(self, app):
self.win = MainWindow(application=app)
self.win.present()
# This is to avoid the error "Your application claims to support opening files but does not implement g_application_open() and has no handlers connected to the 'open' signal." while I learn how to pass this file to the Mainwindow class
def on_open(self, app, files, n_files, hint):
self.on_activate(app)
for file in files:
print("File to open: " + file.get_path())
app = MyApp(application_id="com.example.GtkApplication")
app.run(sys.argv)
The window shows up, with a neat play button in the lower left corner, all signals are good, no warnings or messages in the console, but the video doesn't play, the play button does nothing and the window stays black.
Am I missing something obvious?
Ok, I found out that I missed a library:
sudo apt install libgtk-4-media-gstreamer
Dit the trick, it works now. It's very slow but it works.