Skip to main content

⏯️ Publish a MediaPlayer2 MPRIS device to D-Bus.

Project description

▶️ Add MPRIS integration to media players

mpris_server provides adapters to integrate MPRIS support in your media player or device. By supporting MPRIS in your app, you will allow Linux users to control all aspects of playback from the media controllers they already have installed.

Whereas existing MPRIS libraries for Python implement clients for apps with existing MPRIS support, mpris_server is a library used to implement MPRIS support in apps that don't already have it. If you want to give your media player an MPRIS interface, then mpris_server is right for you.

Check out 📺 cast_control for an app that uses mpris_server.

mpris_server is a fork of Mopidy-MPRIS that was extended and made into a general purpose library.

Features

Implements the following from the MPRIS specification:

  • MediaPlayer2
  • MediaPlayer2.Player
  • MediaPlayer2.Playlist
  • MediaPlayer2.TrackList

The library also provides an event handler that emits org.freedesktop.DBus.Properties.PropertiesChanged in response to changes in your media player. This allows for real-time updates from your media player to D-Bus.

Installation

Requirements

Installing PyGObject

On Debian-derived distributions like Ubuntu, install python3-gi with apt. On Arch, you'll want to install python-gobject.

On macOS, install pygobject3 via brew. Note that mpris_server on macOS hasn't been tested, but is theoretically possible to use.

Use pip to install PyGObject>=3.34.0 if there are no installation candidates available in your vendor's package repositories.

PyPI

pip3 install mpris_server

GitHub

Clone the repo, run pip3 install -r requirements.txt, followed by python3 setup.py install.

Usage

Implement adapters.MprisAdapter

Subclass adapters.MprisAdapter and implement each method.

After subclassing, pass an instance to an instance of server.Server.

Implement events.EventAdapter

Subclass adapters.EventAdapter. This interface has a good default implementation, only override its methods if your app calls for it.

If you choose to re-implement its methods, call emit_changes() with the corresponding interface and a List[str] of properties that changed.

Integrate the adapter with your application to emit changes in your media player that DBus needs to know about. For example, if the user pauses the media player, be sure to call EventAdapter.on_playpause() in the app. DBus won't know about the change otherwise.

Create the Server and Publish

Create an instance of server.Server, pass it an instance of your MprisAdapter, and call publish() to publish your media player via DBus.

mpris = Server('MyMediaPlayer', adapter=my_adapter)
mpris.publish() 

Call loop() to enter the DBus event loop, or enter the DBus event loop elsewhere in your code.

mpris.loop() 

Or:

from gi.repository import GLib


loop = GLib.MainLoop()
loop.run()

Example

from mpris_server.adapters import MprisAdapter
from mpris_server.events import EventAdapter
from mpris_server.server import Server
from mpris_server import Metadata

from my_app import app  # custom app you want to integrate


class MyAppAdapter(MprisAdapter):
  # Make sure to implement all methods on MprisAdapter, not just metadata()
  def metadata(self) -> Metadata:
    ...
  # and so on


class MyAppEventHandler(EventAdapter):
  # EventAdapter has good default implementations for its methods.
  # Only override the default methods if it suits your app.

  def on_app_event(self, event: str):
    # trigger DBus updates based on events in your app
    if event == 'pause':
      self.on_playpause()
    ...
  # and so on


# create mpris adapter and initialize mpris server
my_adapter = MyAppAdapter()
mpris = Server('MyApp', adapter=my_adapter)

# initialize app integration with mpris
event_handler = MyAppEventHandler(root=mpris.root, player=mpris.player)
app.register_event_handler(event_handler)

# publish and serve
mpris.loop()

Support

Want to support this project and other open-source projects like it?

Buy Me A Coffee

License

mpris_server is released under the AGPLv3, see LICENSE. Message me if you'd like to use this project with a different license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mpris_server-0.9.6.tar.gz (27.7 kB view details)

Uploaded Source

Built Distribution

mpris_server-0.9.6-py2.py3-none-any.whl (26.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file mpris_server-0.9.6.tar.gz.

File metadata

  • Download URL: mpris_server-0.9.6.tar.gz
  • Upload date:
  • Size: 27.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for mpris_server-0.9.6.tar.gz
Algorithm Hash digest
SHA256 4f465e0d089820084a47c6b0de2bf7aedc3373e4743d342e221ebe1e2e2b2074
MD5 0714e9dab35be27729797fd3b885c9d1
BLAKE2b-256 82f19397b8b697b7bb15f848dc9fd68cefa663a85e95f4a0c028fcb65e5e107a

See more details on using hashes here.

File details

Details for the file mpris_server-0.9.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for mpris_server-0.9.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 13e8ffaa3b6b907c743b6ba76e9a3dfe4c2274e0a8c99ca03f66d6073c39a7fe
MD5 ea13eaa19c8a9d860f060535f3c476b8
BLAKE2b-256 ab7915590e73b935eb1a1bbc6adb5e2a3165d522b84b29d6c9b8ad74f56bf521

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page