Skip to content

Gtk.MediaFile

class — extends MediaStream, Gdk.Paintable

Implements the GtkMediaStream interface for files.

This provides a simple way to play back video files with GTK.

GTK provides a GIO extension point for GtkMediaFile implementations to allow for external implementations using various media frameworks.

GTK itself includes an implementation using GStreamer.

Constructors

new

@classmethod
def new(cls) -> MediaFile

Creates a new empty media file.

new_for_file

@classmethod
def new_for_file(cls, file: Gio.File) -> MediaFile

Creates a new media file to play file.

Parameters:

  • file — The file to play

new_for_filename

@classmethod
def new_for_filename(cls, filename: str | bytes | os.PathLike[str] | os.PathLike[bytes]) -> MediaFile

Creates a new media file for the given filename.

This is a utility function that converts the given filename to a GFile and calls MediaFile.new_for_file.

Parameters:

  • filename — filename to open

new_for_input_stream

@classmethod
def new_for_input_stream(cls, stream: Gio.InputStream) -> MediaFile

Creates a new media file to play stream.

If you want the resulting media to be seekable, the stream should implement the GSeekable interface.

Parameters:

  • stream — The stream to play

new_for_resource

@classmethod
def new_for_resource(cls, resource_path: str) -> MediaFile

Creates a new new media file for the given resource.

This is a utility function that converts the given resource to a GFile and calls MediaFile.new_for_file.

Parameters:

  • resource_path — resource path to open

Methods

clear

def clear(self) -> None

Resets the media file to be empty.

get_file

def get_file(self) -> Gio.File | None

Returns the file that self is currently playing from.

When self is not playing or not playing from a file, None is returned.

get_input_stream

def get_input_stream(self) -> Gio.InputStream | None

Returns the stream that self is currently playing from.

When self is not playing or not playing from a stream, None is returned.

set_file

def set_file(self, file: Gio.File | None = ...) -> None

Sets the GtkMediaFile to play the given file.

If any file is still playing, stop playing it.

Parameters:

  • file — the file to play

set_filename

def set_filename(self, filename: str | bytes | os.PathLike[str] | os.PathLike[bytes] | None = ...) -> None

Sets the GtkMediaFile to play the given file.

This is a utility function that converts the given filename to a GFile and calls MediaFile.set_file.

Parameters:

  • filename — name of file to play

set_input_stream

def set_input_stream(self, stream: Gio.InputStream | None = ...) -> None

Sets the GtkMediaFile to play the given stream.

If anything is still playing, stop playing it.

Full control about the stream is assumed for the duration of playback. The stream will not be closed.

Parameters:

  • stream — the stream to play from

set_resource

def set_resource(self, resource_path: str | None = ...) -> None

Sets the GtkMediaFile to play the given resource.

This is a utility function that converts the given resource_path to a GFile and calls MediaFile.set_file.

Parameters:

  • resource_path — path to resource to play

Virtual methods

do_close

def do_close(self) -> None

do_open

def do_open(self) -> None

Properties

file

file: Gio.File  # read/write

The file being played back or None if not playing a file.

input_stream

input_stream: Gio.InputStream  # read/write

The stream being played back or None if not playing a stream.

This is None when playing a file.