Giter VIP home page Giter VIP logo

mpris_server's Introduction

▶️ 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.

mpris_server's People

Contributors

alexdelorenzo avatar yashbhosale avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mpris_server's Issues

Can't Import the module.

I am trying to make a GUI overlay application for my media player which pulls my YT Music playlist and plays songs from there. Now leaving the overlay part and the GUI part, everything was complete so i thought that i should make it work with my media keys and so I found this module and installed it but while trying to import it shows the following error :

Traceback (most recent call last):
  File "media_player.py", line 7, in <module>
    from mpris_server.adapters import MprisAdapter
  File "/mnt/sda2/python/virtualenvs/yt-music-mod/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "/mnt/sda2/python/virtualenvs/yt-music-mod/lib/python3.8/site-packages/mpris_server/__init__.py", line 1, in <module>
    from . import adapters, base, types, server, mpris, interfaces
  File "/mnt/sda2/python/virtualenvs/yt-music-mod/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "/mnt/sda2/python/virtualenvs/yt-music-mod/lib/python3.8/site-packages/mpris_server/adapters.py", line 5, in <module>
    from .base import URI, MIME_TYPES, PlayState, DEFAULT_RATE, Microseconds, \
  File "/mnt/sda2/python/virtualenvs/yt-music-mod/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "/mnt/sda2/python/virtualenvs/yt-music-mod/lib/python3.8/site-packages/mpris_server/base.py", line 15, in <module>
    Props = list[Prop]
TypeError: 'type' object is not subscriptable

What should I do to fix this?

Implementation guide explanation / documentation anywhere?

Probably it's kind of a dumb question, but I've heard about mpris only today (because it turned out I'll need it). I've tried to implement the solution, I have a working server, however can't handle events. Here's the output of playerctl for example:

~$ playerctl --list-all
my_server_appears_here
~$ playerctl pause
No player could handle this command

I've used chromecast-mpris as an example as advised in the readme, but I wasn't able to figure this last part out. If anyone can help me, I can show a minimal example. Anyway, it's a nice module @alexdelorenzo , just lacks some documentation / actually working example (at least when comming without background knowledge).

Can I use it in a TUI (terminal) app?

HI @alexdelorenzo

Is it possible to use it in a TUI application?

I tried using it with PyRadio, a curses app, and I have failed 😉

The thing I did was I put mpris.loop() in a separate thread, but then all my debug messages (using logging) are displayed in the terminal, breaking the interface...
It's obvious I don't know what I'm doing here...

Is there anything I can do?

SyntaxError: invalid syntax

It builds fine with python -m build --wheel --no-isolation, however, I receive this output running python -m installer --destdir="$pkgdir" dist/*.whl with both 0.8.0 and 0.8.1:

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/base.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/base.py", line 27
    type Properties = list[Property]
         ^^^^^^^^^^
SyntaxError: invalid syntax

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/base.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/base.py", line 27
    type Properties = list[Property]
         ^^^^^^^^^^
SyntaxError: invalid syntax

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/events.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/events.py", line 47
    def emit_changes[T: MprisInterface](interface: T, changes: Changes):
                    ^
SyntaxError: expected '('

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/events.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/events.py", line 47
    def emit_changes[T: MprisInterface](interface: T, changes: Changes):
                    ^
SyntaxError: expected '('

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/server.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/server.py", line 37
    class Server[A: MprisAdapter, I: MprisInterface]:
                ^
SyntaxError: invalid syntax

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/server.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/server.py", line 37
    class Server[A: MprisAdapter, I: MprisInterface]:
                ^
SyntaxError: invalid syntax

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/types.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/types.py", line 9
    type GenericAliases = GenericAlias | _GenericAlias
         ^^^^^^^^^^^^^^
SyntaxError: invalid syntax

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/types.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/types.py", line 9
    type GenericAliases = GenericAlias | _GenericAlias
         ^^^^^^^^^^^^^^
SyntaxError: invalid syntax

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/interfaces/interface.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/interfaces/interface.py", line 20
    def log_trace[S: Self, **P, T](method: Method) -> Method:
                 ^
SyntaxError: expected '('

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/interfaces/interface.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/interfaces/interface.py", line 20
    def log_trace[S: Self, **P, T](method: Method) -> Method:
                 ^
SyntaxError: expected '('

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/mpris/compat.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/mpris/compat.py", line 24
    type ReturnsStr[**P] = Callable[P, str]
         ^^^^^^^^^^
SyntaxError: invalid syntax

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/mpris/compat.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/mpris/compat.py", line 24
    type ReturnsStr[**P] = Callable[P, str]
         ^^^^^^^^^^
SyntaxError: invalid syntax

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/mpris/metadata.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/mpris/metadata.py", line 21
    type Name = str
         ^^^^
SyntaxError: invalid syntax

*** Error compiling '/build/python-mpris_server/pkg/python-mpris_server/usr/lib/python3.11/site-packages/mpris_server/mpris/metadata.py'...
  File "/usr/lib/python3.11/site-packages/mpris_server/mpris/metadata.py", line 21
    type Name = str
         ^^^^
SyntaxError: invalid syntax

License unclear

The readme file claims the project is released under AGPLv3. So does the notice file. But the license file contains LGPLv3.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.